beancount-gs/service/transactions.go

643 lines
19 KiB
Go
Raw Normal View History

2021-11-21 14:37:13 +00:00
package service
import (
2021-11-29 14:21:25 +00:00
"crypto/sha1"
"encoding/hex"
"encoding/json"
2021-12-12 14:42:07 +00:00
"errors"
2021-11-28 12:19:40 +00:00
"fmt"
2024-10-24 15:26:07 +00:00
"github.com/beancount-gs/script"
"github.com/gin-gonic/gin"
"github.com/shopspring/decimal"
2021-11-29 14:21:25 +00:00
"io"
2024-10-01 06:43:07 +00:00
"strconv"
2021-11-22 08:47:49 +00:00
"strings"
2021-11-28 12:19:40 +00:00
"time"
2021-11-21 14:37:13 +00:00
)
2021-11-24 09:32:24 +00:00
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"`
2022-01-09 05:48:18 +00:00
Balance string `bql:"balance" json:"balance"`
2021-11-24 09:32:24 +00:00
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"`
IsAnotherCurrency bool `json:"isAnotherCurrency,omitempty"`
}
2024-10-24 15:26:07 +00:00
type RawTransaction struct {
RawText string `json:"text"`
StartLineNo int `json:"startLineNo"`
EndLineNo int `json:"endLineNo"`
FilePath string `json:"filePath,omitempty"`
}
2024-10-01 06:43:07 +00:00
type TransactionSort []Transaction
func (s TransactionSort) Len() int {
return len(s)
}
func (s TransactionSort) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s TransactionSort) Less(i, j int) bool {
a, _ := strconv.Atoi(s[i].Number)
b, _ := strconv.Atoi(s[j].Number)
return a <= b
}
2024-10-24 15:26:07 +00:00
func QueryTransactionDetailById(c *gin.Context) {
queryParams := script.GetQueryParams(c)
if queryParams.ID == "" {
BadRequest(c, "Param 'id' must not be blank.")
return
}
ledgerConfig := script.GetLedgerConfigFromContext(c)
transactions := make([]Transaction, 0)
err := script.BQLQueryList(ledgerConfig, &queryParams, &transactions)
if err != nil {
BadRequest(c, err.Error())
return
}
if len(transactions) == 0 {
BadRequest(c, "No transaction found.")
}
transactionForm := TransactionForm{}
transactionForm.Entries = make([]TransactionEntryForm, 0)
for _, transaction := range transactions {
if transactionForm.ID == "" {
transactionForm.ID = transaction.Id
transactionForm.Date = transaction.Date
transactionForm.Payee = transaction.Payee
transactionForm.Desc = transaction.Narration
transactionForm.Narration = transaction.Narration
}
transactionEntryForm := TransactionEntryForm{
Account: transaction.Account,
}
if transaction.Number != "" && transaction.Number != "0" {
transactionEntryForm.Number = decimal.RequireFromString(transaction.Number)
transactionEntryForm.Currency = transaction.Currency
transactionEntryForm.IsAnotherCurrency = transaction.IsAnotherCurrency
}
if transaction.CostPrice != "" && transaction.CostPrice != "0" {
transactionEntryForm.Price = decimal.RequireFromString(transaction.CostPrice)
transactionEntryForm.PriceCurrency = transaction.CostCurrency
}
transactionForm.Entries = append(transactionForm.Entries, transactionEntryForm)
}
OK(c, transactionForm)
}
func QueryTransactionRawTextById(c *gin.Context) {
queryParams := script.GetQueryParams(c)
if queryParams.ID == "" {
BadRequest(c, "Param 'id' must not be blank.")
}
ledgerConfig := script.GetLedgerConfigFromContext(c)
result, err := script.BQLPrint(ledgerConfig, queryParams.ID)
if err != nil {
InternalError(c, err.Error())
return
}
OK(c, result)
}
2021-11-21 14:37:13 +00:00
func QueryTransactions(c *gin.Context) {
ledgerConfig := script.GetLedgerConfigFromContext(c)
2021-11-22 14:50:10 +00:00
queryParams := script.GetQueryParams(c)
// 倒序查询
queryParams.OrderBy = "date desc"
2021-11-24 09:32:24 +00:00
transactions := make([]Transaction, 0)
2021-11-22 08:47:49 +00:00
err := script.BQLQueryList(ledgerConfig, &queryParams, &transactions)
2021-11-21 14:37:13 +00:00
if err != nil {
InternalError(c, err.Error())
return
}
currencyMap := script.GetLedgerCurrencyMap(ledgerConfig.Id)
2021-11-22 08:47:49 +00:00
// 格式化金额
for i := 0; i < len(transactions); i++ {
_, ok := currencyMap[transactions[i].Currency]
if ok {
transactions[i].IsAnotherCurrency = transactions[i].Currency != ledgerConfig.OperatingCurrency
}
symbol := script.GetCommoditySymbol(ledgerConfig.Id, transactions[i].Currency)
2021-11-24 09:32:24 +00:00
transactions[i].CurrencySymbol = symbol
transactions[i].CostCurrencySymbol = symbol
if transactions[i].Price != "" {
transactions[i].Price = strings.Fields(transactions[i].Price)[0]
2021-11-22 08:47:49 +00:00
}
2022-01-09 05:48:18 +00:00
if transactions[i].Balance != "" {
transactions[i].Balance = strings.Fields(transactions[i].Balance)[0]
}
2021-11-22 08:47:49 +00:00
}
2021-11-21 14:37:13 +00:00
OK(c, transactions)
}
2024-10-24 15:26:07 +00:00
type TransactionForm struct {
ID string `form:"id" json:"id"`
Date string `form:"date" binding:"required" json:"date"`
Payee string `form:"payee" json:"payee,omitempty"`
Desc string `form:"desc" binding:"required" json:"desc"`
Narration string `form:"narration" json:"narration,omitempty"`
Tags []string `form:"tags" json:"tags,omitempty"`
DivideDateList []string `form:"divideDateList" json:"divideDateList,omitempty"`
Entries []TransactionEntryForm `form:"entries" json:"entries"`
2024-10-29 06:00:37 +00:00
RawText string `json:"rawText,omitempty"`
}
type UpdateRawTextTransactionForm struct {
ID string `form:"id" binding:"required" json:"id"`
RawText string `form:"rawText" json:"rawText,omitempty" binding:"required"`
2021-11-28 12:19:40 +00:00
}
2024-10-24 15:26:07 +00:00
type TransactionEntryForm struct {
2023-12-10 09:56:21 +00:00
Account string `form:"account" binding:"required" json:"account"`
Number decimal.Decimal `form:"number" json:"number,omitempty"`
Currency string `form:"currency" json:"currency"`
Price decimal.Decimal `form:"price" json:"price,omitempty"`
PriceCurrency string `form:"priceCurrency" json:"priceCurrency,omitempty"`
IsAnotherCurrency bool `form:"isAnotherCurrency" json:"isAnotherCurrency,omitempty"`
2021-11-28 12:19:40 +00:00
}
2024-10-24 15:26:07 +00:00
func sum(entries []TransactionEntryForm, openingBalances string) decimal.Decimal {
2021-11-28 12:19:40 +00:00
sumVal := decimal.NewFromInt(0)
for _, entry := range entries {
if entry.Account == openingBalances {
2021-11-29 14:21:25 +00:00
return decimal.NewFromInt(0)
2021-11-28 12:19:40 +00:00
}
2024-03-10 14:30:56 +00:00
pVal, _ := entry.Price.Float64()
if pVal == 0 {
2021-11-28 12:19:40 +00:00
sumVal = entry.Number.Add(sumVal)
} else {
sumVal = entry.Number.Mul(entry.Price).Add(sumVal)
}
}
return sumVal
}
2021-12-12 14:42:07 +00:00
func AddBatchTransactions(c *gin.Context) {
2024-10-24 15:26:07 +00:00
var addTransactionForms []TransactionForm
2021-12-12 14:42:07 +00:00
if err := c.ShouldBindJSON(&addTransactionForms); err != nil {
BadRequest(c, err.Error())
return
}
result := make([]string, 0)
ledgerConfig := script.GetLedgerConfigFromContext(c)
for _, form := range addTransactionForms {
err := saveTransaction(nil, form, ledgerConfig)
if err == nil {
result = append(result, form.Date+form.Payee+form.Desc)
} else {
script.LogError(ledgerConfig.Mail, err.Error())
}
}
OK(c, result)
}
2021-11-28 12:19:40 +00:00
func AddTransactions(c *gin.Context) {
2024-10-24 15:26:07 +00:00
var addTransactionForm TransactionForm
2021-11-28 12:19:40 +00:00
if err := c.ShouldBindJSON(&addTransactionForm); err != nil {
BadRequest(c, err.Error())
return
}
ledgerConfig := script.GetLedgerConfigFromContext(c)
2022-03-20 15:55:05 +00:00
// 判断是否分期
var err error
var divideCount = len(addTransactionForm.DivideDateList)
if divideCount <= 0 {
err = saveTransaction(c, addTransactionForm, ledgerConfig)
} else {
for idx, entry := range addTransactionForm.Entries {
// 保留 3 位小数
addTransactionForm.Entries[idx].Number = entry.Number.Div(decimal.NewFromInt(int64(divideCount))).Round(3)
}
for _, date := range addTransactionForm.DivideDateList {
addTransactionForm.Date = date
err = saveTransaction(c, addTransactionForm, ledgerConfig)
if err != nil {
break
}
}
}
2021-12-12 14:42:07 +00:00
if err != nil {
2022-03-20 15:55:05 +00:00
script.LogError(ledgerConfig.Mail, err.Error())
2021-12-12 14:42:07 +00:00
return
}
OK(c, nil)
}
2024-10-24 15:26:07 +00:00
func saveTransaction(c *gin.Context, addTransactionForm TransactionForm, ledgerConfig *script.Config) error {
2021-11-28 12:19:40 +00:00
// 账户是否平衡
sumVal := sum(addTransactionForm.Entries, ledgerConfig.OpeningBalances)
val, _ := decimal.NewFromString("0.1")
2021-11-28 12:19:40 +00:00
if sumVal.Abs().GreaterThan(val) {
2021-12-12 14:42:07 +00:00
if c != nil {
TransactionNotBalance(c)
}
return errors.New("transaction not balance")
2021-11-28 12:19:40 +00:00
}
// 2021-09-29 * "支付宝" "黄金补仓X元" #Invest
line := fmt.Sprintf("\r\n%s * \"%s\" \"%s\"", addTransactionForm.Date, addTransactionForm.Payee, addTransactionForm.Desc)
if len(addTransactionForm.Tags) > 0 {
for _, tag := range addTransactionForm.Tags {
line += "#" + tag + " "
}
}
2023-12-07 15:59:28 +00:00
currencyMap := script.GetLedgerCurrencyMap(ledgerConfig.Id)
2021-11-28 12:19:40 +00:00
var autoBalance bool
for _, entry := range addTransactionForm.Entries {
if entry.Account == ledgerConfig.OpeningBalances {
2022-01-09 06:22:28 +00:00
autoBalance = false
2021-11-28 12:19:40 +00:00
line += fmt.Sprintf("\r\n %s", entry.Account)
} else {
line += fmt.Sprintf("\r\n %s %s %s", entry.Account, entry.Number.Round(2).StringFixedBank(2), entry.Currency)
2021-11-28 12:19:40 +00:00
}
2023-12-05 08:21:12 +00:00
zero := decimal.NewFromInt(0)
2022-01-09 06:22:28 +00:00
// 判断是否涉及多币种的转换
if entry.Currency != ledgerConfig.OperatingCurrency && entry.Account != ledgerConfig.OpeningBalances {
2023-12-05 08:21:12 +00:00
// 汇率值小于等于0则不进行汇率转换
if entry.Price.LessThanOrEqual(zero) {
continue
}
2023-12-07 15:59:28 +00:00
currency, isCurrency := currencyMap[entry.Currency]
2024-03-04 14:51:25 +00:00
currencyPrice := entry.Price
if currencyPrice.Equal(zero) {
currencyPrice, _ = decimal.NewFromString(currency.Price)
}
2023-12-07 15:59:28 +00:00
// 货币跳过汇率转换
2023-12-10 14:52:24 +00:00
if !isCurrency {
2023-12-07 15:59:28 +00:00
// 根据 number 的正负来判断是买入还是卖出
if entry.Number.GreaterThan(zero) {
// {351.729 CNY, 2021-09-29}
line += fmt.Sprintf(" {%s %s, %s}", entry.Price, ledgerConfig.OperatingCurrency, addTransactionForm.Date)
} else {
// {} @ 359.019 CNY
line += fmt.Sprintf(" {} @ %s %s", entry.Price, ledgerConfig.OperatingCurrency)
}
2024-03-04 14:51:25 +00:00
} else {
// 外币种格式Assets:Fixed:三顿半咖啡 -1.00 SATURN_BIRD {5.61 CNY}
// fix issue #66 https://github.com/BaoXuebin/beancount-gs/issues/66
line += fmt.Sprintf(" {%s %s}", currencyPrice, ledgerConfig.OperatingCurrency)
2021-11-28 12:19:40 +00:00
}
2023-12-07 15:59:28 +00:00
priceLine := fmt.Sprintf("%s price %s %s %s", addTransactionForm.Date, entry.Currency, entry.Price, ledgerConfig.OperatingCurrency)
2021-11-28 12:19:40 +00:00
err := script.AppendFileInNewLine(script.GetLedgerPriceFilePath(ledgerConfig.DataPath), priceLine)
if err != nil {
2021-12-12 14:42:07 +00:00
if c != nil {
InternalError(c, err.Error())
}
return errors.New("internal error")
2021-11-28 12:19:40 +00:00
}
2023-12-10 14:52:24 +00:00
// 刷新币种汇率
if isCurrency {
err = script.LoadLedgerCurrencyMap(ledgerConfig)
if err != nil {
InternalError(c, err.Error())
return errors.New("internal error")
}
}
2021-11-28 12:19:40 +00:00
}
}
// 平衡小数点误差
if autoBalance {
line += "\r\n " + ledgerConfig.OpeningBalances
}
// 记账的日期
month, err := time.Parse("2006-01-02", addTransactionForm.Date)
if err != nil {
2021-12-12 14:42:07 +00:00
if c != nil {
InternalError(c, err.Error())
}
return errors.New("internal error")
2021-11-28 12:19:40 +00:00
}
2023-02-16 05:43:39 +00:00
// 交易的月份信息
monthStr := month.Format("2006-01")
err = CreateMonthBeanFileIfNotExist(ledgerConfig.DataPath, monthStr)
if err != nil {
if c != nil {
InternalError(c, err.Error())
2021-11-28 12:19:40 +00:00
}
2023-02-16 05:43:39 +00:00
return err
2021-11-28 12:19:40 +00:00
}
2024-10-24 15:26:07 +00:00
beanFilePath := script.GetLedgerMonthFilePath(ledgerConfig.DataPath, monthStr)
if addTransactionForm.ID != "" { // 更新交易
result, e := script.BQLPrint(ledgerConfig, addTransactionForm.ID)
if e != nil {
InternalError(c, e.Error())
return errors.New(e.Error())
}
// 使用 \r\t 分割多行文本片段,并清理每一行的空白
2024-10-29 07:45:13 +00:00
oldLines := filterEmptyStrings(strings.Split(result, "\n"))
2024-10-24 15:26:07 +00:00
startLine, endLine, e := script.FindConsecutiveMultilineTextInFile(beanFilePath, oldLines)
if e != nil {
InternalError(c, e.Error())
return errors.New(e.Error())
}
lines, e := script.RemoveLines(beanFilePath, startLine, endLine)
if e != nil {
InternalError(c, e.Error())
return errors.New(e.Error())
}
2024-10-29 07:45:13 +00:00
newLines := filterEmptyStrings(strings.Split(line, "\n"))
2024-10-24 15:26:07 +00:00
newLines = append(newLines, "")
lines, e = script.InsertLines(lines, startLine, newLines)
if e != nil {
InternalError(c, e.Error())
return errors.New(e.Error())
}
e = script.WriteToFile(beanFilePath, lines)
if e != nil {
InternalError(c, e.Error())
return errors.New(e.Error())
}
} else { // 新增交易
err = script.AppendFileInNewLine(beanFilePath, line)
}
2021-11-28 12:19:40 +00:00
if err != nil {
2021-12-12 14:42:07 +00:00
if c != nil {
InternalError(c, err.Error())
}
return errors.New("internal error")
2021-11-28 12:19:40 +00:00
}
2021-12-12 14:42:07 +00:00
return nil
2021-11-28 12:19:40 +00:00
}
2024-10-24 15:26:07 +00:00
// 过滤字符串数组中的空字符串
func filterEmptyStrings(arr []string) []string {
// 创建一个新切片来存储非空字符串
var result []string
for _, str := range arr {
2024-10-29 07:45:13 +00:00
if script.CleanString(str) != "" { // 检查字符串是否为空
2024-10-24 15:26:07 +00:00
result = append(result, str)
}
}
return result
}
2024-10-29 06:00:37 +00:00
func UpdateTransactionRawTextById(c *gin.Context) {
var rawTextUpdateTransactionForm UpdateRawTextTransactionForm
if err := c.ShouldBindJSON(&rawTextUpdateTransactionForm); err != nil {
BadRequest(c, err.Error())
2024-10-24 15:26:07 +00:00
return
}
ledgerConfig := script.GetLedgerConfigFromContext(c)
2024-10-29 06:00:37 +00:00
beanFilePath, err := getBeanFilePathByTransactionId(rawTextUpdateTransactionForm.ID, ledgerConfig)
2024-10-24 15:26:07 +00:00
if err != nil {
2024-10-29 06:00:37 +00:00
InternalError(c, err.Error())
2024-10-24 15:26:07 +00:00
return
}
2024-10-29 06:00:37 +00:00
result, e := script.BQLPrint(ledgerConfig, rawTextUpdateTransactionForm.ID)
if e != nil {
InternalError(c, e.Error())
2024-10-24 15:26:07 +00:00
return
}
2024-10-29 07:45:13 +00:00
oldLines := filterEmptyStrings(strings.Split(result, "\n"))
2024-10-29 06:00:37 +00:00
startLine, endLine, err := script.FindConsecutiveMultilineTextInFile(beanFilePath, oldLines)
2024-10-24 15:26:07 +00:00
if err != nil {
InternalError(c, err.Error())
return
}
2024-10-29 06:00:37 +00:00
lines, e := script.RemoveLines(beanFilePath, startLine, endLine)
if e != nil {
InternalError(c, e.Error())
return
}
2024-10-29 07:45:13 +00:00
newLines := filterEmptyStrings(strings.Split(rawTextUpdateTransactionForm.RawText, "\n"))
2024-10-29 06:00:37 +00:00
if len(newLines) > 0 {
lines, e = script.InsertLines(lines, startLine, newLines)
if e != nil {
InternalError(c, e.Error())
return
}
}
err = script.WriteToFile(beanFilePath, lines)
if err != nil {
InternalError(c, err.Error())
return
}
OK(c, true)
}
func DeleteTransactionById(c *gin.Context) {
queryParams := script.GetQueryParams(c)
if queryParams.ID == "" {
BadRequest(c, "Param 'id' must not be blank.")
return
}
ledgerConfig := script.GetLedgerConfigFromContext(c)
2024-10-24 15:26:07 +00:00
result, e := script.BQLPrint(ledgerConfig, queryParams.ID)
if e != nil {
InternalError(c, e.Error())
return
}
2024-10-29 06:00:37 +00:00
beanFilePath, err := getBeanFilePathByTransactionId(queryParams.ID, ledgerConfig)
if err != nil {
InternalError(c, err.Error())
return
}
2024-10-29 07:45:13 +00:00
oldLines := filterEmptyStrings(strings.Split(result, "\n"))
2024-10-24 15:26:07 +00:00
startLine, endLine, err := script.FindConsecutiveMultilineTextInFile(beanFilePath, oldLines)
if err != nil {
InternalError(c, err.Error())
return
}
lines, e := script.RemoveLines(beanFilePath, startLine, endLine)
if e != nil {
2024-10-29 06:00:37 +00:00
InternalError(c, e.Error())
2024-10-24 15:26:07 +00:00
return
}
err = script.WriteToFile(beanFilePath, lines)
if err != nil {
InternalError(c, err.Error())
return
}
OK(c, true)
}
2024-10-29 06:00:37 +00:00
func getBeanFilePathByTransactionId(transactionId string, ledgerConfig *script.Config) (string, error) {
queryParams := script.QueryParams{ID: transactionId, Where: true}
transactions := make([]Transaction, 0)
err := script.BQLQueryList(ledgerConfig, &queryParams, &transactions)
if err != nil {
return "", err
}
if len(transactions) == 0 {
return "", errors.New("no transaction found")
}
month, err := script.GetMonth(transactions[0].Date)
if err != nil {
return "", err
}
// 交易记录所在文件位置
beanFilePath := script.GetLedgerMonthFilePath(ledgerConfig.DataPath, month)
return beanFilePath, nil
}
2021-11-24 09:32:24 +00:00
type transactionPayee struct {
Value string `bql:"distinct payee" json:"value"`
}
2021-11-24 09:32:24 +00:00
func QueryTransactionPayees(c *gin.Context) {
ledgerConfig := script.GetLedgerConfigFromContext(c)
2021-11-24 09:32:24 +00:00
payeeList := make([]transactionPayee, 0)
queryParams := script.QueryParams{Where: false, OrderBy: "date desc", Limit: 100}
err := script.BQLQueryList(ledgerConfig, &queryParams, &payeeList)
if err != nil {
InternalError(c, err.Error())
return
}
result := make([]string, 0)
for _, payee := range payeeList {
if payee.Value != "" {
result = append(result, payee.Value)
}
}
OK(c, result)
}
2021-11-29 14:21:25 +00:00
type TransactionTemplate struct {
2024-10-24 15:26:07 +00:00
Id string `json:"id"`
Date string `form:"date" binding:"required" json:"date"`
TemplateName string `form:"templateName" binding:"required" json:"templateName"`
Payee string `form:"payee" json:"payee"`
Desc string `form:"desc" binding:"required" json:"desc"`
Entries []TransactionEntryForm `form:"entries" json:"entries"`
}
2021-11-29 14:21:25 +00:00
func QueryTransactionTemplates(c *gin.Context) {
ledgerConfig := script.GetLedgerConfigFromContext(c)
filePath := script.GetLedgerTransactionsTemplateFilePath(ledgerConfig.DataPath)
templates, err := getLedgerTransactionTemplates(filePath)
if err != nil {
InternalError(c, err.Error())
return
}
OK(c, templates)
}
2021-11-29 14:21:25 +00:00
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)
2021-11-29 14:21:25 +00:00
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) {
bytes, err := script.ReadFile(filePath)
if err != nil {
2021-11-29 14:21:25 +00:00
return nil, err
}
err = json.Unmarshal(bytes, &result)
if err != nil {
2021-11-29 14:21:25 +00:00
return nil, err
}
}
return result, nil
}
func writeLedgerTransactionTemplates(filePath string, templates []TransactionTemplate) error {
if !script.FileIfExist(filePath) {
err := script.CreateFile(filePath)
if err != nil {
return err
}
}
2021-11-29 14:21:25 +00:00
bytes, err := json.Marshal(templates)
if err != nil {
return err
}
err = script.WriteFile(filePath, string(bytes))
if err != nil {
return err
}
return nil
}