管理用户 路由
This commit is contained in:
99
api/admin/user.go
Normal file
99
api/admin/user.go
Normal file
@@ -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!"})
|
||||||
|
}
|
@@ -77,6 +77,15 @@ func Initrouter() {
|
|||||||
//删除公告
|
//删除公告
|
||||||
manager.DELETE("/notice/:id", api.DelNotice)
|
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")
|
err := router.Run(":9000")
|
||||||
|
@@ -108,6 +108,36 @@ type PublicAllInfoResponse struct {
|
|||||||
Hidden string `json:"hidden"`
|
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 定义获取题目的一个响应。
|
// ChallengeResponse 定义获取题目的一个响应。
|
||||||
type ChallengeResponse struct {
|
type ChallengeResponse struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
|
Reference in New Issue
Block a user