-
Notifications
You must be signed in to change notification settings - Fork 14
/
group.go
98 lines (91 loc) · 2.17 KB
/
group.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
package handler
import (
"github.com/gin-gonic/gin"
"github.com/wq1019/cloud_disk/errors"
"github.com/wq1019/cloud_disk/model"
"github.com/wq1019/cloud_disk/service"
"net/http"
"strconv"
)
type groupHandler struct {
}
func (g *groupHandler) GroupCreate(c *gin.Context) {
l := struct {
Name string `json:"name" form:"name"`
MaxStorage uint64 `json:"max_storage" form:"max_storage"`
AllowShare bool `json:"allow_share" form:"allow_share"`
}{}
if err := c.ShouldBind(&l); err != nil {
_ = c.Error(errors.BindError(err))
return
}
group := model.Group{
Name: l.Name,
MaxStorage: l.MaxStorage,
AllowShare: l.AllowShare,
}
err := service.GroupCreate(c.Request.Context(), &group)
if err != nil {
_ = c.Error(err)
return
}
c.JSON(http.StatusCreated, group)
}
func (g *groupHandler) GroupUpdate(c *gin.Context) {
groupId, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
_ = c.Error(errors.BindError(err))
return
}
if groupId <= 0 {
_ = c.Error(model.ErrGroupNotExist)
return
}
l := struct {
Name string `json:"name" form:"name"`
MaxStorage uint64 `json:"max_storage" form:"max_storage"`
AllowShare bool `json:"allow_share" form:"allow_share"`
}{}
if err := c.ShouldBind(&l); err != nil {
_ = c.Error(errors.BindError(err))
return
}
err = service.GroupUpdate(c.Request.Context(), groupId, map[string]interface{}{
"name": l.Name,
"max_storage": l.MaxStorage,
"allow_share": l.AllowShare,
})
if err != nil {
_ = c.Error(err)
return
}
c.Status(http.StatusCreated)
}
func (g *groupHandler) GroupList(c *gin.Context) {
limit, offset := getInt64LimitAndOffset(c)
groups, count, err := service.GroupList(c.Request.Context(), offset, limit)
if err != nil {
_ = c.Error(err)
return
}
c.JSON(200, gin.H{
"count": count,
"data": groups,
})
}
func (g *groupHandler) GroupDelete(c *gin.Context) {
groupId, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
_ = c.Error(errors.BindError(err))
return
}
err = service.GroupDelete(c.Request.Context(), groupId)
if err != nil {
_ = c.Error(err)
return
}
c.Status(204)
}
func NewGroupHandler() *groupHandler {
return &groupHandler{}
}