103 lines
2.4 KiB
Go
103 lines
2.4 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/beancount-gs/script"
|
|
"github.com/gin-gonic/gin"
|
|
"strings"
|
|
)
|
|
|
|
type Event struct {
|
|
Date string `form:"date" binding:"required" json:"date"`
|
|
Stage string `form:"stage" json:"stage"`
|
|
Type string `form:"type" json:"type"`
|
|
Types []string `form:"types" json:"types"`
|
|
Description string `form:"description" binding:"required" json:"description"`
|
|
}
|
|
|
|
func GetAllEvents(c *gin.Context) {
|
|
ledgerConfig := script.GetLedgerConfigFromContext(c)
|
|
|
|
beanFilePath := script.GetLedgerEventsFilePath(ledgerConfig.DataPath)
|
|
bytes, err := script.ReadFile(beanFilePath)
|
|
if err != nil {
|
|
InternalError(c, err.Error())
|
|
return
|
|
}
|
|
lines := strings.Split(string(bytes), "\n")
|
|
events := make([]Event, 0)
|
|
// foreach lines
|
|
for _, line := range lines {
|
|
if strings.Trim(line, " ") == "" {
|
|
continue
|
|
}
|
|
// split line by " "
|
|
words := strings.Fields(line)
|
|
if words[1] != "event" {
|
|
continue
|
|
}
|
|
events = append(events, Event{
|
|
Date: words[0],
|
|
Type: strings.ReplaceAll(words[2], "\"", ""),
|
|
Description: strings.ReplaceAll(words[3], "\"", ""),
|
|
})
|
|
}
|
|
OK(c, events)
|
|
}
|
|
|
|
func AddEvent(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)
|
|
|
|
if event.Type != "" {
|
|
event.Types = []string{event.Type}
|
|
}
|
|
|
|
// 定义Event类型的数组
|
|
events := make([]Event, 0)
|
|
|
|
if event.Types != nil {
|
|
for _, t := range event.Types {
|
|
events = append(events, Event{
|
|
Date: event.Date,
|
|
Type: t,
|
|
Description: event.Description,
|
|
})
|
|
line := fmt.Sprintf("%s event \"%s\" \"%s\"", event.Date, t, event.Description)
|
|
// 写入文件
|
|
err := script.AppendFileInNewLine(filePath, line)
|
|
if err != nil {
|
|
InternalError(c, err.Error())
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
OK(c, events)
|
|
}
|
|
|
|
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
|
|
}
|
|
OK(c, nil)
|
|
}
|