update transaction api

This commit is contained in:
BaoXuebin 2021-11-24 17:32:24 +08:00
parent a69f8e1091
commit af8716ce75
4 changed files with 40 additions and 32 deletions

View File

@ -14,6 +14,7 @@ type QueryParams struct {
Where bool `bql:"where"` Where bool `bql:"where"`
Year int `bql:"year ="` Year int `bql:"year ="`
Month int `bql:"month ="` Month int `bql:"month ="`
Account string `bql:"account ="`
AccountType string `bql:"account ~"` AccountType string `bql:"account ~"`
OrderBy string `bql:"order by"` OrderBy string `bql:"order by"`
Limit int `bql:"limit"` Limit int `bql:"limit"`
@ -41,6 +42,11 @@ func GetQueryParams(c *gin.Context) QueryParams {
queryParams.AccountType = c.Query("type") queryParams.AccountType = c.Query("type")
hasWhere = true hasWhere = true
} }
if c.Query("account") != "" {
queryParams.Account = c.Query("account")
queryParams.Limit = 100
hasWhere = true
}
queryParams.Where = hasWhere queryParams.Where = hasWhere
if c.Query("path") != "" { if c.Query("path") != "" {
queryParams.Path = c.Query("path") queryParams.Path = c.Query("path")

View File

@ -255,5 +255,5 @@ func GetCommoditySymbol(commodity string) string {
case "USD": case "USD":
return "$" return "$"
} }
return commodity return ""
} }

View File

@ -54,9 +54,9 @@ func RegisterRouter(router *gin.Engine) {
authorized.GET("/account/type", service.QueryAccountType) authorized.GET("/account/type", service.QueryAccountType)
authorized.GET("/stats/months", service.MonthsList) authorized.GET("/stats/months", service.MonthsList)
authorized.GET("/stats/total", service.StatsTotal) authorized.GET("/stats/total", service.StatsTotal)
authorized.GET("/transactions", service.QueryTransactions) authorized.GET("/transaction", service.QueryTransactions)
authorized.GET("/transactions/payee", service.QueryTransactionsPayee) authorized.GET("/transaction/payee", service.QueryTransactionPayees)
authorized.GET("/transactions/template", service.QueryTransactionsTemplate) authorized.GET("/transaction/template", service.QueryTransactionTemplates)
authorized.GET("/tags", service.QueryTags) authorized.GET("/tags", service.QueryTags)
authorized.GET("/file/dir", service.QueryLedgerSourceFileDir) authorized.GET("/file/dir", service.QueryLedgerSourceFileDir)
authorized.GET("/file/content", service.QueryLedgerSourceFileContent) authorized.GET("/file/content", service.QueryLedgerSourceFileContent)
@ -64,8 +64,7 @@ func RegisterRouter(router *gin.Engine) {
// 兼容旧版本 // 兼容旧版本
authorized.GET("/entry", service.QueryTransactions) authorized.GET("/entry", service.QueryTransactions)
authorized.GET("/payee", service.QueryTransactionsPayee) authorized.GET("/payee", service.QueryTransactionPayees)
authorized.GET("/transaction/template", service.QueryTransactionsTemplate)
} }
} }

View File

@ -7,17 +7,21 @@ import (
"strings" "strings"
) )
type Transactions struct { type Transaction struct {
Id string `bql:"id" json:"id"` Id string `bql:"id" json:"id"`
Date string `bql:"date" json:"date"` Account string `bql:"account" json:"account"`
Payee string `bql:"payee" json:"payee"` Date string `bql:"date" json:"date"`
Narration string `bql:"narration" json:"desc"` Payee string `bql:"payee" json:"payee"`
Account string `bql:"account" json:"account"` Narration string `bql:"narration" json:"desc"`
Tags []string `bql:"tags" json:"tags"` Number string `bql:"number" json:"number"`
Position string `bql:"position" json:"position"` Currency string `bql:"currency" json:"currency"`
Amount string `json:"amount"` CostDate string `bql:"cost_date" json:"costDate"`
Commodity string `json:"commodity"` CostPrice string `bql:"cost_number" json:"costPrice"` // 交易净值
CommoditySymbol string `json:"commoditySymbol"` CostCurrency string `bql:"cost_currency" json:"costCurrency"`
Price string `bql:"price" json:"price"`
Tags []string `bql:"tags" json:"tags"`
CurrencySymbol string `json:"currencySymbol,omitempty"`
CostCurrencySymbol string `json:"costCurrencySymbol,omitempty"`
} }
func QueryTransactions(c *gin.Context) { func QueryTransactions(c *gin.Context) {
@ -25,7 +29,7 @@ func QueryTransactions(c *gin.Context) {
queryParams := script.GetQueryParams(c) queryParams := script.GetQueryParams(c)
// 倒序查询 // 倒序查询
queryParams.OrderBy = "date desc" queryParams.OrderBy = "date desc"
transactions := make([]Transactions, 0) transactions := make([]Transaction, 0)
err := script.BQLQueryList(ledgerConfig, &queryParams, &transactions) err := script.BQLQueryList(ledgerConfig, &queryParams, &transactions)
if err != nil { if err != nil {
InternalError(c, err.Error()) InternalError(c, err.Error())
@ -33,24 +37,23 @@ func QueryTransactions(c *gin.Context) {
} }
// 格式化金额 // 格式化金额
for i := 0; i < len(transactions); i++ { for i := 0; i < len(transactions); i++ {
pos := strings.Split(transactions[i].Position, " ") symbol := script.GetCommoditySymbol(transactions[i].Currency)
if len(pos) == 2 { transactions[i].CurrencySymbol = symbol
transactions[i].Amount = pos[0] transactions[i].CostCurrencySymbol = symbol
transactions[i].Commodity = pos[1] if transactions[i].Price != "" {
transactions[i].CommoditySymbol = script.GetCommoditySymbol(pos[1]) transactions[i].Price = strings.Fields(transactions[i].Price)[0]
} }
transactions[i].Position = ""
} }
OK(c, transactions) OK(c, transactions)
} }
type transactionsPayee struct { type transactionPayee struct {
Value string `bql:"distinct payee" json:"value"` Value string `bql:"distinct payee" json:"value"`
} }
func QueryTransactionsPayee(c *gin.Context) { func QueryTransactionPayees(c *gin.Context) {
ledgerConfig := script.GetLedgerConfigFromContext(c) ledgerConfig := script.GetLedgerConfigFromContext(c)
payeeList := make([]transactionsPayee, 0) payeeList := make([]transactionPayee, 0)
queryParams := script.QueryParams{Where: false, OrderBy: "date desc", Limit: 100} queryParams := script.QueryParams{Where: false, OrderBy: "date desc", Limit: 100}
err := script.BQLQueryList(ledgerConfig, &queryParams, &payeeList) err := script.BQLQueryList(ledgerConfig, &queryParams, &payeeList)
if err != nil { if err != nil {
@ -66,22 +69,22 @@ func QueryTransactionsPayee(c *gin.Context) {
OK(c, result) OK(c, result)
} }
type transactionsTemplate struct { type transactionTemplate struct {
Id string `json:"id"` Id string `json:"id"`
Date string `json:"date"` Date string `json:"date"`
TemplateName string `json:"templateName"` TemplateName string `json:"templateName"`
Payee string `json:"payee"` Payee string `json:"payee"`
Desc string `json:"desc"` Desc string `json:"desc"`
Entries []transactionsTemplateEntity `json:"entries"` Entries []transactionTemplateEntity `json:"entries"`
} }
type transactionsTemplateEntity struct { type transactionTemplateEntity struct {
Account string `json:"account"` Account string `json:"account"`
Commodity string `json:"commodity"` Commodity string `json:"commodity"`
Amount string `json:"amount"` Amount string `json:"amount"`
} }
func QueryTransactionsTemplate(c *gin.Context) { func QueryTransactionTemplates(c *gin.Context) {
ledgerConfig := script.GetLedgerConfigFromContext(c) ledgerConfig := script.GetLedgerConfigFromContext(c)
filePath := script.GetLedgerTransactionsTemplateFilePath(ledgerConfig.DataPath) filePath := script.GetLedgerTransactionsTemplateFilePath(ledgerConfig.DataPath)
if script.FileIfExist(filePath) { if script.FileIfExist(filePath) {
@ -90,7 +93,7 @@ func QueryTransactionsTemplate(c *gin.Context) {
InternalError(c, err.Error()) InternalError(c, err.Error())
return return
} }
result := make([]transactionsTemplate, 0) result := make([]transactionTemplate, 0)
err = json.Unmarshal(bytes, &result) err = json.Unmarshal(bytes, &result)
if err != nil { if err != nil {
InternalError(c, err.Error()) InternalError(c, err.Error())