code refactor

This commit is contained in:
MaysWind
2021-03-28 19:58:58 +08:00
parent d034661734
commit f9066cb81d
6 changed files with 66 additions and 90 deletions
-53
View File
@@ -1,53 +0,0 @@
package api
import (
"github.com/mayswind/lab/pkg/core"
"github.com/mayswind/lab/pkg/errs"
"github.com/mayswind/lab/pkg/log"
"github.com/mayswind/lab/pkg/models"
"github.com/mayswind/lab/pkg/services"
)
// StatisticApi represents statistic api
type StatisticApi struct {
transactions *services.TransactionService
}
// Initialize an statistic api singleton instance
var (
Statistics = &StatisticApi{
transactions: services.Transactions,
}
)
// TransactionStatisticsHandler returns transaction statistics of current user
func (a *StatisticApi) TransactionStatisticsHandler(c *core.Context) (interface{}, *errs.Error) {
var statisticReq models.TransactionStatisticRequest
err := c.ShouldBindQuery(&statisticReq)
if err != nil {
log.WarnfWithRequestId(c, "[statistics.TransactionOverviewHandler] parse request failed, because %s", err.Error())
return nil, errs.NewIncompleteOrIncorrectSubmissionError(err)
}
uid := c.GetCurrentUid()
totalAmounts, err := a.transactions.GetAccountsAndCategoriesTotalIncomeAndExpense(uid, statisticReq.StartTime, statisticReq.EndTime)
statisticResp := &models.TransactionStatisticResponse{
StartTime: statisticReq.StartTime,
EndTime: statisticReq.EndTime,
}
statisticResp.Items = make([]*models.TransactionStatisticResponseItem, len(totalAmounts))
for i := 0; i < len(totalAmounts); i++ {
totalAmountItem := totalAmounts[i]
statisticResp.Items[i] = &models.TransactionStatisticResponseItem{
CategoryId: totalAmountItem.CategoryId,
AccountId: totalAmountItem.AccountId,
TotalAmount: totalAmountItem.Amount,
}
}
return statisticResp, nil
}
+32
View File
@@ -192,6 +192,38 @@ func (a *TransactionsApi) TransactionMonthListHandler(c *core.Context) (interfac
return transactionResps, nil
}
// TransactionStatisticsHandler returns transaction statistics of current user
func (a *TransactionsApi) TransactionStatisticsHandler(c *core.Context) (interface{}, *errs.Error) {
var statisticReq models.TransactionStatisticRequest
err := c.ShouldBindQuery(&statisticReq)
if err != nil {
log.WarnfWithRequestId(c, "[transactions.TransactionOverviewHandler] parse request failed, because %s", err.Error())
return nil, errs.NewIncompleteOrIncorrectSubmissionError(err)
}
uid := c.GetCurrentUid()
totalAmounts, err := a.transactions.GetAccountsAndCategoriesTotalIncomeAndExpense(uid, statisticReq.StartTime, statisticReq.EndTime)
statisticResp := &models.TransactionStatisticResponse{
StartTime: statisticReq.StartTime,
EndTime: statisticReq.EndTime,
}
statisticResp.Items = make([]*models.TransactionStatisticResponseItem, len(totalAmounts))
for i := 0; i < len(totalAmounts); i++ {
totalAmountItem := totalAmounts[i]
statisticResp.Items[i] = &models.TransactionStatisticResponseItem{
CategoryId: totalAmountItem.CategoryId,
AccountId: totalAmountItem.AccountId,
TotalAmount: totalAmountItem.Amount,
}
}
return statisticResp, nil
}
// TransactionGetHandler returns one specific transaction of current user
func (a *TransactionsApi) TransactionGetHandler(c *core.Context) (interface{}, *errs.Error) {
var transactionGetReq models.TransactionGetRequest
-21
View File
@@ -1,21 +0,0 @@
package models
// TransactionStatisticRequest represents all parameters of transaction statistic request
type TransactionStatisticRequest struct {
StartTime int64 `form:"start_time" binding:"min=0"`
EndTime int64 `form:"end_time" binding:"min=0"`
}
// TransactionStatisticResponse represents an item of transaction overview
type TransactionStatisticResponse struct {
StartTime int64 `json:"startTime"`
EndTime int64 `json:"endTime"`
Items []*TransactionStatisticResponseItem `json:"items"`
}
// TransactionStatisticResponseItem represents total amount item for an response
type TransactionStatisticResponseItem struct {
CategoryId int64 `json:"categoryId,string"`
AccountId int64 `json:"accountId,string"`
TotalAmount int64 `json:"amount"`
}
+20
View File
@@ -119,6 +119,12 @@ type TransactionListInMonthByPageRequest struct {
TrimTag bool `form:"trim_tag"`
}
// TransactionStatisticRequest represents all parameters of transaction statistic request
type TransactionStatisticRequest struct {
StartTime int64 `form:"start_time" binding:"min=0"`
EndTime int64 `form:"end_time" binding:"min=0"`
}
// TransactionGetRequest represents all parameters of transaction getting request
type TransactionGetRequest struct {
Id int64 `form:"id,string" binding:"required,min=1"`
@@ -170,6 +176,20 @@ type TransactionInfoPageWrapperResponse2 struct {
TotalCount int64 `json:"total_count"`
}
// TransactionStatisticResponse represents an item of transaction overview
type TransactionStatisticResponse struct {
StartTime int64 `json:"startTime"`
EndTime int64 `json:"endTime"`
Items []*TransactionStatisticResponseItem `json:"items"`
}
// TransactionStatisticResponseItem represents total amount item for an response
type TransactionStatisticResponseItem struct {
CategoryId int64 `json:"categoryId,string"`
AccountId int64 `json:"accountId,string"`
TotalAmount int64 `json:"amount"`
}
// IsEditable returns whether this transaction can be edited
func (t *Transaction) IsEditable(currentUser *User, utcOffset int16, account *Account, relatedAccount *Account) bool {
if currentUser == nil || !currentUser.CanEditTransactionByTransactionTime(t.TransactionTime, utcOffset) {