This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
/
team.go
150 lines (129 loc) · 3.35 KB
/
team.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package controller
import (
"strconv"
"github.com/dump-time/games-admin-server/log"
"github.com/dump-time/games-admin-server/model"
"github.com/dump-time/games-admin-server/services"
"github.com/dump-time/games-admin-server/util"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
type CreateTeamReq struct {
Name string `json:"name"`
Organization string `json:"organization"`
Code string `json:"code"`
}
type UpdateTeamReq struct {
Name string `json:"name"`
Organization string `json:"organization"`
Code string `json:"code"`
}
const (
createTeamErrorCode = 4401
listTeamErrorCode = 4402
deleteTeamErrorCode = 4403
getTeamInfoErrorCode = 4404
updateTeamErrorCode = 4405
)
func CreateTeamController(context *gin.Context) {
var req CreateTeamReq
if err := context.ShouldBindJSON(&req); err != nil {
log.Error(err)
util.ParamsErrResp(context)
return
}
if err := services.CreateTeam(&model.Team{
Name: req.Name,
Organization: req.Organization,
Code: req.Code,
}); err != nil {
log.Error(err)
util.FailedResp(context, createTeamErrorCode, "Create team error")
return
}
util.SuccessResp(context, nil)
}
func ListTeamsController(context *gin.Context) {
offsetRaw := context.DefaultQuery("offset", "0")
pageSizeRaw := context.DefaultQuery("page-size", "10")
offset, err := strconv.Atoi(offsetRaw)
if err != nil {
log.Error(err)
util.ParamsErrResp(context)
return
}
pageSize, err := strconv.Atoi(pageSizeRaw)
if err != nil {
log.Error(err)
util.ParamsErrResp(context)
return
}
teams, num, err := services.ListTeams(offset, pageSize)
if err != nil {
log.Error(err)
util.FailedResp(context, listTeamErrorCode, "List team error")
return
}
var teamData []gin.H
for _, team := range teams {
teamData = append(teamData, gin.H{
"id": team.ID,
"name": team.Name,
"organization": team.Organization,
"code": team.Code,
})
}
util.SuccessResp(context, gin.H{
"num": num,
"teams": teamData,
})
}
func DeleteTeamController(context *gin.Context) {
teamIDRaw := context.Param("teamID")
teamID, _ := strconv.Atoi(teamIDRaw)
if err := services.DeleteTeam(uint(teamID)); err != nil {
log.Error(err)
util.FailedResp(context, deleteTeamErrorCode, "Delete team error")
return
}
util.SuccessResp(context, nil)
}
func UpdateTeamController(context *gin.Context) {
teamIDRaw := context.Param("teamID")
teamID, _ := strconv.Atoi(teamIDRaw)
var req UpdateTeamReq
if err := context.ShouldBindJSON(&req); err != nil {
log.Error(err)
util.ParamsErrResp(context)
return
}
if err := services.UpdateTeam(&model.Team{
Model: gorm.Model{
ID: uint(teamID),
},
Name: req.Name,
Organization: req.Organization,
Code: req.Code,
}); err != nil {
log.Error(err)
util.FailedResp(context, updateTeamErrorCode, "Update team error")
return
}
util.SuccessResp(context, nil)
}
func GetTeamInfoController(context *gin.Context) {
teamIDRaw := context.Param("teamID")
teamID, _ := strconv.Atoi(teamIDRaw)
team, err := services.GetTeamInfo(uint(teamID))
if err != nil {
log.Error(err)
util.FailedResp(context, getTeamInfoErrorCode, "Get team info error")
return
}
util.SuccessResp(context, gin.H{
"id": team.ID,
"name": team.Name,
"organization": team.Organization,
"code": team.Code,
})
}