mirror of
https://github.com/mayswind/ezbookkeeping.git
synced 2026-05-16 16:07:33 +08:00
code refactor
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user