2023-12-05 09:57:48 +00:00
|
|
|
package service
|
|
|
|
|
|
2023-12-05 16:29:38 +00:00
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"github.com/beancount-gs/script"
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
2023-12-05 09:57:48 +00:00
|
|
|
|
|
|
|
|
type Event struct {
|
2023-12-05 16:29:38 +00:00
|
|
|
Date string `form:"date" binding:"required" json:"date"`
|
|
|
|
|
Type string `form:"type" binding:"required" json:"type"`
|
|
|
|
|
Description string `form:"description" binding:"required" json:"description"`
|
2023-12-05 09:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetAllEvents(c *gin.Context) {
|
2023-12-05 16:29:38 +00:00
|
|
|
ledgerConfig := script.GetLedgerConfigFromContext(c)
|
|
|
|
|
output := script.BeanReportAllEvents(ledgerConfig)
|
|
|
|
|
script.LogInfo(ledgerConfig.Mail, output)
|
|
|
|
|
|
|
|
|
|
events := make([]Event, 0)
|
|
|
|
|
lines := strings.Split(output, "\n")
|
|
|
|
|
// foreach lines
|
|
|
|
|
for idx, line := range lines {
|
|
|
|
|
if idx < 2 || idx > len(lines)-3 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if strings.Trim(line, " ") == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
// split line by " "
|
|
|
|
|
words := strings.Fields(line)
|
|
|
|
|
events = append(events, Event{
|
|
|
|
|
Date: words[0],
|
|
|
|
|
Type: words[1],
|
|
|
|
|
Description: words[2],
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
OK(c, events)
|
2023-12-05 09:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func AddEvent(c *gin.Context) {
|
2023-12-05 16:29:38 +00:00
|
|
|
var event Event
|
|
|
|
|
if err := c.ShouldBindJSON(&event); err != nil {
|
|
|
|
|
BadRequest(c, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ledgerConfig := script.GetLedgerConfigFromContext(c)
|
|
|
|
|
filePath := script.GetLedgerEventsFilePath(ledgerConfig.DataPath)
|
|
|
|
|
|
|
|
|
|
line := fmt.Sprintf("%s event \"%s\" \"%s\"", event.Date, event.Type, event.Description)
|
|
|
|
|
// 写入文件
|
|
|
|
|
err := script.AppendFileInNewLine(filePath, line)
|
|
|
|
|
if err != nil {
|
|
|
|
|
InternalError(c, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
OK(c, event)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func DeleteEvent(c *gin.Context) {
|
|
|
|
|
var event Event
|
|
|
|
|
if err := c.ShouldBindJSON(&event); err != nil {
|
|
|
|
|
BadRequest(c, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ledgerConfig := script.GetLedgerConfigFromContext(c)
|
|
|
|
|
filePath := script.GetLedgerEventsFilePath(ledgerConfig.DataPath)
|
|
|
|
|
|
|
|
|
|
line := fmt.Sprintf("%s event \"%s\" \"%s\"", event.Date, event.Type, event.Description)
|
|
|
|
|
err := script.DeleteLinesWithText(filePath, line)
|
|
|
|
|
if err != nil {
|
|
|
|
|
InternalError(c, err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-12-05 09:57:48 +00:00
|
|
|
OK(c, nil)
|
|
|
|
|
}
|