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

View File

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

View File

@ -3,14 +3,16 @@ package service
import ( import (
"crypto/sha1" "crypto/sha1"
"encoding/hex" "encoding/hex"
"github.com/beancount-gs/script"
"github.com/gin-gonic/gin"
"io" "io"
"io/ioutil" "io/ioutil"
"os"
"os/exec" "os/exec"
"sort" "sort"
"strings" "strings"
"time" "time"
"github.com/beancount-gs/script"
"github.com/gin-gonic/gin"
) )
func CheckBeancount(c *gin.Context) { func CheckBeancount(c *gin.Context) {
@ -178,6 +180,29 @@ func OpenOrCreateLedger(c *gin.Context) {
OK(c, resultMap) 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) { func createNewLedger(loginForm LoginForm, ledgerId string) (*script.Config, error) {
// create new ledger // create new ledger
serverConfig := script.GetServerConfig() serverConfig := script.GetServerConfig()