Files
cms-server/service/service_user/get_user_info.go
Asai Neko cbc358b96e
All checks were successful
Client CMS Check Build (NixCN CMS) TeamCity build finished
Backend Check Build (NixCN CMS) TeamCity build finished
Mod get_user_info in service_user, handle isother
Signed-off-by: Asai Neko <sugar@sne.moe>
2026-01-31 08:38:37 +08:00

121 lines
2.7 KiB
Go

package service_user
import (
"context"
"nixcn-cms/data"
"nixcn-cms/internal/exception"
"nixcn-cms/service/shared"
"github.com/google/uuid"
)
type UserInfoData struct {
UserId uuid.UUID `json:"user_id"`
Email string `json:"email"`
Username *string `json:"username"`
Nickname *string `json:"nickname"`
Subtitle *string `json:"subtitle"`
Avatar *string `json:"avatar"`
Bio *string `json:"bio"`
PermissionLevel uint `json:"permission_level"`
AllowPublic *bool `json:"allow_public"`
}
type UserInfoPayload struct {
Context context.Context
UserId uuid.UUID
IsOther bool
Data *UserInfoData
}
type UserInfoResult struct {
Common shared.CommonResult
Data *UserInfoData
}
// GetUserInfo
func (self *UserServiceImpl) GetUserInfo(payload *UserInfoPayload) (result *UserInfoResult) {
var err error
userData, err := new(data.User).
GetByUserId(
payload.Context,
&payload.UserId,
)
if err != nil {
exception := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceUser).
SetEndpoint(exception.EndpointUserServiceInfo).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorUserNotFound).
SetError(err).
Throw(payload.Context)
result = &UserInfoResult{
Common: shared.CommonResult{
HttpCode: 404,
Exception: exception,
},
Data: nil,
}
return
}
if payload.IsOther {
if !userData.AllowPublic {
exception := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceUser).
SetEndpoint(exception.EndpointUserServiceInfo).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonErrorUserNotFound).
SetError(err).
Throw(payload.Context)
result = &UserInfoResult{
Common: shared.CommonResult{
HttpCode: 404,
Exception: exception,
},
Data: &UserInfoData{
AllowPublic: &userData.AllowPublic,
},
}
return
}
}
exception := new(exception.Builder).
SetStatus(exception.StatusUser).
SetService(exception.ServiceUser).
SetEndpoint(exception.EndpointUserServiceInfo).
SetType(exception.TypeCommon).
SetOriginal(exception.CommonSuccess).
SetError(nil).
Throw(payload.Context)
result = &UserInfoResult{
Common: shared.CommonResult{
HttpCode: 200,
Exception: exception,
},
Data: &UserInfoData{
UserId: userData.UserId,
Email: userData.Email,
Username: &userData.Username,
Nickname: &userData.Nickname,
Subtitle: &userData.Subtitle,
Avatar: &userData.Avatar,
Bio: &userData.Bio,
PermissionLevel: userData.PermissionLevel,
AllowPublic: &userData.AllowPublic,
},
}
return
}