Use utils.HttpResponse/Abort to replace c.JSON/Abort
Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
@@ -3,6 +3,7 @@ package user
|
||||
import (
|
||||
"nixcn-cms/data"
|
||||
"nixcn-cms/internal/cryptography"
|
||||
"nixcn-cms/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
@@ -12,12 +13,12 @@ func Update(c *gin.Context) {
|
||||
// New user model
|
||||
userIdOrig, ok := c.Get("user_id")
|
||||
if !ok {
|
||||
c.JSON(403, gin.H{"status": "userid error"})
|
||||
utils.HttpResponse(c, 403, "", "userid error")
|
||||
return
|
||||
}
|
||||
userId, err := uuid.Parse(userIdOrig.(string))
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"status": "failed to parse uuid"})
|
||||
utils.HttpResponse(c, 500, "", "failed to parse uuid")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -27,18 +28,29 @@ func Update(c *gin.Context) {
|
||||
// Get user info
|
||||
userData, err := new(data.User).GetByUserId(userId)
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"status": "failed to find user"})
|
||||
utils.HttpResponse(c, 500, "", "failed to find user")
|
||||
return
|
||||
}
|
||||
|
||||
userData.Avatar = ReqInfo.Avatar
|
||||
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) {
|
||||
c.JSON(400, gin.H{"status": "invalid base64"})
|
||||
utils.HttpResponse(c, 400, "", "invalid base64")
|
||||
}
|
||||
}
|
||||
userData.Bio = ReqInfo.Bio
|
||||
@@ -46,5 +58,5 @@ func Update(c *gin.Context) {
|
||||
// Update user info
|
||||
userData.UpdateByUserID(userId)
|
||||
|
||||
c.JSON(200, gin.H{"status": "success"})
|
||||
utils.HttpResponse(c, 200, "", "success")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user