-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.go
121 lines (113 loc) · 2.56 KB
/
tag.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package service
import (
"github.com/gin-gonic/gin"
log "github.com/mhchlib/logger"
"github.com/mhchlib/mconfig-admin/model"
"github.com/mhchlib/mconfig-admin/pkg/tools"
"strconv"
)
// BuildTagRequest ...
type BuildTagRequest struct {
Tag string `form:"tag" binding:"required"`
Desc string `form:"desc"`
Config int `form:"config" binding:"required"`
}
// BuildTag ...
func BuildTag(c *gin.Context) {
var param BuildTagRequest
err := c.Bind(¶m)
if err != nil {
tools.ResponseParamError(c)
return
}
config, err := model.GetConfig(param.Config)
if err != nil {
log.Error(err)
tools.ResponseDefaultFail(c, nil)
return
}
err = model.InsertTag(param.Tag, param.Desc, param.Config, config.Val, config.Schema)
if err != nil {
log.Error(err)
tools.ResponseDefaultFail(c, nil)
return
}
tools.ResponseDefaultSuccess(c, nil)
return
}
// ListTagRequest ...
type ListTagRequest struct {
Config int `form:"config" binding:"required"`
Filter string `form:"filter"`
Limit int `form:"limit"`
Offset int `form:"offset"`
}
// ListTagResponse ...
type ListTagResponse struct {
Id int `json:"id"`
Tag string `json:"tag"`
Desc string `json:"desc"`
CreateTime int64 `json:"create_time"`
UpdateTime int64 `json:"update_time"`
}
// ListTag ...
func ListTag(c *gin.Context) {
var param ListTagRequest
err := c.Bind(¶m)
if err != nil {
tools.ResponseParamError(c)
return
}
if param.Limit == 0 {
param.Limit = DEFAULT_LIST_LIMIT
}
tags, err := model.ListTags(param.Config, param.Filter, param.Limit, param.Offset)
data := make([]*ListTagResponse, 0)
for _, tag := range tags {
data = append(data, &ListTagResponse{
Id: tag.Id,
Tag: tag.Tag,
Desc: tag.Description,
CreateTime: tag.CreateTime,
UpdateTime: tag.UpdateTime,
})
}
if err != nil {
tools.ResponseDefaultFail(c, nil)
return
}
tools.ResponseDefaultSuccess(c, data)
return
}
// DeleteTag ...
func DeleteTag(c *gin.Context) {
id := c.Param("id")
log.Info(id)
atoi, err := strconv.Atoi(id)
if err != nil {
tools.ResponseParamError(c)
return
}
err = model.DeleteTag(atoi)
if err != nil {
tools.ResponseDefaultFail(c, "删除失败")
return
}
tools.ResponseDefaultSuccess(c, nil)
}
// GetTag ...
func GetTag(c *gin.Context) {
idStr := c.Param("id")
id, err := strconv.Atoi(idStr)
if err != nil {
tools.ResponseParamError(c)
return
}
item, err := model.GetTag(id)
if err != nil {
tools.ResponseDefaultFail(c, "获取tag信息失败")
return
}
tools.ResponseDefaultSuccess(c, item)
return
}