update transaction template api
This commit is contained in:
parent
571c3303c8
commit
bd2c1f07e7
|
|
@ -27,16 +27,11 @@ func ReadFile(filePath string) ([]byte, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func WriteFile(filePath string, content string) error {
|
func WriteFile(filePath string, content string) error {
|
||||||
content = "\r\n" + content
|
err := ioutil.WriteFile(filePath, []byte(content), 0777)
|
||||||
file, err := os.OpenFile(filePath, os.O_CREATE, 0644)
|
if err != nil {
|
||||||
if err == nil {
|
LogSystemError("Failed to write file (" + filePath + ")")
|
||||||
_, err = file.WriteString(content)
|
return err
|
||||||
if err != nil {
|
|
||||||
LogSystemError("Failed to write file (" + filePath + ")")
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
defer file.Close()
|
|
||||||
LogSystemInfo("Success write file (" + filePath + ")")
|
LogSystemInfo("Success write file (" + filePath + ")")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -68,6 +68,8 @@ func RegisterRouter(router *gin.Engine) {
|
||||||
authorized.POST("/transaction", service.AddTransactions)
|
authorized.POST("/transaction", service.AddTransactions)
|
||||||
authorized.GET("/transaction/payee", service.QueryTransactionPayees)
|
authorized.GET("/transaction/payee", service.QueryTransactionPayees)
|
||||||
authorized.GET("/transaction/template", service.QueryTransactionTemplates)
|
authorized.GET("/transaction/template", service.QueryTransactionTemplates)
|
||||||
|
authorized.POST("/transaction/template", service.AddTransactionTemplate)
|
||||||
|
authorized.DELETE("/transaction/template", service.DeleteTransactionTemplate)
|
||||||
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)
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,14 @@
|
||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha1"
|
||||||
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/beancount-gs/script"
|
"github.com/beancount-gs/script"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/shopspring/decimal"
|
"github.com/shopspring/decimal"
|
||||||
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
@ -59,18 +62,18 @@ type AddTransactionForm struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
type AddTransactionEntryForm struct {
|
type AddTransactionEntryForm struct {
|
||||||
Account string `form:"account" binding:"required"`
|
Account string `form:"account" binding:"required" json:"account"`
|
||||||
Number decimal.Decimal `form:"number"`
|
Number decimal.Decimal `form:"number" json:"number"`
|
||||||
//Currency string `form:"currency"`
|
Currency string `form:"currency" json:"currency"`
|
||||||
Price decimal.Decimal `form:"price"`
|
Price decimal.Decimal `form:"price" json:"price"`
|
||||||
//PriceCurrency string `form:"priceCurrency"`
|
PriceCurrency string `form:"priceCurrency" json:"priceCurrency"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func sum(entries []AddTransactionEntryForm, openingBalances string) decimal.Decimal {
|
func sum(entries []AddTransactionEntryForm, openingBalances string) decimal.Decimal {
|
||||||
sumVal := decimal.NewFromInt(0)
|
sumVal := decimal.NewFromInt(0)
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
if entry.Account == openingBalances {
|
if entry.Account == openingBalances {
|
||||||
return sumVal
|
return decimal.NewFromInt(0)
|
||||||
}
|
}
|
||||||
if entry.Price.IntPart() == 0 {
|
if entry.Price.IntPart() == 0 {
|
||||||
sumVal = entry.Number.Add(sumVal)
|
sumVal = entry.Number.Add(sumVal)
|
||||||
|
|
@ -190,38 +193,120 @@ func QueryTransactionPayees(c *gin.Context) {
|
||||||
OK(c, result)
|
OK(c, result)
|
||||||
}
|
}
|
||||||
|
|
||||||
type transactionTemplate struct {
|
type TransactionTemplate struct {
|
||||||
Id string `json:"id"`
|
Id string `json:"id"`
|
||||||
Date string `json:"date"`
|
Date string `form:"date" binding:"required" json:"date"`
|
||||||
TemplateName string `json:"templateName"`
|
TemplateName string `form:"templateName" binding:"required" json:"templateName"`
|
||||||
Payee string `json:"payee"`
|
Payee string `form:"payee" json:"payee"`
|
||||||
Desc string `json:"desc"`
|
Desc string `form:"desc" binding:"required" json:"desc"`
|
||||||
Entries []transactionTemplateEntity `json:"entries"`
|
Entries []AddTransactionEntryForm `form:"entries" json:"entries"`
|
||||||
}
|
|
||||||
|
|
||||||
type transactionTemplateEntity struct {
|
|
||||||
Account string `json:"account"`
|
|
||||||
Commodity string `json:"commodity"`
|
|
||||||
Amount string `json:"amount"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func QueryTransactionTemplates(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)
|
||||||
|
templates, err := getLedgerTransactionTemplates(filePath)
|
||||||
|
if err != nil {
|
||||||
|
InternalError(c, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
OK(c, templates)
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddTransactionTemplate(c *gin.Context) {
|
||||||
|
var transactionTemplate TransactionTemplate
|
||||||
|
if err := c.ShouldBindJSON(&transactionTemplate); err != nil {
|
||||||
|
BadRequest(c, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ledgerConfig := script.GetLedgerConfigFromContext(c)
|
||||||
|
filePath := script.GetLedgerTransactionsTemplateFilePath(ledgerConfig.DataPath)
|
||||||
|
templates, err := getLedgerTransactionTemplates(filePath)
|
||||||
|
if err != nil {
|
||||||
|
InternalError(c, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
t := sha1.New()
|
||||||
|
_, err = io.WriteString(t, time.Now().String())
|
||||||
|
if err != nil {
|
||||||
|
InternalError(c, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
transactionTemplate.Id = hex.EncodeToString(t.Sum(nil))
|
||||||
|
templates = append(templates, transactionTemplate)
|
||||||
|
|
||||||
|
err = writeLedgerTransactionTemplates(filePath, templates)
|
||||||
|
if err != nil {
|
||||||
|
InternalError(c, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
OK(c, transactionTemplate)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteTransactionTemplate(c *gin.Context) {
|
||||||
|
templateId := c.Query("id")
|
||||||
|
if templateId == "" {
|
||||||
|
BadRequest(c, "templateId is not blank")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ledgerConfig := script.GetLedgerConfigFromContext(c)
|
||||||
|
filePath := script.GetLedgerTransactionsTemplateFilePath(ledgerConfig.DataPath)
|
||||||
|
|
||||||
|
oldTemplates, err := getLedgerTransactionTemplates(filePath)
|
||||||
|
if err != nil {
|
||||||
|
InternalError(c, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
newTemplates := make([]TransactionTemplate, 0)
|
||||||
|
for _, template := range oldTemplates {
|
||||||
|
if template.Id != templateId {
|
||||||
|
newTemplates = append(newTemplates, template)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = writeLedgerTransactionTemplates(filePath, newTemplates)
|
||||||
|
if err != nil {
|
||||||
|
InternalError(c, err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
OK(c, templateId)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getLedgerTransactionTemplates(filePath string) ([]TransactionTemplate, error) {
|
||||||
|
result := make([]TransactionTemplate, 0)
|
||||||
if script.FileIfExist(filePath) {
|
if script.FileIfExist(filePath) {
|
||||||
bytes, err := script.ReadFile(filePath)
|
bytes, err := script.ReadFile(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
InternalError(c, err.Error())
|
return nil, err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
result := make([]transactionTemplate, 0)
|
|
||||||
err = json.Unmarshal(bytes, &result)
|
err = json.Unmarshal(bytes, &result)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
InternalError(c, err.Error())
|
return nil, err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
OK(c, result)
|
|
||||||
} else {
|
|
||||||
OK(c, new([]string))
|
|
||||||
}
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeLedgerTransactionTemplates(filePath string, templates []TransactionTemplate) error {
|
||||||
|
if !script.FileIfExist(filePath) {
|
||||||
|
err := script.CreateFile(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bytes, err := json.Marshal(templates)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = script.WriteFile(filePath, string(bytes))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue