Files
cms-server/service/user/update.go
Noa Virellia e4329dfc2b refactor: standardize error handling with exception.Builder
- Replace hardcoded error messages with structured error codes using exception.Builder.
- Introduce new common error constants in exception/common.go (CommonErrorInvalidInput, CommonErrorUserNotFound, etc.).
- Update exception/specific.go with domain-specific errors and remove redundant ones.
- Apply consistent error handling across auth, event, user services and middleware.

Co-authored-by: Gemini <gemini@google.com>
Signed-off-by: Noa Virellia <noa@requiem.garden>
2026-01-21 12:47:49 +08:00

160 lines
4.3 KiB
Go

package user
import (
"net/url"
"nixcn-cms/data"
"nixcn-cms/exception"
"nixcn-cms/internal/cryptography"
"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")
}