add: delete ledger

This commit is contained in:
BaoXuebin 2022-03-11 23:18:38 +08:00
parent 9a47d9ecd2
commit f0c2b58061
3 changed files with 42 additions and 6 deletions

View File

@ -3,10 +3,11 @@ package script
import (
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"os"
"sort"
"strings"
"github.com/gin-gonic/gin"
)
var serverSecret string
@ -155,6 +156,10 @@ func UpdateLedgerAccounts(ledgerId string, accounts []Account) {
ledgerAccountsMap[ledgerId] = accounts
}
func ClearLedgerAccounts(ledgerId string) {
delete(ledgerAccountsMap, ledgerId)
}
func GetLedgerAccountTypes(ledgerId string) map[string]string {
return ledgerAccountTypesMap[ledgerId]
}
@ -163,6 +168,10 @@ func UpdateLedgerAccountTypes(ledgerId string, accountTypesMap map[string]string
ledgerAccountTypesMap[ledgerId] = accountTypesMap
}
func ClearLedgerAccountTypes(ledgerId string) {
delete(ledgerAccountTypesMap, ledgerId)
}
func GetAccountType(ledgerId string, acc string) AccountType {
accountTypes := ledgerAccountTypesMap[ledgerId]
accNodes := strings.Split(acc, ":")

View File

@ -3,12 +3,13 @@ package main
import (
"flag"
"fmt"
"github.com/beancount-gs/script"
"github.com/beancount-gs/service"
"github.com/gin-gonic/gin"
"io"
"net/http"
"os"
"github.com/beancount-gs/script"
"github.com/beancount-gs/service"
"github.com/gin-gonic/gin"
)
func InitServerFiles() error {
@ -88,6 +89,7 @@ func RegisterRouter(router *gin.Engine) {
authorized.POST("/file", service.UpdateLedgerSourceFileContent)
authorized.POST("/import/alipay", service.ImportAliPayCSV)
authorized.POST("/import/wx", service.ImportWxPayCSV)
authorized.DELETE("/ledger", service.DeleteLedger)
}
}

View File

@ -3,14 +3,16 @@ package service
import (
"crypto/sha1"
"encoding/hex"
"github.com/beancount-gs/script"
"github.com/gin-gonic/gin"
"io"
"io/ioutil"
"os"
"os/exec"
"sort"
"strings"
"time"
"github.com/beancount-gs/script"
"github.com/gin-gonic/gin"
)
func CheckBeancount(c *gin.Context) {
@ -178,6 +180,29 @@ func OpenOrCreateLedger(c *gin.Context) {
OK(c, resultMap)
}
// 删除账本
func DeleteLedger(c *gin.Context) {
ledgerConfig := script.GetLedgerConfigFromContext(c)
// 删除账本源文件
os.RemoveAll(ledgerConfig.DataPath)
script.LogInfo(ledgerConfig.Mail, "Success delete "+ledgerConfig.DataPath)
// 删除
ledgerConfigMap := script.GetLedgerConfigMap()
delete(ledgerConfigMap, ledgerConfig.Id)
err := script.WriteLedgerConfigMap(ledgerConfigMap)
if err != nil {
InternalError(c, "Failed to update ledger_config.json")
return
}
// remove from account cache
script.ClearLedgerAccounts(ledgerConfig.Id)
script.LogInfo(ledgerConfig.Mail, "Success clear ledger account cache "+ledgerConfig.Id)
// remove from account types cache
script.ClearLedgerAccountTypes(ledgerConfig.Id)
script.LogInfo(ledgerConfig.Mail, "Success clear ledger account types cache "+ledgerConfig.Id)
OK(c, "OK")
}
func createNewLedger(loginForm LoginForm, ledgerId string) (*script.Config, error) {
// create new ledger
serverConfig := script.GetServerConfig()