WIP: Restructing auth api and service
Signed-off-by: Asai Neko <sugar@sne.moe>
This commit is contained in:
3
service/service_user/create_user.go
Normal file
3
service/service_user/create_user.go
Normal file
@@ -0,0 +1,3 @@
|
||||
package service_user
|
||||
|
||||
func (self *UserServiceImpl) CreateUser() {}
|
||||
94
service/service_user/get_user_info.go
Normal file
94
service/service_user/get_user_info.go
Normal file
@@ -0,0 +1,94 @@
|
||||
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
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
65
service/service_user/list_user_full_table.go
Normal file
65
service/service_user/list_user_full_table.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package service_user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"nixcn-cms/data"
|
||||
"nixcn-cms/internal/exception"
|
||||
"nixcn-cms/service/shared"
|
||||
)
|
||||
|
||||
type UserTablePayload struct {
|
||||
Context context.Context
|
||||
}
|
||||
|
||||
type UserTableResult struct {
|
||||
Common shared.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: shared.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: shared.CommonResult{
|
||||
HttpCode: 200,
|
||||
Exception: exception,
|
||||
},
|
||||
Data: userFullTable,
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
138
service/service_user/list_users.go
Normal file
138
service/service_user/list_users.go
Normal file
@@ -0,0 +1,138 @@
|
||||
package service_user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"nixcn-cms/data"
|
||||
"nixcn-cms/internal/exception"
|
||||
"nixcn-cms/service/shared"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type UserListPayload struct {
|
||||
Context context.Context
|
||||
Limit *string
|
||||
Offset *string
|
||||
}
|
||||
|
||||
type UserListResult struct {
|
||||
Common shared.CommonResult
|
||||
Data *[]data.UserSearchDoc `json:"user_list"`
|
||||
}
|
||||
|
||||
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: shared.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: shared.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: shared.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: shared.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: shared.CommonResult{
|
||||
HttpCode: 200,
|
||||
Exception: exception,
|
||||
},
|
||||
Data: userList,
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
}
|
||||
15
service/service_user/service.go
Normal file
15
service/service_user/service.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package service_user
|
||||
|
||||
type UserService interface {
|
||||
GetUserInfo(*UserInfoPayload) *UserInfoResult
|
||||
UpdateUserInfo(*UserInfoPayload) *UserInfoResult
|
||||
ListUsers(*UserListPayload) *UserListResult
|
||||
GetUserFullTable(*UserTablePayload) *UserTableResult
|
||||
CreateUser()
|
||||
}
|
||||
|
||||
type UserServiceImpl struct{}
|
||||
|
||||
func NewUserService() UserService {
|
||||
return &UserServiceImpl{}
|
||||
}
|
||||
177
service/service_user/update_user_info.go
Normal file
177
service/service_user/update_user_info.go
Normal file
@@ -0,0 +1,177 @@
|
||||
package service_user
|
||||
|
||||
import (
|
||||
"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
|
||||
|
||||
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: shared.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: shared.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: 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 {
|
||||
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
|
||||
}
|
||||
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: shared.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: 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
|
||||
}
|
||||
Reference in New Issue
Block a user