修复bug

This commit is contained in:
jiayuqi7813
2022-07-16 02:16:31 +08:00
parent e70bde7bd4
commit 0cd63a7111
4 changed files with 52 additions and 1 deletions

View File

@@ -35,6 +35,7 @@ func Login(c *gin.Context) {
//code 1用户名或密码错误
err := DB.Take(&user, "username = ?", request.Username).Error
if err != nil {
c.JSON(200, gin.H{
"Error": true,
@@ -193,7 +194,7 @@ func GetInfoByUserId(c *gin.Context) {
return
}
err := DB.Debug().Raw("SELECT username,affiliation,country,team_id FROM user WHERE id = ? LIMIT 1", id).Scan(&info).Error
err := DB.Debug().Raw("SELECT username,affiliation,country,team_id,website FROM user WHERE id = ? LIMIT 1", id).Scan(&info).Error
//err := DB.Where("id = ?", id).First(user).Error
if err != nil {
c.JSON(400, gin.H{"code": 400, "msg": "Get info error!"})
@@ -203,6 +204,44 @@ func GetInfoByUserId(c *gin.Context) {
}
// UpdateUserInfo 更新用户信息
func UpdateUserInfo(c *gin.Context) {
var user User
var request UpdateUserInfoRequest
if err := c.ShouldBindJSON(&request); err != nil {
c.JSON(400, gin.H{"code": 400, "msg": "Request format wrong!"})
return
}
Link()
DB := db.DBsnctf
session, err := Store.Get(c.Request, "SNCTFSESSID")
if err != nil {
c.JSON(200, gin.H{"code": 400, "msg": "Get SNCTFSESSID error"})
}
user, ok := session.Values["user"].(User)
if !ok {
c.JSON(200, gin.H{"code": 400, "msg": "No session"})
}
//获取用户id
userid := user.ID
//获取传入数据
username := request.Name
affiliation := request.Affiliation
country := request.Country
website := request.Website
email := request.Email
//数据库更新数据
err = DB.Model(&user).Where("id = ?", userid).Update("username", username).Update("affiliation", affiliation).Update("country", country).Update("website", website).Update("email", email).Error
if err != nil {
c.JSON(200, gin.H{"code": 400, "msg": "Update info error!"})
return
}
//更新session
session.Values["user"] = user
c.JSON(200, gin.H{"code": 200, "msg": "Update info success!"})
}
// GetAllUserInfo 获取所有用户信息
func GetAllUserInfo(c *gin.Context) {
var info PublicAllInfoResponse

View File

@@ -35,6 +35,8 @@ func Initrouter() {
{
// 获取当前用户信息
personal.GET("/session", api.Session)
// 修改当前用户的信息
personal.PUT("/userinfo", api.UpdateUserInfo)
// 获取题目分类
personal.GET("/category", api.GetCategories)
// 获取所有题目信息

BIN
snctf.db

Binary file not shown.

View File

@@ -94,6 +94,7 @@ type PublicInfoResponse struct {
Affiliation string `json:"affiliation"`
Country string `json:"country"`
TeamID int `json:"team_id"`
Website string `json:"website"`
}
// PublicAllInfoResponse 定义返回所有用户公开信息结构体 暂时不显示队伍名称
@@ -133,3 +134,12 @@ type StudentsOrOthersInfoResponse struct {
IDOrEmail string `json:"id_email"`
QQ string `json:"qq"`
}
// UpdateUserInfoResponse 定义更新用户信息的响应
type UpdateUserInfoRequest struct {
Name string `json:"name"`
Email string `json:"email"`
Country string `json:"country"`
Website string `json:"website"`
Affiliation string `json:"affiliation"`
}