160 lines
4.3 KiB
Go
160 lines
4.3 KiB
Go
package user
|
|
|
|
import (
|
|
"net/url"
|
|
"nixcn-cms/data"
|
|
"nixcn-cms/internal/cryptography"
|
|
"nixcn-cms/internal/exception"
|
|
"nixcn-cms/utils"
|
|
"unicode/utf8"
|
|
|
|
"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 {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.ErrorStatusUser).
|
|
SetService(exception.UserService).
|
|
SetEndpoint(exception.UserUpdateEndpoint).
|
|
SetType(exception.ErrorTypeCommon).
|
|
SetOriginal(exception.CommonErrorMissingUserId).
|
|
Build()
|
|
utils.HttpResponse(c, 403, errorCode)
|
|
return
|
|
}
|
|
userId, err := uuid.Parse(userIdOrig.(string))
|
|
if err != nil {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.ErrorStatusServer).
|
|
SetService(exception.UserService).
|
|
SetEndpoint(exception.UserUpdateEndpoint).
|
|
SetType(exception.ErrorTypeCommon).
|
|
SetOriginal(exception.CommonErrorUuidParseFailed).
|
|
Build()
|
|
utils.HttpResponse(c, 500, errorCode)
|
|
return
|
|
}
|
|
|
|
var ReqInfo data.User
|
|
err = c.ShouldBindJSON(&ReqInfo)
|
|
if err != nil {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.ErrorStatusClient).
|
|
SetService(exception.UserService).
|
|
SetEndpoint(exception.UserUpdateEndpoint).
|
|
SetType(exception.ErrorTypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
Build()
|
|
utils.HttpResponse(c, 400, errorCode)
|
|
return
|
|
}
|
|
|
|
// Get user info
|
|
userData, err := new(data.User).GetByUserId(userId)
|
|
if err != nil {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.ErrorStatusUser).
|
|
SetService(exception.UserService).
|
|
SetEndpoint(exception.UserUpdateEndpoint).
|
|
SetType(exception.ErrorTypeCommon).
|
|
SetOriginal(exception.CommonErrorUserNotFound).
|
|
Build()
|
|
utils.HttpResponse(c, 500, errorCode)
|
|
return
|
|
}
|
|
|
|
// if len(ReqInfo.Email) < 5 || len(ReqInfo.Email) >= 255 {
|
|
// utils.HttpResponse(c, 400, "", "invilad email")
|
|
// return
|
|
// }
|
|
// userData.Email = ReqInfo.Email
|
|
|
|
// utils.HttpResponse(c, 400, "", "invilad user name")
|
|
// return
|
|
|
|
if ReqInfo.Username != "" {
|
|
if len(ReqInfo.Username) < 5 || len(ReqInfo.Username) >= 255 {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.ErrorStatusClient).
|
|
SetService(exception.UserService).
|
|
SetEndpoint(exception.UserUpdateEndpoint).
|
|
SetType(exception.ErrorTypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
Build()
|
|
utils.HttpResponse(c, 400, errorCode)
|
|
return
|
|
}
|
|
userData.Username = ReqInfo.Username
|
|
}
|
|
|
|
if ReqInfo.Nickname != "" {
|
|
if utf8.RuneCountInString(ReqInfo.Nickname) > 24 {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.ErrorStatusClient).
|
|
SetService(exception.UserService).
|
|
SetEndpoint(exception.UserUpdateEndpoint).
|
|
SetType(exception.ErrorTypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
Build()
|
|
utils.HttpResponse(c, 400, errorCode)
|
|
return
|
|
}
|
|
userData.Nickname = ReqInfo.Nickname
|
|
}
|
|
|
|
if ReqInfo.Subtitle != "" {
|
|
if utf8.RuneCountInString(ReqInfo.Subtitle) > 32 {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.ErrorStatusClient).
|
|
SetService(exception.UserService).
|
|
SetEndpoint(exception.UserUpdateEndpoint).
|
|
SetType(exception.ErrorTypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
Build()
|
|
utils.HttpResponse(c, 400, errorCode)
|
|
return
|
|
}
|
|
userData.Subtitle = ReqInfo.Subtitle
|
|
}
|
|
|
|
if ReqInfo.Avatar != "" {
|
|
_, err := url.ParseRequestURI(ReqInfo.Avatar)
|
|
if err != nil {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.ErrorStatusClient).
|
|
SetService(exception.UserService).
|
|
SetEndpoint(exception.UserUpdateEndpoint).
|
|
SetType(exception.ErrorTypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
Build()
|
|
utils.HttpResponse(c, 400, errorCode)
|
|
return
|
|
}
|
|
userData.Avatar = ReqInfo.Avatar
|
|
}
|
|
|
|
if ReqInfo.Bio != "" {
|
|
if !cryptography.IsBase64Std(ReqInfo.Bio) {
|
|
errorCode := new(exception.Builder).
|
|
SetStatus(exception.ErrorStatusClient).
|
|
SetService(exception.UserService).
|
|
SetEndpoint(exception.UserUpdateEndpoint).
|
|
SetType(exception.ErrorTypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
Build()
|
|
utils.HttpResponse(c, 400, errorCode)
|
|
return
|
|
}
|
|
userData.Bio = ReqInfo.Bio
|
|
}
|
|
|
|
// Update user info
|
|
userData.UpdateByUserID(userId)
|
|
|
|
utils.HttpResponse(c, 200, "", "success")
|
|
}
|