/
data.go
executable file
·106 lines (95 loc) · 2.52 KB
/
data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package handler
import (
"net/http"
"github.com/AlpherJang/mcache/pkg/cache"
"github.com/AlpherJang/mcache/pkg/common/errs"
"github.com/AlpherJang/mcache/pkg/model"
"github.com/gin-gonic/gin"
)
type DataStruct struct {
}
func NewDataStruct() *DataStruct {
return &DataStruct{}
}
func (d *DataStruct) list(ctx *gin.Context) {
tableName := ctx.Param("table")
table, err := cache.GetTable(tableName)
if err != nil {
ctx.AbortWithError(err.Code(), err.ToError())
return
}
cacheData := table.List()
ctx.JSON(http.StatusOK, gin.H{"data": cacheData})
}
func (d *DataStruct) get(ctx *gin.Context) {
tableName := ctx.Param("table")
key := ctx.Param("key")
table, err := cache.GetTable(tableName)
if err != nil {
ctx.AbortWithError(err.Code(), err.ToError())
return
}
cacheData, err := table.Get(key)
if err != nil {
ctx.AbortWithError(err.Code(), err.ToError())
return
}
ctx.JSON(http.StatusOK, gin.H{"data": cacheData})
}
func (d *DataStruct) add(ctx *gin.Context) {
tableName := ctx.Param("table")
table, err := cache.GetTable(tableName)
if err != nil {
ctx.AbortWithError(err.Code(), err.ToError())
return
}
var req model.AddCacheReq
if err := ctx.ShouldBindJSON(&req); err != nil {
ctx.AbortWithError(errs.ParamErr.Code(), errs.ParamErr.ToError())
return
}
if success, err := table.Add(req.Key, req.Value); !success {
ctx.AbortWithError(err.Code(), err.ToError())
return
}
ctx.JSON(http.StatusOK, nil)
}
func (d *DataStruct) update(ctx *gin.Context) {
var req model.UpdateCacheReq
if err := ctx.ShouldBindJSON(&req); err != nil {
ctx.AbortWithError(errs.ParamErr.Code(), errs.ParamErr.ToError())
return
}
table, err := cache.GetTable(req.Name)
if err != nil {
ctx.AbortWithError(err.Code(), err.ToError())
return
}
if err = table.Update(req.Key, req.Value); err != nil {
ctx.AbortWithError(err.Code(), err.ToError())
return
}
ctx.JSON(http.StatusOK, nil)
}
func (d *DataStruct) delete(ctx *gin.Context) {
tableName := ctx.Param("table")
key := ctx.Param("key")
table, err := cache.GetTable(tableName)
if err != nil {
ctx.AbortWithError(errs.TableNotFoundErr.Code(), errs.TableNotFoundErr.ToError())
return
}
if !table.Delete(key) {
ctx.AbortWithError(errs.CacheDeleteErr.Code(), errs.CacheDeleteErr.ToError())
return
}
ctx.JSON(http.StatusOK, nil)
}
func (d *DataStruct) Registry(r *gin.Engine) {
data := r.Group("data")
data.GET("/:table", d.list)
data.GET("/:table/:key", d.get)
data.PUT("/:table", d.add)
data.POST("/:table", d.update)
data.DELETE("/:table/:key", d.delete)
}