Skip to content

Commit

Permalink
bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
zgwit committed Nov 4, 2024
1 parent b3a6df2 commit 17b3095
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
9 changes: 7 additions & 2 deletions api/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import "github.com/god-jason/bucket/table"
// 表相关接口只能放这里了,否则会import circle: api->table->api
func init() {

Register("GET", "table/list", table.ApiList)
Register("GET", "table/:table/reload", table.ApiReload)
//TODO 限管理员权限
Register("GET", "table/list", table.ApiTableList)
Register("POST", "table/:table", table.ApiTableCreate)
Register("POST", "table/:table/rename", table.ApiTableRename)
Register("GET", "table/:table/remove", table.ApiTableRemove)
Register("GET", "table/:table/reload", table.ApiTableReload)
Register("GET", "table/:table/conf/*conf", table.ApiConf)
Register("POST", "table/:table/conf/*conf", table.ApiConfUpdate)

//普通权限
Register("POST", "table/:table/count", table.ApiCount)
Register("POST", "table/:table/create", table.ApiCreate)
Register("POST", "table/:table/update/:id", table.ApiUpdate)
Expand Down
61 changes: 59 additions & 2 deletions table/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func Load(id string, strict bool) error {
return nil
}

func ApiList(ctx *gin.Context) {
func ApiTableList(ctx *gin.Context) {

var infos []Info
//&InfoEx{
Expand All @@ -204,7 +204,7 @@ func ApiList(ctx *gin.Context) {
OK(ctx, infos)
}

func ApiReload(ctx *gin.Context) {
func ApiTableReload(ctx *gin.Context) {
tab := ctx.Param("table")
err := Load(tab, true)
if err != nil {
Expand All @@ -214,3 +214,60 @@ func ApiReload(ctx *gin.Context) {

OK(ctx, nil)
}

type rename struct {
Name string `json:"name"`
}

func ApiTableRename(ctx *gin.Context) {
var r rename
err := ctx.ShouldBindJSON(&r)
if err != nil {
Error(ctx, err)
return
}

tab := ctx.Param("table")
old := filepath.Join(viper.GetString("data"), Path, tab)
name := filepath.Join(viper.GetString("data"), Path, r.Name)
err = os.Rename(old, name)
if err != nil {
Error(ctx, err)
return
}

//直接修改map,不雅
t := tables.LoadAndDelete(tab)
t.Id = r.Name
tables.Store(r.Name, t)

OK(ctx, nil)
}

func ApiTableRemove(ctx *gin.Context) {
tab := ctx.Param("table")
dir := filepath.Join(viper.GetString("data"), Path, tab)
err := os.RemoveAll(dir)
if err != nil {
Error(ctx, err)
return
}

OK(ctx, nil)
}

func ApiTableCreate(ctx *gin.Context) {
tab := ctx.Param("table")
dir := filepath.Join(viper.GetString("data"), Path, tab)
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
Error(ctx, err)
return
}

//TODO 空白的
_ = Load(tab, false)

OK(ctx, nil)

}
8 changes: 4 additions & 4 deletions table/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ const Path = "tables"

var tables lib.Map[Table]

func Get(name string) (*Table, error) {
table := tables.Load(name)
func Get(id string) (*Table, error) {
table := tables.Load(id)
if table == nil {
return nil, exception.New("没有表定义 " + name)
return nil, exception.New("没有表定义 " + id)
}
return table, nil
}

func Register(table *Table) {
tables.Store(table.Name, table)
tables.Store(table.Id, table)
}

func LoadAll() error {
Expand Down

0 comments on commit 17b3095

Please sign in to comment.