add total amount in home page
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"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"
|
||||
"github.com/mayswind/lab/pkg/utils"
|
||||
)
|
||||
|
||||
// OverviewApi represents overview api
|
||||
type OverviewApi struct {
|
||||
transactions *services.TransactionService
|
||||
}
|
||||
|
||||
// Initialize an overview api singleton instance
|
||||
var (
|
||||
Overviews = &OverviewApi{
|
||||
transactions: services.Transactions,
|
||||
}
|
||||
)
|
||||
|
||||
// TransactionOverviewHandler returns transaction over of current user
|
||||
func (a *OverviewApi) TransactionOverviewHandler(c *core.Context) (interface{}, *errs.Error) {
|
||||
var transactionOverviewReq models.TransactionOverviewRequest
|
||||
err := c.ShouldBindQuery(&transactionOverviewReq)
|
||||
|
||||
if err != nil {
|
||||
log.WarnfWithRequestId(c, "[overviews.TransactionOverviewHandler] parse request failed, because %s", err.Error())
|
||||
return nil, errs.NewIncompleteOrIncorrectSubmissionError(err)
|
||||
}
|
||||
|
||||
items := strings.Split(transactionOverviewReq.Query, "|")
|
||||
requestItems := make([]*models.TransactionOverviewRequestItem, 0, len(items))
|
||||
|
||||
for i := 0; i < len(items); i++ {
|
||||
itemValues := strings.Split(items[i], "_")
|
||||
|
||||
if len(itemValues) != 3 {
|
||||
log.WarnfWithRequestId(c, "[overviews.TransactionOverviewHandler] parse request item failed, because its not valid item, content is \"%s\"", items[i])
|
||||
continue
|
||||
}
|
||||
|
||||
startTime, err := utils.StringToInt64(itemValues[1])
|
||||
|
||||
if err != nil {
|
||||
log.WarnfWithRequestId(c, "[overviews.TransactionOverviewHandler] parse request item start time failed, because %s", err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
endTime, err := utils.StringToInt64(itemValues[2])
|
||||
|
||||
if err != nil {
|
||||
log.WarnfWithRequestId(c, "[overviews.TransactionOverviewHandler] parse request item end time failed, because %s", err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
requestItem := &models.TransactionOverviewRequestItem{
|
||||
Name: itemValues[0],
|
||||
StartTime: startTime,
|
||||
EndTime: endTime,
|
||||
}
|
||||
|
||||
requestItems = append(requestItems, requestItem)
|
||||
}
|
||||
|
||||
if len(requestItems) < 1 {
|
||||
log.WarnfWithRequestId(c, "[overviews.TransactionOverviewHandler] parse request failed, because there are no valid items")
|
||||
return nil, errs.ErrQueryItemsEmpty
|
||||
}
|
||||
|
||||
if len(requestItems) > 5 {
|
||||
log.WarnfWithRequestId(c, "[overviews.TransactionOverviewHandler] parse request failed, because there are too many items")
|
||||
return nil, errs.ErrQueryItemsTooMuch
|
||||
}
|
||||
|
||||
uid := c.GetCurrentUid()
|
||||
|
||||
overviewResp := make(map[string]*models.TransactionOverviewResponseItem)
|
||||
|
||||
for i := 0; i < len(requestItems); i++ {
|
||||
requestItem := requestItems[i]
|
||||
|
||||
incomeAmount, expenseAmount, err := a.transactions.GetTotalIncomeAndExpenseByDateRange(uid, requestItem.StartTime, requestItem.EndTime)
|
||||
|
||||
if err != nil {
|
||||
log.ErrorfWithRequestId(c, "[overviews.TransactionOverviewHandler] failed to get transaction overview item for user \"uid:%d\", because %s", uid, err.Error())
|
||||
return nil, errs.ErrOperationFailed
|
||||
}
|
||||
|
||||
overviewResp[requestItem.Name] = &models.TransactionOverviewResponseItem{
|
||||
StartTime: requestItem.StartTime,
|
||||
EndTime: requestItem.EndTime,
|
||||
IncomeAmount: incomeAmount,
|
||||
ExpenseAmount: expenseAmount,
|
||||
}
|
||||
}
|
||||
|
||||
return overviewResp, nil
|
||||
}
|
||||
@@ -27,6 +27,7 @@ const (
|
||||
NormalSubcategoryCategory = 6
|
||||
NormalSubcategoryTag = 7
|
||||
NormalSubcategoryDataManagement = 8
|
||||
NormalSubcategoryOverview = 9
|
||||
)
|
||||
|
||||
// Error represents the specific error returned to user
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
package errs
|
||||
|
||||
import "net/http"
|
||||
|
||||
// Error codes related to overview
|
||||
var (
|
||||
ErrQueryItemsEmpty = NewNormalError(NormalSubcategoryOverview, 0, http.StatusBadRequest, "query items cannot be empty")
|
||||
ErrQueryItemsTooMuch = NewNormalError(NormalSubcategoryOverview, 1, http.StatusBadRequest, "query items too much")
|
||||
)
|
||||
@@ -0,0 +1,21 @@
|
||||
package models
|
||||
|
||||
// TransactionOverviewRequest represents all parameters of transaction overview request
|
||||
type TransactionOverviewRequest struct {
|
||||
Query string `form:"query"`
|
||||
}
|
||||
|
||||
// TransactionOverviewRequestItem represents an item of transaction overview request
|
||||
type TransactionOverviewRequestItem struct {
|
||||
Name string
|
||||
StartTime int64
|
||||
EndTime int64
|
||||
}
|
||||
|
||||
// TransactionOverviewResponseItem represents an item of transaction overview
|
||||
type TransactionOverviewResponseItem struct {
|
||||
StartTime int64 `json:"startTime"`
|
||||
EndTime int64 `json:"endTime"`
|
||||
IncomeAmount int64 `json:"incomeAmount"`
|
||||
ExpenseAmount int64 `json:"expenseAmount"`
|
||||
}
|
||||
@@ -44,6 +44,12 @@ type Transaction struct {
|
||||
DeletedUnixTime int64
|
||||
}
|
||||
|
||||
type TransactionTotalAmount struct {
|
||||
Uid int64
|
||||
Type TransactionDbType
|
||||
TotalAmount int64 `xorm:"NOT NULL"`
|
||||
}
|
||||
|
||||
// TransactionCreateRequest represents all parameters of transaction creation request
|
||||
type TransactionCreateRequest struct {
|
||||
Type TransactionType `json:"type" binding:"required"`
|
||||
|
||||
@@ -922,6 +922,38 @@ func (s *TransactionService) GetRelatedTransferTransaction(originalTransaction *
|
||||
return relatedTransaction
|
||||
}
|
||||
|
||||
// GetTotalIncomeAndExpenseByDateRange returns the total income and expense amount by specific date range
|
||||
func (s *TransactionService) GetTotalIncomeAndExpenseByDateRange(uid int64, startUnixTime int64, endUnixTime int64) (int64, int64, error) {
|
||||
if uid <= 0 {
|
||||
return 0, 0, errs.ErrUserIdInvalid
|
||||
}
|
||||
|
||||
startTransactionTime := utils.GetMinTransactionTimeFromUnixTime(startUnixTime)
|
||||
endTransactionTime := utils.GetMaxTransactionTimeFromUnixTime(endUnixTime)
|
||||
|
||||
var transactionTotalAmounts []*models.Transaction
|
||||
err := s.UserDataDB(uid).Select("uid, type, SUM(amount) as amount").Where("uid=? AND deleted=? AND (type=? OR type=?) AND transaction_time>=? AND transaction_time<=?", uid, false, models.TRANSACTION_DB_TYPE_INCOME, models.TRANSACTION_DB_TYPE_EXPENSE, startTransactionTime, endTransactionTime).GroupBy("type").Find(&transactionTotalAmounts)
|
||||
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
|
||||
var incomeAmount int64
|
||||
var expenseAmount int64
|
||||
|
||||
for i := 0; i < len(transactionTotalAmounts); i++ {
|
||||
transactionTotalAmount := transactionTotalAmounts[i]
|
||||
|
||||
if transactionTotalAmount.Type == models.TRANSACTION_DB_TYPE_INCOME {
|
||||
incomeAmount = transactionTotalAmount.Amount
|
||||
} else if transactionTotalAmount.Type == models.TRANSACTION_DB_TYPE_EXPENSE {
|
||||
expenseAmount = transactionTotalAmount.Amount
|
||||
}
|
||||
}
|
||||
|
||||
return incomeAmount, expenseAmount, nil
|
||||
}
|
||||
|
||||
func (s *TransactionService) isAccountIdValid(transaction *models.Transaction) error {
|
||||
if transaction.Type == models.TRANSACTION_DB_TYPE_MODIFY_BALANCE {
|
||||
if transaction.RelatedAccountId != 0 && transaction.RelatedAccountId != transaction.AccountId {
|
||||
|
||||
Reference in New Issue
Block a user