-
Notifications
You must be signed in to change notification settings - Fork 14
/
user.go
96 lines (89 loc) · 2.52 KB
/
user.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
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"
"github.com/wq1019/go-image_uploader/image_url"
"strconv"
)
type userHandler struct {
imageUrl image_url.URL
}
func (*userHandler) UpdateBanStatus(c *gin.Context) {
userId, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
_ = c.Error(errors.BindError(err))
return
}
if userId <= 0 {
_ = c.Error(errors.ErrAccountNotFound())
return
}
l := struct {
IsBan bool `json:"is_ban" form:"is_ban"`
}{}
if err := c.ShouldBind(&l); err != nil {
_ = c.Error(errors.BindError(err))
return
}
user, err := service.UserLoad(c.Request.Context(), userId)
if user.IsAdmin == true {
_ = c.Error(errors.UserNotAllowBeBan("管理员账号不允许被 ban"))
return
}
err = service.UserUpdateBanStatus(c.Request.Context(), userId, l.IsBan)
if err != nil {
_ = c.Error(err)
return
}
c.Status(204)
}
func (u *userHandler) UserList(c *gin.Context) {
limit, offset := getInt64LimitAndOffset(c)
users, count, err := service.UserList(c.Request.Context(), offset, limit)
if err != nil {
_ = c.Error(err)
return
}
c.JSON(200, gin.H{
"count": count,
"data": convert2UserListResp(users, u.imageUrl),
})
}
func convert2UserListResp(users []*model.User, imageUrl image_url.URL) []map[string]interface{} {
userList := make([]map[string]interface{}, 0, len(users))
for _, v := range users {
userList = append(userList, convert2UserResp(v, imageUrl))
}
return userList
}
func convert2UserResp(user *model.User, imageUrl image_url.URL) map[string]interface{} {
var gender string
if user.Gender {
gender = "男"
} else {
gender = "女"
}
return map[string]interface{}{
"id": user.Id,
"name": user.Name,
"email": user.Email,
"gender": gender,
"profile": user.Profile,
"nickname": user.Nickname,
"created_at": user.CreatedAt,
"updated_at": user.UpdatedAt,
"avatar_url": imageUrl.Generate(user.AvatarHash),
"group_name": user.Group.Name,
"avatar_hash": user.AvatarHash,
"used_storage": user.UsedStorage,
"is_allow_share": user.Group.AllowShare,
"max_allow_storage": user.Group.MaxStorage,
//"used_storage": bytesize.ByteSize(user.UsedStorage),
//"max_allow_storage": bytesize.ByteSize(user.Group.MaxStorage),
}
}
func NewUserHandler(imageUrl image_url.URL) *userHandler {
return &userHandler{imageUrl: imageUrl}
}