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

View File

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

View File

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

View File

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