51 lines
1019 B
Go
51 lines
1019 B
Go
package user
|
|
|
|
import (
|
|
"nixcn-cms/data"
|
|
"nixcn-cms/internal/cryptography"
|
|
|
|
"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 {
|
|
c.JSON(403, gin.H{"status": "userid error"})
|
|
return
|
|
}
|
|
userId, err := uuid.Parse(userIdOrig.(string))
|
|
if err != nil {
|
|
c.JSON(500, gin.H{"status": "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 {
|
|
c.JSON(500, gin.H{"status": "failed to find user"})
|
|
return
|
|
}
|
|
|
|
userData.Avatar = ReqInfo.Avatar
|
|
userData.Email = ReqInfo.Email
|
|
userData.Nickname = ReqInfo.Nickname
|
|
userData.Subtitle = ReqInfo.Subtitle
|
|
|
|
if ReqInfo.Bio != "" {
|
|
if !cryptography.IsBase64Std(ReqInfo.Bio) {
|
|
c.JSON(400, gin.H{"status": "invalid base64"})
|
|
}
|
|
}
|
|
userData.Bio = ReqInfo.Bio
|
|
|
|
// Update user info
|
|
userData.UpdateByUserID(userId)
|
|
|
|
c.JSON(200, gin.H{"status": "success"})
|
|
}
|