14
data/user.go
14
data/user.go
@@ -127,12 +127,20 @@ func (self *User) Create(ctx context.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *User) UpdateByUserID(ctx context.Context, userId *uuid.UUID) error {
|
func (self *User) UpdateByUserID(ctx context.Context, userId *uuid.UUID, updates map[string]any) error {
|
||||||
return Database.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
return Database.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||||
if err := tx.Model(&User{}).Where("user_id = ?", userId).Updates(&self).Error; err != nil {
|
if err := tx.Model(&User{}).
|
||||||
|
Where("user_id = ?", userId).
|
||||||
|
Updates(updates).Error; err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
|
var updatedUser User
|
||||||
|
if err := tx.Where("user_id = ?", userId).First(&updatedUser).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return updatedUser.UpdateSearchIndex(&ctx)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,13 +12,13 @@ import (
|
|||||||
type UserInfoData struct {
|
type UserInfoData struct {
|
||||||
UserId uuid.UUID `json:"user_id"`
|
UserId uuid.UUID `json:"user_id"`
|
||||||
Email string `json:"email"`
|
Email string `json:"email"`
|
||||||
Username string `json:"username"`
|
Username *string `json:"username"`
|
||||||
Nickname string `json:"nickname"`
|
Nickname *string `json:"nickname"`
|
||||||
Subtitle string `json:"subtitle"`
|
Subtitle *string `json:"subtitle"`
|
||||||
Avatar string `json:"avatar"`
|
Avatar *string `json:"avatar"`
|
||||||
Bio string `json:"bio"`
|
Bio *string `json:"bio"`
|
||||||
PermissionLevel uint `json:"permission_level"`
|
PermissionLevel uint `json:"permission_level"`
|
||||||
AllowPublic bool `json:"allow_public"`
|
AllowPublic *bool `json:"allow_public"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserInfoPayload struct {
|
type UserInfoPayload struct {
|
||||||
@@ -80,13 +80,13 @@ func (self *UserServiceImpl) GetUserInfo(payload *UserInfoPayload) (result *User
|
|||||||
Data: &UserInfoData{
|
Data: &UserInfoData{
|
||||||
UserId: userData.UserId,
|
UserId: userData.UserId,
|
||||||
Email: userData.Email,
|
Email: userData.Email,
|
||||||
Username: userData.Username,
|
Username: &userData.Username,
|
||||||
Nickname: userData.Nickname,
|
Nickname: &userData.Nickname,
|
||||||
Subtitle: userData.Subtitle,
|
Subtitle: &userData.Subtitle,
|
||||||
Avatar: userData.Avatar,
|
Avatar: &userData.Avatar,
|
||||||
Bio: userData.Bio,
|
Bio: &userData.Bio,
|
||||||
PermissionLevel: userData.PermissionLevel,
|
PermissionLevel: userData.PermissionLevel,
|
||||||
AllowPublic: userData.AllowPublic,
|
AllowPublic: &userData.AllowPublic,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package service_user
|
package service_user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log/slog"
|
||||||
"net/url"
|
"net/url"
|
||||||
"nixcn-cms/data"
|
"nixcn-cms/data"
|
||||||
"nixcn-cms/internal/cryptography"
|
"nixcn-cms/internal/cryptography"
|
||||||
@@ -12,15 +13,36 @@ import (
|
|||||||
func (self *UserServiceImpl) UpdateUserInfo(payload *UserInfoPayload) (result *UserInfoResult) {
|
func (self *UserServiceImpl) UpdateUserInfo(payload *UserInfoPayload) (result *UserInfoResult) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
userData := new(data.User).
|
updates := make(map[string]any)
|
||||||
SetNickname(payload.Data.Nickname).
|
|
||||||
SetSubtitle(payload.Data.Subtitle).
|
|
||||||
SetAvatar(payload.Data.Avatar).
|
|
||||||
SetBio(payload.Data.Bio).
|
|
||||||
SetAllowPublic(payload.Data.AllowPublic)
|
|
||||||
|
|
||||||
if payload.Data.Username != "" {
|
if payload.Data.Username != nil {
|
||||||
if len(payload.Data.Username) < 5 || len(payload.Data.Username) >= 255 {
|
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).
|
execption := new(exception.Builder).
|
||||||
SetStatus(exception.StatusUser).
|
SetStatus(exception.StatusUser).
|
||||||
SetService(exception.ServiceUser).
|
SetService(exception.ServiceUser).
|
||||||
@@ -37,52 +59,35 @@ func (self *UserServiceImpl) UpdateUserInfo(payload *UserInfoPayload) (result *U
|
|||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
userData.SetUsername(payload.Data.Username)
|
updates["nickname"] = *payload.Data.Nickname
|
||||||
}
|
}
|
||||||
|
|
||||||
if utf8.RuneCountInString(payload.Data.Nickname) > 24 {
|
if payload.Data.Subtitle != nil {
|
||||||
execption := new(exception.Builder).
|
if utf8.RuneCountInString(*payload.Data.Subtitle) > 32 {
|
||||||
SetStatus(exception.StatusUser).
|
execption := new(exception.Builder).
|
||||||
SetService(exception.ServiceUser).
|
SetStatus(exception.StatusUser).
|
||||||
SetType(exception.TypeCommon).
|
SetService(exception.ServiceUser).
|
||||||
SetOriginal(exception.CommonErrorInvalidInput)
|
SetEndpoint(exception.EndpointUserServiceUpdate).
|
||||||
|
SetType(exception.TypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInvalidInput).
|
||||||
|
SetError(nil).
|
||||||
|
Throw(payload.Context)
|
||||||
|
|
||||||
result = &UserInfoResult{
|
result = &UserInfoResult{
|
||||||
Common: shared.CommonResult{
|
Common: shared.CommonResult{
|
||||||
HttpCode: 400,
|
HttpCode: 400,
|
||||||
Exception: execption,
|
Exception: execption,
|
||||||
},
|
},
|
||||||
Data: nil,
|
Data: nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
updates["subtitle"] = *payload.Data.Subtitle
|
||||||
return
|
|
||||||
}
|
}
|
||||||
userData.SetNickname(payload.Data.Nickname)
|
|
||||||
|
|
||||||
if utf8.RuneCountInString(payload.Data.Subtitle) > 32 {
|
if payload.Data.Avatar != nil {
|
||||||
execption := new(exception.Builder).
|
_, err := url.ParseRequestURI(*payload.Data.Avatar)
|
||||||
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
|
|
||||||
}
|
|
||||||
userData.SetSubtitle(payload.Data.Subtitle)
|
|
||||||
|
|
||||||
if payload.Data.Avatar != "" {
|
|
||||||
_, err := url.ParseRequestURI(payload.Data.Avatar)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
execption := new(exception.Builder).
|
execption := new(exception.Builder).
|
||||||
SetStatus(exception.StatusUser).
|
SetStatus(exception.StatusUser).
|
||||||
@@ -103,38 +108,62 @@ func (self *UserServiceImpl) UpdateUserInfo(payload *UserInfoPayload) (result *U
|
|||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
userData.SetAvatar(payload.Data.Avatar)
|
updates["avatar"] = *payload.Data.Avatar
|
||||||
} else if payload.Data.Avatar == "" {
|
|
||||||
userData.SetAvatar("")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if payload.Data.Bio != "" {
|
if payload.Data.Bio != nil {
|
||||||
if !cryptography.IsBase64Std(payload.Data.Bio) {
|
val := *payload.Data.Bio
|
||||||
execption := new(exception.Builder).
|
if val != "" {
|
||||||
SetStatus(exception.StatusUser).
|
if !cryptography.IsBase64Std(*payload.Data.Bio) {
|
||||||
SetService(exception.ServiceUser).
|
execption := new(exception.Builder).
|
||||||
SetEndpoint(exception.EndpointUserServiceUpdate).
|
SetStatus(exception.StatusUser).
|
||||||
SetType(exception.TypeCommon).
|
SetService(exception.ServiceUser).
|
||||||
SetOriginal(exception.CommonErrorInvalidInput).
|
SetEndpoint(exception.EndpointUserServiceUpdate).
|
||||||
SetError(nil).
|
SetType(exception.TypeCommon).
|
||||||
Throw(payload.Context)
|
SetOriginal(exception.CommonErrorInvalidInput).
|
||||||
|
SetError(nil).
|
||||||
|
Throw(payload.Context)
|
||||||
|
|
||||||
result = &UserInfoResult{
|
result = &UserInfoResult{
|
||||||
Common: shared.CommonResult{
|
Common: shared.CommonResult{
|
||||||
HttpCode: 400,
|
HttpCode: 400,
|
||||||
Exception: execption,
|
Exception: execption,
|
||||||
},
|
},
|
||||||
Data: nil,
|
Data: nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
userData.SetBio(payload.Data.Bio)
|
updates["bio"] = *payload.Data.Bio
|
||||||
} else if payload.Data.Bio == "" {
|
|
||||||
userData.SetBio("")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
err = userData.UpdateByUserID(payload.Context, &payload.UserId)
|
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 {
|
if err != nil {
|
||||||
exception := new(exception.Builder).
|
exception := new(exception.Builder).
|
||||||
SetStatus(exception.StatusServer).
|
SetStatus(exception.StatusServer).
|
||||||
|
|||||||
Reference in New Issue
Block a user