207 lines
4.7 KiB
Go
207 lines
4.7 KiB
Go
package service_user
|
|
|
|
import (
|
|
"log/slog"
|
|
"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
|
|
}
|
|
|
|
slog.Debug("DataNickname", slog.Any("DataNickName", payload.Data.Nickname))
|
|
|
|
if payload.Data.Nickname != nil {
|
|
if utf8.RuneCountInString(*payload.Data.Nickname) > 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 {
|
|
_, 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
|
|
}
|
|
|
|
err = new(data.User).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
|
|
}
|