63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
package user
|
|
|
|
import (
|
|
"nixcn-cms/data"
|
|
"nixcn-cms/internal/cryptography"
|
|
"nixcn-cms/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func Update(c *gin.Context) {
|
|
// New user model
|
|
userIdOrig, ok := c.Get("user_id")
|
|
if !ok {
|
|
utils.HttpResponse(c, 403, "", "userid error")
|
|
return
|
|
}
|
|
userId, err := uuid.Parse(userIdOrig.(string))
|
|
if err != nil {
|
|
utils.HttpResponse(c, 500, "", "failed to parse uuid")
|
|
return
|
|
}
|
|
|
|
var ReqInfo data.User
|
|
c.BindJSON(&ReqInfo)
|
|
|
|
// Get user info
|
|
userData, err := new(data.User).GetByUserId(userId)
|
|
if err != nil {
|
|
utils.HttpResponse(c, 500, "", "failed to find user")
|
|
return
|
|
}
|
|
|
|
if len(ReqInfo.Email) < 5 || len(ReqInfo.Email) >= 255 {
|
|
utils.HttpResponse(c, 400, "", "invilad email")
|
|
return
|
|
}
|
|
userData.Email = ReqInfo.Email
|
|
|
|
if len(ReqInfo.Username) < 5 || len(ReqInfo.Username) >= 255 {
|
|
utils.HttpResponse(c, 400, "", "invilad user name")
|
|
return
|
|
}
|
|
userData.Username = ReqInfo.Username
|
|
|
|
userData.Nickname = ReqInfo.Nickname
|
|
userData.Subtitle = ReqInfo.Subtitle
|
|
userData.Avatar = ReqInfo.Avatar
|
|
|
|
if ReqInfo.Bio != "" {
|
|
if !cryptography.IsBase64Std(ReqInfo.Bio) {
|
|
utils.HttpResponse(c, 400, "", "invalid base64")
|
|
}
|
|
}
|
|
userData.Bio = ReqInfo.Bio
|
|
|
|
// Update user info
|
|
userData.UpdateByUserID(userId)
|
|
|
|
utils.HttpResponse(c, 200, "", "success")
|
|
}
|