Skip to content

Commit

Permalink
update transaction by raw text
Browse files Browse the repository at this point in the history
  • Loading branch information
BaoXuebin committed Oct 29, 2024
1 parent 3e0766d commit 1d55b9e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 24 deletions.
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,8 @@ dataPath=自定义的目录

[MIT](https://github.com/BaoXuebin/beancount-gs/blob/main/License) @BaoXuebin

## 赞助
## 感谢️

[赞助地址](https://xdbin.com/sponsor)

## 感谢

<a href="https://jb.gg/OpenSourceSupport" style="display: flex; justify-content: left; align-items: center; flex-direction: row;">
<img alt="JetBrains" src="./jb_beam.png" style="width: 120px; height: 120px;" />
<h3>Licenses for Open Source Development</h3>
</a>
感谢 **@Cabin**,**@潇** 两位朋友的赞助支持❤️
5 changes: 3 additions & 2 deletions script/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ func FindConsecutiveMultilineTextInFile(filePath string, multilineLines []string

// cleanString 去除字符串中的首尾空白和中间的所有空格字符
func cleanString(str string) string {
return strings.ReplaceAll(strings.TrimSpace(str), " ", "")
all := strings.ReplaceAll(strings.TrimSpace(str), " ", "")
// 去除逗号,处理金额千分位
return strings.ReplaceAll(all, ",", "")
}

// 删除指定行范围的内容
Expand Down Expand Up @@ -303,7 +305,6 @@ func InsertLines(lines []string, startLineNo int, newLines []string) ([]string,
if startLineNo < 1 || startLineNo > len(lines)+1 {
return nil, fmt.Errorf("插入行号无效")
}

// 在指定位置插入新的内容
modifiedLines := append(lines[:startLineNo-1], append(newLines, lines[startLineNo-1:]...)...)
return modifiedLines, nil
Expand Down
1 change: 1 addition & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func RegisterRouter(router *gin.Engine) {
authorized.GET("/transaction/raw", service.QueryTransactionRawTextById)
authorized.GET("/transaction", service.QueryTransactions)
authorized.POST("/transaction", service.AddTransactions)
authorized.POST("/transaction/raw", service.UpdateTransactionRawTextById)
authorized.DELETE("/transaction", service.DeleteTransactionById)
authorized.POST("/transaction/batch", service.AddBatchTransactions)
authorized.GET("/transaction/payee", service.QueryTransactionPayees)
Expand Down
88 changes: 73 additions & 15 deletions service/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,12 @@ type TransactionForm struct {
Tags []string `form:"tags" json:"tags,omitempty"`
DivideDateList []string `form:"divideDateList" json:"divideDateList,omitempty"`
Entries []TransactionEntryForm `form:"entries" json:"entries"`
Raw RawTransaction `json:"raw,omitempty"`
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"`
}

type TransactionEntryForm struct {
Expand Down Expand Up @@ -391,38 +396,73 @@ func filterEmptyStrings(arr []string) []string {
return result
}

func DeleteTransactionById(c *gin.Context) {
queryParams := script.GetQueryParams(c)
if queryParams.ID == "" {
BadRequest(c, "Param 'id' must not be blank.")
func UpdateTransactionRawTextById(c *gin.Context) {
var rawTextUpdateTransactionForm UpdateRawTextTransactionForm
if err := c.ShouldBindJSON(&rawTextUpdateTransactionForm); err != nil {
BadRequest(c, err.Error())
return
}
ledgerConfig := script.GetLedgerConfigFromContext(c)
transactions := make([]Transaction, 0)
err := script.BQLQueryList(ledgerConfig, &queryParams, &transactions)

beanFilePath, err := getBeanFilePathByTransactionId(rawTextUpdateTransactionForm.ID, ledgerConfig)
if err != nil {
BadRequest(c, err.Error())
InternalError(c, err.Error())
return
}

if len(transactions) == 0 {
InternalError(c, "No transaction found.")
result, e := script.BQLPrint(ledgerConfig, rawTextUpdateTransactionForm.ID)
if e != nil {
InternalError(c, e.Error())
return
}

month, err := script.GetMonth(transactions[0].Date)
oldLines := filterEmptyStrings(strings.Split(result, "\r\n"))
startLine, endLine, err := script.FindConsecutiveMultilineTextInFile(beanFilePath, oldLines)
if err != nil {
InternalError(c, err.Error())
return
}
// 交易记录所在文件位置
beanFilePath := script.GetLedgerMonthFilePath(ledgerConfig.DataPath, month)
lines, e := script.RemoveLines(beanFilePath, startLine, endLine)
if e != nil {
InternalError(c, e.Error())
return
}
newLines := filterEmptyStrings(strings.Split(rawTextUpdateTransactionForm.RawText, "\r\n"))
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)

result, e := script.BQLPrint(ledgerConfig, queryParams.ID)
if e != nil {
InternalError(c, e.Error())
return
}

beanFilePath, err := getBeanFilePathByTransactionId(queryParams.ID, ledgerConfig)
if err != nil {
InternalError(c, err.Error())
return
}

oldLines := filterEmptyStrings(strings.Split(result, "\r\n"))
startLine, endLine, err := script.FindConsecutiveMultilineTextInFile(beanFilePath, oldLines)
if err != nil {
Expand All @@ -431,18 +471,36 @@ func DeleteTransactionById(c *gin.Context) {
}
lines, e := script.RemoveLines(beanFilePath, startLine, endLine)
if e != nil {
InternalError(c, err.Error())
InternalError(c, e.Error())
return
}
err = script.WriteToFile(beanFilePath, lines)
if err != nil {
InternalError(c, err.Error())
return
}

OK(c, true)
}

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
}

type transactionPayee struct {
Value string `bql:"distinct payee" json:"value"`
}
Expand Down

0 comments on commit 1d55b9e

Please sign in to comment.