Files
cms-server/service/service_user/get_user_info.go
Asai Neko 83cec316bc
All checks were successful
Client CMS Check Build (NixCN CMS) TeamCity build finished
Backend Check Build (NixCN CMS) TeamCity build finished
Add common error user not public
Signed-off-by: Asai Neko <sugar@sne.moe>
2026-01-31 09:08:03 +08:00

119 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.CommonErrorUserNotPublic).
SetError(err).
Throw(payload.Context)
result = &UserInfoResult{
Common: shared.CommonResult{
HttpCode: 403,
Exception: exception,
},
Data: nil,
}
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
}