470 lines
11 KiB
Go
470 lines
11 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
"nixcn-cms/data"
|
|
"nixcn-cms/internal/cryptography"
|
|
"nixcn-cms/internal/exception"
|
|
"strconv"
|
|
"unicode/utf8"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type UserService interface {
|
|
GetUserInfo(*UserInfoPayload) *UserInfoResult
|
|
UpdateUserInfo(*UserInfoPayload) *UserInfoResult
|
|
ListUsers(*UserListPayload) *UserListResult
|
|
GetUserFullTable(*UserTablePayload) *UserTableResult
|
|
CreateUser()
|
|
}
|
|
|
|
type UserServiceImpl struct{}
|
|
|
|
func NewUserService() UserService {
|
|
return &UserServiceImpl{}
|
|
}
|
|
|
|
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
|
|
Data *UserInfoData
|
|
}
|
|
|
|
type UserInfoResult struct {
|
|
Common 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: CommonResult{
|
|
HttpCode: 404,
|
|
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: 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
|
|
}
|
|
|
|
// UpdateUserInfo
|
|
func (self *UserServiceImpl) UpdateUserInfo(payload *UserInfoPayload) (result *UserInfoResult) {
|
|
var err error
|
|
|
|
userData := new(data.User).
|
|
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 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: CommonResult{
|
|
HttpCode: 400,
|
|
Exception: execption,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
userData.SetUsername(payload.Data.Username)
|
|
}
|
|
|
|
if payload.Data.Nickname != "" {
|
|
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: CommonResult{
|
|
HttpCode: 400,
|
|
Exception: execption,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
userData.SetNickname(payload.Data.Nickname)
|
|
}
|
|
|
|
if payload.Data.Subtitle != "" {
|
|
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: 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 {
|
|
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: CommonResult{
|
|
HttpCode: 400,
|
|
Exception: execption,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
userData.SetAvatar(payload.Data.Avatar)
|
|
}
|
|
|
|
if payload.Data.Bio != "" {
|
|
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: CommonResult{
|
|
HttpCode: 400,
|
|
Exception: execption,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
userData.Bio = payload.Data.Bio
|
|
}
|
|
|
|
err = userData.UpdateByUserID(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: 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: CommonResult{
|
|
HttpCode: 200,
|
|
Exception: exception,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
type UserListPayload struct {
|
|
Context context.Context
|
|
Limit *string
|
|
Offset *string
|
|
}
|
|
|
|
type UserListResult struct {
|
|
Common CommonResult
|
|
Data *[]data.UserSearchDoc `json:"user_list"`
|
|
}
|
|
|
|
// ListUsers
|
|
func (self *UserServiceImpl) ListUsers(payload *UserListPayload) (result *UserListResult) {
|
|
var limit string
|
|
if payload.Limit == nil || *payload.Limit == "" {
|
|
limit = "0"
|
|
}
|
|
|
|
var offset string
|
|
if payload.Offset == nil || *payload.Offset == "" {
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.ServiceUser).
|
|
SetEndpoint(exception.EndpointUserServiceList).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
SetError(nil).
|
|
Throw(payload.Context)
|
|
|
|
result = &UserListResult{
|
|
Common: CommonResult{
|
|
HttpCode: 500,
|
|
Exception: exception,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
} else {
|
|
offset = *payload.Offset
|
|
}
|
|
|
|
// Parse string to int64
|
|
limitNum, err := strconv.ParseInt(limit, 10, 64)
|
|
if err != nil {
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.ServiceUser).
|
|
SetEndpoint(exception.EndpointUserServiceList).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
SetError(err).
|
|
Throw(payload.Context)
|
|
|
|
result = &UserListResult{
|
|
Common: CommonResult{
|
|
HttpCode: 400,
|
|
Exception: exception,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
offsetNum, err := strconv.ParseInt(offset, 10, 64)
|
|
if err != nil {
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusUser).
|
|
SetService(exception.ServiceUser).
|
|
SetEndpoint(exception.EndpointUserServiceList).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorInvalidInput).
|
|
SetError(err).
|
|
Throw(payload.Context)
|
|
|
|
result = &UserListResult{
|
|
Common: CommonResult{
|
|
HttpCode: 400,
|
|
Exception: exception,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// Get user list from search engine
|
|
userList, err := new(data.User).
|
|
FastListUsers(payload.Context, &limitNum, &offsetNum)
|
|
if err != nil {
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusServer).
|
|
SetService(exception.ServiceUser).
|
|
SetEndpoint(exception.EndpointUserServiceList).
|
|
SetType(exception.TypeSpecific).
|
|
SetOriginal(exception.UserListMeilisearchFailed).
|
|
SetError(err).
|
|
Throw(payload.Context)
|
|
|
|
result = &UserListResult{
|
|
Common: CommonResult{
|
|
HttpCode: 500,
|
|
Exception: exception,
|
|
},
|
|
Data: nil,
|
|
}
|
|
}
|
|
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusServer).
|
|
SetService(exception.ServiceUser).
|
|
SetEndpoint(exception.EndpointUserServiceList).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonSuccess).
|
|
SetError(nil).
|
|
Throw(payload.Context)
|
|
|
|
result = &UserListResult{
|
|
Common: CommonResult{
|
|
HttpCode: 200,
|
|
Exception: exception,
|
|
},
|
|
Data: userList,
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
type UserTablePayload struct {
|
|
Context context.Context
|
|
}
|
|
|
|
type UserTableResult struct {
|
|
Common CommonResult
|
|
Data *[]data.User `json:"user_table"`
|
|
}
|
|
|
|
// ListUserFullTable
|
|
func (self *UserServiceImpl) GetUserFullTable(payload *UserTablePayload) (result *UserTableResult) {
|
|
var err error
|
|
|
|
userFullTable, err := new(data.User).
|
|
GetFullTable(payload.Context)
|
|
|
|
if err != nil {
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusServer).
|
|
SetService(exception.ServiceUser).
|
|
SetEndpoint(exception.EndpointUserServiceFull).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonErrorDatabase).
|
|
SetError(err).
|
|
Throw(payload.Context)
|
|
|
|
result = &UserTableResult{
|
|
Common: CommonResult{
|
|
HttpCode: 500,
|
|
Exception: exception,
|
|
},
|
|
Data: nil,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
exception := new(exception.Builder).
|
|
SetStatus(exception.StatusServer).
|
|
SetService(exception.ServiceUser).
|
|
SetEndpoint(exception.EndpointUserServiceFull).
|
|
SetType(exception.TypeCommon).
|
|
SetOriginal(exception.CommonSuccess).
|
|
SetError(nil).
|
|
Throw(payload.Context)
|
|
|
|
result = &UserTableResult{
|
|
Common: CommonResult{
|
|
HttpCode: 200,
|
|
Exception: exception,
|
|
},
|
|
Data: userFullTable,
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// CreateUser
|
|
func (self *UserServiceImpl) CreateUser() {}
|