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.StatusUser). SetService(exception.ServiceUser). SetEndpoint(exception.EndpointUserServiceUpdate). SetType(exception.TypeCommon). SetOriginal(exception.CommonErrorMissingUserId). Build(c) utils.HttpResponse(c, 403, errorCode) return } userId, err := uuid.Parse(userIdOrig.(string)) if err != nil { errorCode := new(exception.Builder). SetStatus(exception.StatusServer). SetService(exception.ServiceUser). SetEndpoint(exception.EndpointUserServiceUpdate). SetType(exception.TypeCommon). SetOriginal(exception.CommonErrorUuidParseFailed). SetError(err). Build(c) utils.HttpResponse(c, 500, errorCode) return } var ReqInfo data.User err = c.ShouldBindJSON(&ReqInfo) if err != nil { errorCode := new(exception.Builder). SetStatus(exception.StatusUser). SetService(exception.ServiceUser). SetEndpoint(exception.EndpointUserServiceUpdate). SetType(exception.TypeCommon). SetOriginal(exception.CommonErrorInvalidInput). SetError(err). Build(c) utils.HttpResponse(c, 400, errorCode) return } // Get user info userData, err := new(data.User).GetByUserId(c, userId) if err != nil { errorCode := new(exception.Builder). SetStatus(exception.StatusUser). SetService(exception.ServiceUser). SetEndpoint(exception.EndpointUserServiceUpdate). SetType(exception.TypeCommon). SetOriginal(exception.CommonErrorUserNotFound). SetError(err). Build(c) 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.StatusUser). SetService(exception.ServiceUser). SetEndpoint(exception.EndpointUserServiceUpdate). SetType(exception.TypeCommon). SetOriginal(exception.CommonErrorInvalidInput). Build(c) 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.StatusUser). SetService(exception.ServiceUser). SetEndpoint(exception.EndpointUserServiceUpdate). SetType(exception.TypeCommon). SetOriginal(exception.CommonErrorInvalidInput). Build(c) 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.StatusUser). SetService(exception.ServiceUser). SetEndpoint(exception.EndpointUserServiceUpdate). SetType(exception.TypeCommon). SetOriginal(exception.CommonErrorInvalidInput). Build(c) 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.StatusUser). SetService(exception.ServiceUser). SetEndpoint(exception.EndpointUserServiceUpdate). SetType(exception.TypeCommon). SetOriginal(exception.CommonErrorInvalidInput). SetError(err). Build(c) utils.HttpResponse(c, 400, errorCode) return } userData.Avatar = ReqInfo.Avatar } if ReqInfo.Bio != "" { if !cryptography.IsBase64Std(ReqInfo.Bio) { errorCode := new(exception.Builder). SetStatus(exception.StatusUser). SetService(exception.ServiceUser). SetEndpoint(exception.EndpointUserServiceUpdate). SetType(exception.TypeCommon). SetOriginal(exception.CommonErrorInvalidInput). Build(c) utils.HttpResponse(c, 400, errorCode) return } userData.Bio = ReqInfo.Bio } // Update user info userData.UpdateByUserID(c, userId) errorCode := new(exception.Builder). SetStatus(exception.StatusUser). SetService(exception.ServiceUser). SetEndpoint(exception.EndpointUserServiceUpdate). SetType(exception.TypeCommon). SetOriginal(exception.CommonSuccess). Build(c) utils.HttpResponse(c, 200, errorCode) }