From d660b5ec00fa8f7e03e02c252fe3247fc3432852 Mon Sep 17 00:00:00 2001 From: jiayuqi7813 <63686458+jiayuqi7813@users.noreply.github.com> Date: Mon, 18 Jul 2022 16:34:09 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=A1=E7=90=86=E7=94=A8=E6=88=B7=20?= =?UTF-8?q?=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/admin/user.go | 99 ++++++++++++++++++++++++++++++++++++++++++++++ routers/router.go | 9 +++++ snctf.db | Bin 65536 -> 65536 bytes type/userTypes.go | 30 ++++++++++++++ 4 files changed, 138 insertions(+) create mode 100644 api/admin/user.go diff --git a/api/admin/user.go b/api/admin/user.go new file mode 100644 index 0000000..ca0644b --- /dev/null +++ b/api/admin/user.go @@ -0,0 +1,99 @@ +package admin + +import ( + "fmt" + "github.com/gin-gonic/gin" + "main.go/api" + db "main.go/database" + . "main.go/type" + "strconv" +) + +//GetAllUserInfo 获取所有用户 +func GetAllUserInfo(c *gin.Context) { + api.Link() + DB := db.DBsnctf + var resquest []AllInfoResponse + rows, err := DB.Debug().Table("user").Select([]string{"id", "username", "affiliation", "country", "website", "password", "email", "banned", "hidden", "role", "team_id"}).Rows() + if err != nil { + c.JSON(400, gin.H{"code": 400, "msg": "Get info error!"}) + return + } + for rows.Next() { + var info AllInfoResponse + err := rows.Scan(&info.Id, &info.Username, &info.Affiliation, &info.Country, &info.Website, &info.Password, &info.Email, &info.Banned, &info.Hidden, &info.Role, &info.TeamID) + if err != nil { + c.JSON(400, gin.H{"code": 400, "msg": "Get info error!"}) + return + } + resquest = append(resquest, info) + } + c.JSON(200, gin.H{"code": 200, "data": resquest}) +} + +// DelUser 删除用户 +func DelUser(c *gin.Context) { + id := c.Param("id") + uid, _ := strconv.Atoi(id) + api.Link() + DB := db.DBsnctf + err := DB.Debug().Table("user").Where("id = ?", uid).Delete(&User{}).Error + if err != nil { + c.JSON(400, gin.H{"code": 400, "msg": "Delete user error!"}) + return + } + c.JSON(200, gin.H{"code": 200, "msg": "Delete user success!"}) + +} + +// EditUser 更新用户信息 +func EditUser(c *gin.Context) { + var user EditInfoRequest + id := c.Param("id") + uid, _ := strconv.Atoi(id) + api.Link() + DB := db.DBsnctf + err := c.ShouldBindJSON(&user) + if err != nil { + fmt.Println(err) + c.JSON(400, gin.H{"code": 400, "msg": "Update user error!"}) + return + } + upuser := &EditInfoRequest{ + Username: user.Username, + Password: user.Password, + Email: user.Email, + Affiliation: user.Affiliation, + Country: user.Country, + Website: user.Website, + Banned: user.Banned, + Hidden: user.Hidden, + Role: user.Role, + TeamID: user.TeamID, + } + err = DB.Table("user").Where("id = ?", uid).Updates(upuser).Error + if err != nil { + fmt.Println(err) + c.JSON(400, gin.H{"code": 400, "msg": "Update user error!"}) + return + } + c.JSON(200, gin.H{"code": 200, "msg": "Update user success!"}) +} + +// AddUser 新增用户 +func AddUser(c *gin.Context) { + var user User + api.Link() + DB := db.DBsnctf + err := c.ShouldBindJSON(&user) + if err != nil { + c.JSON(400, gin.H{"code": 400, "msg": "Add user error!"}) + return + } + err = DB.Debug().Table("user").Create(&user).Error + if err != nil { + c.JSON(400, gin.H{"code": 400, "msg": "Add user error!"}) + return + } + c.JSON(200, gin.H{"code": 200, "msg": "Add user success!"}) +} diff --git a/routers/router.go b/routers/router.go index 89f02b1..59e1eb1 100644 --- a/routers/router.go +++ b/routers/router.go @@ -77,6 +77,15 @@ func Initrouter() { //删除公告 manager.DELETE("/notice/:id", api.DelNotice) + //显示数据库中所有用户信息 + manager.GET("/users", admin.GetAllUserInfo) + //删除用户信息 + manager.DELETE("/user/:id", admin.DelUser) + //修改用户信息 + manager.PUT("/user/:id", admin.EditUser) + //新增用户 + manager.POST("/user", admin.AddUser) + } err := router.Run(":9000") diff --git a/snctf.db b/snctf.db index 069a119d6b928d4b49c07baf9a4b5adb9779d15e..dcf0bf0289e1f13f8e1aa30bfc98d5cb8bce33f4 100644 GIT binary patch delta 80 zcmZo@U}Z>R%sLr|BQug>G0KwN66#xJL delta 46 zcmZo@U}83X;4$WsH0bzM{U0(t_$7n=NIJKLP+X CqYU2w diff --git a/type/userTypes.go b/type/userTypes.go index e1d8fa2..8c05a74 100644 --- a/type/userTypes.go +++ b/type/userTypes.go @@ -108,6 +108,36 @@ type PublicAllInfoResponse struct { Hidden string `json:"hidden"` } +// AllInfoResponse 定义返回所有用户公开信息结构体 +type AllInfoResponse struct { + Id string `json:"id"` + Username string `json:"username"` + Password string `json:"password"` + Email string `json:"email"` + Affiliation string `json:"affiliation"` + Country string `json:"country"` + Website string `json:"website"` + TeamID string `json:"team_id"` + Hidden string `json:"hidden"` + Banned string `json:"banned"` + Role string `json:"role"` +} + +// EditInfoRequest 定义修改用户信息的结构体 +type EditInfoRequest struct { + Id string `json:"id"` + Username string `json:"username"` + Password string `json:"password"` + Email string `json:"email"` + Affiliation string `json:"affiliation"` + Country string `json:"country"` + Website string `json:"website"` + TeamID int `json:"team_id"` + Hidden string `json:"hidden"` + Banned string `json:"banned"` + Role string `json:"role"` +} + // ChallengeResponse 定义获取题目的一个响应。 type ChallengeResponse struct { ID int `json:"id"`