管理用户 路由
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!"})
|
||||
}
|
Reference in New Issue
Block a user