253 lines
5.8 KiB
Go
253 lines
5.8 KiB
Go
package service_user
|
|
|
|
import (
|
|
"net/url"
|
|
"nixcn-cms/data"
|
|
"nixcn-cms/internal/cryptography"
|
|
"nixcn-cms/internal/exception"
|
|
"nixcn-cms/service/shared"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
func (self *UserServiceImpl) UpdateUserInfo(payload *UserInfoPayload) (result *UserInfoResult) {
|
|
var err error
|
|
|
|
updates := make(map[string]any)
|
|
|
|
if payload.Data.Username != nil {
|
|
val := *payload.Data.Username
|
|
if val != "" {
|
|
if len(*payload.Data.Username) < 5 || len(*payload.Data.Username) >= 255 {
|
|
execption := new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.ServiceUser).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput)
|
|
|
|
result = &UserInfoResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 400,
|
|
Exception: execption,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
}
|
|
updates["username"] = *payload.Data.Username
|
|
}
|
|
|
|
if payload.Data.Nickname != nil {
|
|
val := *payload.Data.Nickname
|
|
if val == "" {
|
|
execption := new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.ServiceUser).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput)
|
|
|
|
result = &UserInfoResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 400,
|
|
Exception: execption,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
if utf8.RuneCountInString(val) > 24 {
|
|
execption := new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.ServiceUser).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput)
|
|
|
|
result = &UserInfoResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 400,
|
|
Exception: execption,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
updates["nickname"] = *payload.Data.Nickname
|
|
}
|
|
|
|
if payload.Data.Subtitle != nil {
|
|
if utf8.RuneCountInString(*payload.Data.Subtitle) > 32 {
|
|
execption := new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.ServiceUser).
|
|
SetEndpoint(exception.EndpointUserServiceUpdate).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
SetError(nil).
|
|
Throw(payload.Context)
|
|
|
|
result = &UserInfoResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 400,
|
|
Exception: execption,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
updates["subtitle"] = *payload.Data.Subtitle
|
|
}
|
|
|
|
if payload.Data.Avatar != nil {
|
|
val := *payload.Data.Avatar
|
|
if val != "" {
|
|
_, err := url.ParseRequestURI(*payload.Data.Avatar)
|
|
if err != nil {
|
|
execption := new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.ServiceUser).
|
|
SetEndpoint(exception.EndpointUserServiceUpdate).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
SetError(err).
|
|
Throw(payload.Context)
|
|
|
|
result = &UserInfoResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 400,
|
|
Exception: execption,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
}
|
|
updates["avatar"] = *payload.Data.Avatar
|
|
}
|
|
|
|
if payload.Data.Bio != nil {
|
|
val := *payload.Data.Bio
|
|
if val != "" {
|
|
if !cryptography.IsBase64Std(*payload.Data.Bio) {
|
|
execption := new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.ServiceUser).
|
|
SetEndpoint(exception.EndpointUserServiceUpdate).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
SetError(nil).
|
|
Throw(payload.Context)
|
|
|
|
result = &UserInfoResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 400,
|
|
Exception: execption,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
}
|
|
updates["bio"] = *payload.Data.Bio
|
|
}
|
|
|
|
if payload.Data.AllowPublic != nil {
|
|
updates["allow_public"] = *payload.Data.AllowPublic
|
|
}
|
|
|
|
if len(updates) == 0 {
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.ServiceUser).
|
|
SetEndpoint(exception.EndpointUserServiceUpdate).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonSuccess).
|
|
SetError(nil).
|
|
Throw(payload.Context)
|
|
|
|
result = &UserInfoResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 200,
|
|
Exception: exception,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
userData := new(data.User)
|
|
|
|
userInfo, err := userData.GetByUserId(payload.Context, &payload.UserId)
|
|
if err != nil {
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusServer).
|
|
SetService(exception.ServiceUser).
|
|
SetEndpoint(exception.EndpointUserServiceUpdate).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorDatabase).
|
|
SetError(err).
|
|
Throw(payload.Context)
|
|
|
|
result = &UserInfoResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 500,
|
|
Exception: exception,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
if payload.Data.PermissionLevel != nil && userInfo.PermissionLevel >= 50 {
|
|
updates["permission_level"] = *payload.Data.PermissionLevel
|
|
}
|
|
|
|
err = userData.UpdateByUserID(payload.Context, &payload.UserId, updates)
|
|
if err != nil {
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusServer).
|
|
SetService(exception.ServiceUser).
|
|
SetEndpoint(exception.EndpointUserServiceUpdate).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorDatabase).
|
|
SetError(err).
|
|
Throw(payload.Context)
|
|
|
|
result = &UserInfoResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 500,
|
|
Exception: exception,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.ServiceUser).
|
|
SetEndpoint(exception.EndpointUserServiceUpdate).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonSuccess).
|
|
SetError(nil).
|
|
Throw(payload.Context)
|
|
|
|
result = &UserInfoResult{
|
|
Common: shared.CommonResult{
|
|
HttpCode: 200,
|
|
Exception: exception,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|