First merge from develop to main (WIP) #7
@@ -1,7 +1,24 @@
|
||||
package user
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"nixcn-cms/internal/exception"
|
||||
"nixcn-cms/service"
|
||||
"nixcn-cms/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func (self *UserHandler) Full(c *gin.Context) {
|
||||
userTablePayload := &service.UserTablePayload{
|
||||
Context: c,
|
||||
}
|
||||
|
||||
result := self.svc.GetUserFullTable(userTablePayload)
|
||||
|
||||
if result.Common.Exception.Original != exception.CommonSuccess {
|
||||
utils.HttpResponse(c, result.Common.HttpCode, result.Common.Exception.String())
|
||||
return
|
||||
}
|
||||
|
||||
utils.HttpResponse(c, result.Common.HttpCode, result.Common.Exception.String(), result.Data)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,56 @@
|
||||
package user
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"nixcn-cms/internal/exception"
|
||||
"nixcn-cms/service"
|
||||
"nixcn-cms/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func (self *UserHandler) Info(c *gin.Context) {
|
||||
userIdOrig, ok := c.Get("user_id")
|
||||
if !ok {
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceUser).
|
||||
SetEndpoint(exception.EndpointUserServiceInfo).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorMissingUserId).
|
||||
Throw(c).
|
||||
String()
|
||||
utils.HttpResponse(c, 403, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
userId, err := uuid.Parse(userIdOrig.(string))
|
||||
if err != nil {
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.StatusServer).
|
||||
SetService(exception.ServiceUser).
|
||||
SetEndpoint(exception.EndpointUserServiceInfo).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||
SetError(err).
|
||||
Throw(c).
|
||||
String()
|
||||
utils.HttpResponse(c, 500, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
UserInfoPayload := &service.UserInfoPayload{
|
||||
Context: c,
|
||||
UserId: userId,
|
||||
Data: nil,
|
||||
}
|
||||
|
||||
result := self.svc.GetUserInfo(UserInfoPayload)
|
||||
|
||||
if result.Common.Exception.Original != exception.CommonSuccess {
|
||||
utils.HttpResponse(c, result.Common.HttpCode, result.Common.Exception.String())
|
||||
return
|
||||
}
|
||||
|
||||
utils.HttpResponse(c, result.Common.HttpCode, result.Common.Exception.String(), result.Data)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,31 @@
|
||||
package user
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"nixcn-cms/internal/exception"
|
||||
"nixcn-cms/service"
|
||||
"nixcn-cms/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func (self *UserHandler) List(c *gin.Context) {
|
||||
limit, limitStatus := c.GetQuery("limit")
|
||||
offset, offsetStatus := c.GetQuery("offset")
|
||||
|
||||
userListPayload := &service.UserListPayload{
|
||||
Context: c,
|
||||
Limit: &limit,
|
||||
LimitStatus: &limitStatus,
|
||||
Offset: &offset,
|
||||
OffsetStatus: &offsetStatus,
|
||||
}
|
||||
|
||||
result := self.svc.ListUsers(userListPayload)
|
||||
|
||||
if result.Common.Exception.Original != exception.CommonSuccess {
|
||||
utils.HttpResponse(c, result.Common.HttpCode, result.Common.Exception.String())
|
||||
return
|
||||
}
|
||||
|
||||
utils.HttpResponse(c, result.Common.HttpCode, result.Common.Exception.String(), result.Data)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,69 @@
|
||||
package user
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"nixcn-cms/internal/exception"
|
||||
"nixcn-cms/service"
|
||||
"nixcn-cms/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func (self *UserHandler) Update(c *gin.Context) {
|
||||
userIdOrig, ok := c.Get("user_id")
|
||||
if !ok {
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceUser).
|
||||
SetEndpoint(exception.EndpointUserServiceUpdate).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorMissingUserId).
|
||||
Throw(c).
|
||||
String()
|
||||
utils.HttpResponse(c, 403, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
userId, err := uuid.Parse(userIdOrig.(string))
|
||||
if err != nil {
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.StatusServer).
|
||||
SetService(exception.ServiceUser).
|
||||
SetEndpoint(exception.EndpointUserServiceUpdate).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||
Throw(c).
|
||||
String()
|
||||
utils.HttpResponse(c, 500, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
userInfoPayload := &service.UserInfoPayload{
|
||||
Context: c,
|
||||
UserId: userId,
|
||||
}
|
||||
|
||||
err = c.ShouldBindJSON(&userInfoPayload.Data)
|
||||
if err != nil {
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceUser).
|
||||
SetEndpoint(exception.EndpointUserServiceUpdate).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
SetError(err).
|
||||
Throw(c).
|
||||
String()
|
||||
utils.HttpResponse(c, 400, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
result := self.svc.UpdateUserInfo(userInfoPayload)
|
||||
|
||||
if result.Common.Exception.Original != exception.CommonSuccess {
|
||||
utils.HttpResponse(c, result.Common.HttpCode, result.Common.Exception.String())
|
||||
return
|
||||
}
|
||||
|
||||
utils.HttpResponse(c, result.Common.HttpCode, result.Common.Exception.String(), result.Data)
|
||||
}
|
||||
|
||||
26
data/user.go
26
data/user.go
@@ -73,10 +73,10 @@ func (self *User) SetAllowPublic(s bool) *User {
|
||||
return self
|
||||
}
|
||||
|
||||
func (self *User) GetByEmail(ctx *context.Context, email *string) (*User, error) {
|
||||
func (self *User) GetByEmail(ctx context.Context, email *string) (*User, error) {
|
||||
var user User
|
||||
|
||||
err := Database.WithContext(*ctx).
|
||||
err := Database.WithContext(ctx).
|
||||
Where("email = ?", email).
|
||||
First(&user).Error
|
||||
|
||||
@@ -90,10 +90,10 @@ func (self *User) GetByEmail(ctx *context.Context, email *string) (*User, error)
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (self *User) GetByUserId(ctx *context.Context, userId *uuid.UUID) (*User, error) {
|
||||
func (self *User) GetByUserId(ctx context.Context, userId *uuid.UUID) (*User, error) {
|
||||
var user User
|
||||
|
||||
err := Database.WithContext(*ctx).
|
||||
err := Database.WithContext(ctx).
|
||||
Where("user_id = ?", userId).
|
||||
First(&user).Error
|
||||
|
||||
@@ -107,12 +107,12 @@ func (self *User) GetByUserId(ctx *context.Context, userId *uuid.UUID) (*User, e
|
||||
return &user, err
|
||||
}
|
||||
|
||||
func (self *User) Create(ctx *context.Context) error {
|
||||
func (self *User) Create(ctx context.Context) error {
|
||||
self.UUID = uuid.New()
|
||||
self.UserId = uuid.New()
|
||||
|
||||
// DB transaction only
|
||||
if err := Database.WithContext(*ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := Database.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Create(self).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -122,7 +122,7 @@ func (self *User) Create(ctx *context.Context) error {
|
||||
}
|
||||
|
||||
// Search index (eventual consistency)
|
||||
if err := self.UpdateSearchIndex(ctx); err != nil {
|
||||
if err := self.UpdateSearchIndex(&ctx); err != nil {
|
||||
// TODO: async retry / log
|
||||
return err
|
||||
}
|
||||
@@ -130,8 +130,8 @@ func (self *User) Create(ctx *context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *User) UpdateByUserID(ctx *context.Context, userId *uuid.UUID) error {
|
||||
return Database.WithContext(*ctx).Transaction(func(tx *gorm.DB) error {
|
||||
func (self *User) UpdateByUserID(ctx context.Context, userId *uuid.UUID) 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 {
|
||||
return err
|
||||
}
|
||||
@@ -139,20 +139,20 @@ func (self *User) UpdateByUserID(ctx *context.Context, userId *uuid.UUID) error
|
||||
})
|
||||
}
|
||||
|
||||
func (self *User) GetFullTable(ctx *context.Context) (*[]User, error) {
|
||||
func (self *User) GetFullTable(ctx context.Context) (*[]User, error) {
|
||||
var users []User
|
||||
err := Database.WithContext(*ctx).Find(&users).Error
|
||||
err := Database.WithContext(ctx).Find(&users).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &users, nil
|
||||
}
|
||||
|
||||
func (self *User) FastListUsers(ctx *context.Context, limit, offset *int64) (*[]UserSearchDoc, error) {
|
||||
func (self *User) FastListUsers(ctx context.Context, limit, offset *int64) (*[]UserSearchDoc, error) {
|
||||
index := MeiliSearch.Index("user")
|
||||
|
||||
// Fast read from MeiliSearch, no DB involved
|
||||
result, err := index.SearchWithContext(*ctx, "", &meilisearch.SearchRequest{
|
||||
result, err := index.SearchWithContext(ctx, "", &meilisearch.SearchRequest{
|
||||
Limit: *limit,
|
||||
Offset: *offset,
|
||||
})
|
||||
|
||||
@@ -62,7 +62,7 @@ func (self *Builder) build() {
|
||||
)
|
||||
}
|
||||
|
||||
func (self *Builder) Throw(ctx *context.Context) *Builder {
|
||||
func (self *Builder) Throw(ctx context.Context) *Builder {
|
||||
self.build()
|
||||
if self.Error != nil {
|
||||
ErrorHandler(ctx, self.Status, self.ErrorCode, self.Error)
|
||||
|
||||
@@ -5,15 +5,15 @@ import (
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
func ErrorHandler(ctx *context.Context, status string, errorCode string, err error) {
|
||||
func ErrorHandler(ctx context.Context, status string, errorCode string, err error) {
|
||||
switch status {
|
||||
case StatusSuccess:
|
||||
slog.InfoContext(*ctx, "Service exception! ErrId: "+errorCode, "id", errorCode, "err", err)
|
||||
slog.InfoContext(ctx, "Service exception! ErrId: "+errorCode, "id", errorCode, "err", err)
|
||||
case StatusUser:
|
||||
slog.WarnContext(*ctx, "Service exception! ErrId: "+errorCode, "id", errorCode, "err", err)
|
||||
slog.WarnContext(ctx, "Service exception! ErrId: "+errorCode, "id", errorCode, "err", err)
|
||||
case StatusServer:
|
||||
slog.ErrorContext(*ctx, "Service exception! ErrId: "+errorCode, "id", errorCode, "err", err)
|
||||
slog.ErrorContext(ctx, "Service exception! ErrId: "+errorCode, "id", errorCode, "err", err)
|
||||
case StatusClient:
|
||||
slog.ErrorContext(*ctx, "Service exception! ErrId: "+errorCode, "id", errorCode, "err", err)
|
||||
slog.ErrorContext(ctx, "Service exception! ErrId: "+errorCode, "id", errorCode, "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,8 @@ func ApiVersionCheck() gin.HandlerFunc {
|
||||
SetEndpoint(exception.EndpointMiddlewareService).
|
||||
SetType(exception.TypeSpecific).
|
||||
SetOriginal(exception.ApiVersionNotFound).
|
||||
Build(c)
|
||||
Throw(c).
|
||||
String()
|
||||
utils.HttpAbort(c, 400, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -23,7 +23,8 @@ func JWTAuth() gin.HandlerFunc {
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorUnauthorized).
|
||||
SetError(err).
|
||||
Build(c)
|
||||
Throw(c).
|
||||
String()
|
||||
|
||||
utils.HttpAbort(c, 401, errorCode)
|
||||
return
|
||||
|
||||
@@ -22,7 +22,9 @@ func Permission(requiredLevel uint) gin.HandlerFunc {
|
||||
SetEndpoint(exception.EndpointMiddlewareService).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorMissingUserId).
|
||||
Build(c)
|
||||
Throw(c).
|
||||
String()
|
||||
|
||||
utils.HttpAbort(c, 401, errorCode)
|
||||
return
|
||||
}
|
||||
@@ -36,12 +38,13 @@ func Permission(requiredLevel uint) gin.HandlerFunc {
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||
SetError(err).
|
||||
Build(c)
|
||||
Throw(c).
|
||||
String()
|
||||
utils.HttpAbort(c, 500, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
userData, err := new(data.User).GetByUserId(c, userId)
|
||||
userData, err := new(data.User).GetByUserId(c, &userId)
|
||||
if err != nil {
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
@@ -50,7 +53,9 @@ func Permission(requiredLevel uint) gin.HandlerFunc {
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorUserNotFound).
|
||||
SetError(err).
|
||||
Build(c)
|
||||
Throw(c).
|
||||
String()
|
||||
|
||||
utils.HttpAbort(c, 404, errorCode)
|
||||
return
|
||||
}
|
||||
@@ -68,7 +73,9 @@ func Permission(requiredLevel uint) gin.HandlerFunc {
|
||||
SetEndpoint(exception.EndpointMiddlewareService).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorPermissionDenied).
|
||||
Build(c)
|
||||
Throw(c).
|
||||
String()
|
||||
|
||||
utils.HttpAbort(c, 403, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -39,14 +39,14 @@ type UserInfoData struct {
|
||||
}
|
||||
|
||||
type UserInfoPayload struct {
|
||||
Context *context.Context
|
||||
UserId *uuid.UUID
|
||||
Context context.Context
|
||||
UserId uuid.UUID
|
||||
Data *UserInfoData
|
||||
}
|
||||
|
||||
type UserInfoResult struct {
|
||||
*CommonResult
|
||||
Data *UserInfoData
|
||||
Common CommonResult
|
||||
Data *UserInfoData
|
||||
}
|
||||
|
||||
// GetUserInfo
|
||||
@@ -56,7 +56,7 @@ func (self *UserServiceImpl) GetUserInfo(payload *UserInfoPayload) (result *User
|
||||
userData, err := new(data.User).
|
||||
GetByUserId(
|
||||
payload.Context,
|
||||
payload.UserId,
|
||||
&payload.UserId,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
@@ -70,7 +70,7 @@ func (self *UserServiceImpl) GetUserInfo(payload *UserInfoPayload) (result *User
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &UserInfoResult{
|
||||
CommonResult: &CommonResult{
|
||||
Common: CommonResult{
|
||||
HttpCode: 404,
|
||||
Exception: exception,
|
||||
},
|
||||
@@ -90,7 +90,7 @@ func (self *UserServiceImpl) GetUserInfo(payload *UserInfoPayload) (result *User
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &UserInfoResult{
|
||||
CommonResult: &CommonResult{
|
||||
Common: CommonResult{
|
||||
HttpCode: 200,
|
||||
Exception: exception,
|
||||
},
|
||||
@@ -130,10 +130,11 @@ func (self *UserServiceImpl) UpdateUserInfo(payload *UserInfoPayload) (result *U
|
||||
SetOriginal(exception.CommonErrorInvalidInput)
|
||||
|
||||
result = &UserInfoResult{
|
||||
CommonResult: &CommonResult{
|
||||
Common: CommonResult{
|
||||
HttpCode: 400,
|
||||
Exception: execption,
|
||||
},
|
||||
Data: nil,
|
||||
}
|
||||
|
||||
return
|
||||
@@ -150,10 +151,11 @@ func (self *UserServiceImpl) UpdateUserInfo(payload *UserInfoPayload) (result *U
|
||||
SetOriginal(exception.CommonErrorInvalidInput)
|
||||
|
||||
result = &UserInfoResult{
|
||||
CommonResult: &CommonResult{
|
||||
Common: CommonResult{
|
||||
HttpCode: 400,
|
||||
Exception: execption,
|
||||
},
|
||||
Data: nil,
|
||||
}
|
||||
|
||||
return
|
||||
@@ -173,10 +175,11 @@ func (self *UserServiceImpl) UpdateUserInfo(payload *UserInfoPayload) (result *U
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &UserInfoResult{
|
||||
CommonResult: &CommonResult{
|
||||
Common: CommonResult{
|
||||
HttpCode: 400,
|
||||
Exception: execption,
|
||||
},
|
||||
Data: nil,
|
||||
}
|
||||
|
||||
return
|
||||
@@ -197,10 +200,11 @@ func (self *UserServiceImpl) UpdateUserInfo(payload *UserInfoPayload) (result *U
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &UserInfoResult{
|
||||
CommonResult: &CommonResult{
|
||||
Common: CommonResult{
|
||||
HttpCode: 400,
|
||||
Exception: execption,
|
||||
},
|
||||
Data: nil,
|
||||
}
|
||||
|
||||
return
|
||||
@@ -220,10 +224,11 @@ func (self *UserServiceImpl) UpdateUserInfo(payload *UserInfoPayload) (result *U
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &UserInfoResult{
|
||||
CommonResult: &CommonResult{
|
||||
Common: CommonResult{
|
||||
HttpCode: 400,
|
||||
Exception: execption,
|
||||
},
|
||||
Data: nil,
|
||||
}
|
||||
|
||||
return
|
||||
@@ -231,7 +236,7 @@ func (self *UserServiceImpl) UpdateUserInfo(payload *UserInfoPayload) (result *U
|
||||
userData.Bio = payload.Data.Bio
|
||||
}
|
||||
|
||||
err = userData.UpdateByUserID(payload.Context, payload.UserId)
|
||||
err = userData.UpdateByUserID(payload.Context, &payload.UserId)
|
||||
if err != nil {
|
||||
exception := new(exception.Builder).
|
||||
SetStatus(exception.StatusServer).
|
||||
@@ -243,10 +248,11 @@ func (self *UserServiceImpl) UpdateUserInfo(payload *UserInfoPayload) (result *U
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &UserInfoResult{
|
||||
CommonResult: &CommonResult{
|
||||
Common: CommonResult{
|
||||
HttpCode: 500,
|
||||
Exception: exception,
|
||||
},
|
||||
Data: nil,
|
||||
}
|
||||
|
||||
return
|
||||
@@ -262,7 +268,7 @@ func (self *UserServiceImpl) UpdateUserInfo(payload *UserInfoPayload) (result *U
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &UserInfoResult{
|
||||
CommonResult: &CommonResult{
|
||||
Common: CommonResult{
|
||||
HttpCode: 200,
|
||||
Exception: exception,
|
||||
},
|
||||
@@ -273,7 +279,7 @@ func (self *UserServiceImpl) UpdateUserInfo(payload *UserInfoPayload) (result *U
|
||||
}
|
||||
|
||||
type UserListPayload struct {
|
||||
Context *context.Context
|
||||
Context context.Context
|
||||
Limit *string
|
||||
LimitStatus *bool
|
||||
Offset *string
|
||||
@@ -281,8 +287,8 @@ type UserListPayload struct {
|
||||
}
|
||||
|
||||
type UserListResult struct {
|
||||
*CommonResult
|
||||
UserList *[]data.UserSearchDoc `json:"user_list"`
|
||||
Common CommonResult
|
||||
Data *[]data.UserSearchDoc `json:"user_list"`
|
||||
}
|
||||
|
||||
// ListUsers
|
||||
@@ -304,11 +310,11 @@ func (self *UserServiceImpl) ListUsers(payload *UserListPayload) (result *UserLi
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &UserListResult{
|
||||
CommonResult: &CommonResult{
|
||||
Common: CommonResult{
|
||||
HttpCode: 500,
|
||||
Exception: exception,
|
||||
},
|
||||
UserList: nil,
|
||||
Data: nil,
|
||||
}
|
||||
|
||||
return
|
||||
@@ -329,11 +335,11 @@ func (self *UserServiceImpl) ListUsers(payload *UserListPayload) (result *UserLi
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &UserListResult{
|
||||
CommonResult: &CommonResult{
|
||||
Common: CommonResult{
|
||||
HttpCode: 400,
|
||||
Exception: exception,
|
||||
},
|
||||
UserList: nil,
|
||||
Data: nil,
|
||||
}
|
||||
|
||||
return
|
||||
@@ -351,11 +357,11 @@ func (self *UserServiceImpl) ListUsers(payload *UserListPayload) (result *UserLi
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &UserListResult{
|
||||
CommonResult: &CommonResult{
|
||||
Common: CommonResult{
|
||||
HttpCode: 400,
|
||||
Exception: exception,
|
||||
},
|
||||
UserList: nil,
|
||||
Data: nil,
|
||||
}
|
||||
|
||||
return
|
||||
@@ -375,11 +381,11 @@ func (self *UserServiceImpl) ListUsers(payload *UserListPayload) (result *UserLi
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &UserListResult{
|
||||
CommonResult: &CommonResult{
|
||||
Common: CommonResult{
|
||||
HttpCode: 500,
|
||||
Exception: exception,
|
||||
},
|
||||
UserList: nil,
|
||||
Data: nil,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -393,11 +399,11 @@ func (self *UserServiceImpl) ListUsers(payload *UserListPayload) (result *UserLi
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &UserListResult{
|
||||
CommonResult: &CommonResult{
|
||||
Common: CommonResult{
|
||||
HttpCode: 200,
|
||||
Exception: exception,
|
||||
},
|
||||
UserList: userList,
|
||||
Data: userList,
|
||||
}
|
||||
|
||||
return
|
||||
@@ -405,12 +411,12 @@ func (self *UserServiceImpl) ListUsers(payload *UserListPayload) (result *UserLi
|
||||
}
|
||||
|
||||
type UserTablePayload struct {
|
||||
Context *context.Context
|
||||
Context context.Context
|
||||
}
|
||||
|
||||
type UserTableResult struct {
|
||||
*CommonResult
|
||||
UserTable *[]data.User `json:"user_table"`
|
||||
Common CommonResult
|
||||
Data *[]data.User `json:"user_table"`
|
||||
}
|
||||
|
||||
// ListUserFullTable
|
||||
@@ -431,11 +437,11 @@ func (self *UserServiceImpl) GetUserFullTable(payload *UserTablePayload) (result
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &UserTableResult{
|
||||
CommonResult: &CommonResult{
|
||||
Common: CommonResult{
|
||||
HttpCode: 500,
|
||||
Exception: exception,
|
||||
},
|
||||
UserTable: nil,
|
||||
Data: nil,
|
||||
}
|
||||
|
||||
return
|
||||
@@ -451,11 +457,11 @@ func (self *UserServiceImpl) GetUserFullTable(payload *UserTablePayload) (result
|
||||
Throw(payload.Context)
|
||||
|
||||
result = &UserTableResult{
|
||||
CommonResult: &CommonResult{
|
||||
Common: CommonResult{
|
||||
HttpCode: 200,
|
||||
Exception: exception,
|
||||
},
|
||||
UserTable: userFullTable,
|
||||
Data: userFullTable,
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
@@ -1,81 +0,0 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"nixcn-cms/data"
|
||||
"nixcn-cms/internal/exception"
|
||||
"nixcn-cms/utils"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ServiceFullResponse struct {
|
||||
}
|
||||
|
||||
func ServiceFull(ctx context.Context, userId uuid.UUID) (data *[]data.User, httpCode int, errorCode string) {
|
||||
userIdOrig, ok := c.Get("user_id")
|
||||
if !ok {
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceUser).
|
||||
SetEndpoint(exception.EndpointUserServiceFull).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorMissingUserId).
|
||||
Build(c)
|
||||
utils.HttpResponse(c, 403, errorCode)
|
||||
return
|
||||
}
|
||||
userId, err := uuid.Parse(userIdOrig.(string))
|
||||
if err != nil {
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.StatusServer).
|
||||
SetService(exception.ServiceUser).
|
||||
SetEndpoint(exception.EndpointUserServiceFull).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||
SetError(err).
|
||||
Build(c)
|
||||
utils.HttpResponse(c, 500, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
userData, err := new(data.User).GetByUserId(c, userId)
|
||||
if err != nil {
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceUser).
|
||||
SetEndpoint(exception.EndpointUserServiceFull).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorUserNotFound).
|
||||
SetError(err).
|
||||
Build(c)
|
||||
utils.HttpResponse(c, 404, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
users, err := userData.GetFullTable(c)
|
||||
if err != nil {
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.StatusServer).
|
||||
SetService(exception.ServiceUser).
|
||||
SetEndpoint(exception.EndpointUserServiceFull).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorDatabase).
|
||||
SetError(err).
|
||||
Build(c)
|
||||
utils.HttpResponse(c, 500, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
userFullResp := struct {
|
||||
UserTable *[]data.User `json:"user_table"`
|
||||
}{users}
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.StatusServer).
|
||||
SetService(exception.ServiceUser).
|
||||
SetEndpoint(exception.EndpointUserServiceFull).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonSuccess).
|
||||
Build(c)
|
||||
utils.HttpResponse(c, 200, errorCode, userFullResp)
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"nixcn-cms/internal/exception"
|
||||
"nixcn-cms/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Info(c *gin.Context) {
|
||||
// userData := new(data.User)
|
||||
// userIdOrig, ok := c.Get("user_id")
|
||||
// if !ok {
|
||||
// errorCode := new(exception.Builder).
|
||||
// SetStatus(exception.StatusUser).
|
||||
// SetService(exception.ServiceUser).
|
||||
// SetEndpoint(exception.EndpointUserServiceInfo).
|
||||
// SetType(exception.TypeCommon).
|
||||
// SetOriginal(exception.CommonErrorMissingUserId).
|
||||
// Build(c)
|
||||
// utils.HttpResponse(c, 403, errorCode)
|
||||
// return
|
||||
// }
|
||||
// userId, err := uuid.Parse(userIdOrig.(string))
|
||||
// if err != nil {
|
||||
// errorCode := new(exception.Builder).
|
||||
// SetStatus(exception.StatusServer).
|
||||
// SetService(exception.ServiceUser).
|
||||
// SetEndpoint(exception.EndpointUserServiceInfo).
|
||||
// SetType(exception.TypeCommon).
|
||||
// SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||
// SetError(err).
|
||||
// Build(c)
|
||||
// utils.HttpResponse(c, 500, errorCode)
|
||||
// return
|
||||
// }
|
||||
|
||||
// Get user from database
|
||||
// user, err := userData.GetByUserId(c, userId)
|
||||
// if err != nil {
|
||||
// utils.HttpResponse(c, 404, errorCode)
|
||||
// return
|
||||
// }
|
||||
|
||||
userInfoResp := struct {
|
||||
}{user.UserId, user.Email, user.Username, user.Nickname, user.Subtitle, user.Avatar, user.Bio, user.PermissionLevel}
|
||||
|
||||
errorCode := new(exception.Builder).
|
||||
Build(c)
|
||||
utils.HttpResponse(c, 200, errorCode, userInfoResp)
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"nixcn-cms/data"
|
||||
"nixcn-cms/internal/exception"
|
||||
"nixcn-cms/utils"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func List(c *gin.Context) {
|
||||
// Get limit and offset from query
|
||||
limit, ok := c.GetQuery("limit")
|
||||
if !ok {
|
||||
limit = "0"
|
||||
}
|
||||
offset, ok := c.GetQuery("offset")
|
||||
if !ok {
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceUser).
|
||||
SetEndpoint(exception.EndpointUserServiceList).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
Build(c)
|
||||
utils.HttpResponse(c, 400, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
// Parse string to int64
|
||||
limitNum, err := strconv.ParseInt(limit, 10, 64)
|
||||
if err != nil {
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceUser).
|
||||
SetEndpoint(exception.EndpointUserServiceList).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
SetError(err).
|
||||
Build(c)
|
||||
utils.HttpResponse(c, 400, errorCode)
|
||||
return
|
||||
}
|
||||
offsetNum, err := strconv.ParseInt(offset, 10, 64)
|
||||
if err != nil {
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceUser).
|
||||
SetEndpoint(exception.EndpointUserServiceList).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
SetError(err).
|
||||
Build(c)
|
||||
utils.HttpResponse(c, 400, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
// Get user list from search engine
|
||||
list, err := new(data.User).FastListUsers(c, limitNum, offsetNum)
|
||||
if err != nil {
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.StatusServer).
|
||||
SetService(exception.ServiceUser).
|
||||
SetEndpoint(exception.EndpointUserServiceList).
|
||||
SetType(exception.TypeSpecific).
|
||||
SetOriginal(exception.UserListMeilisearchFailed).
|
||||
SetError(err).
|
||||
Build(c)
|
||||
utils.HttpResponse(c, 500, errorCode)
|
||||
}
|
||||
|
||||
userListResp := struct {
|
||||
List *[]data.UserSearchDoc `json:"list"`
|
||||
}{list}
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.StatusServer).
|
||||
SetService(exception.ServiceUser).
|
||||
SetEndpoint(exception.EndpointUserServiceList).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonSuccess).
|
||||
Build(c)
|
||||
utils.HttpResponse(c, 200, errorCode, userListResp)
|
||||
}
|
||||
@@ -1,161 +0,0 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"nixcn-cms/data"
|
||||
"nixcn-cms/internal/exception"
|
||||
"nixcn-cms/utils"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Update(c *gin.Context) {
|
||||
// New user model
|
||||
// userIdOrig, ok := c.Get("user_id")
|
||||
// if !ok {
|
||||
// errorCode := new(exception.Builder).
|
||||
// SetStatus(exception.StatusUser).
|
||||
// SetService(exception.ServiceUser).
|
||||
// SetEndpoint(exception.EndpointUserServiceUpdate).
|
||||
// SetType(exception.TypeCommon).
|
||||
// SetOriginal(exception.CommonErrorMissingUserId).
|
||||
// Build(c)
|
||||
// utils.HttpResponse(c, 403, errorCode)
|
||||
// return
|
||||
// }
|
||||
// userId, err := uuid.Parse(userIdOrig.(string))
|
||||
// if err != nil {
|
||||
// errorCode := new(exception.Builder).
|
||||
// SetStatus(exception.StatusServer).
|
||||
// SetService(exception.ServiceUser).
|
||||
// SetEndpoint(exception.EndpointUserServiceUpdate).
|
||||
// SetType(exception.TypeCommon).
|
||||
// SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||
// SetError(err).
|
||||
// Build(c)
|
||||
// utils.HttpResponse(c, 500, errorCode)
|
||||
// return
|
||||
// }
|
||||
|
||||
var ReqInfo data.User
|
||||
err = c.ShouldBindJSON(&ReqInfo)
|
||||
if err != nil {
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceUser).
|
||||
SetEndpoint(exception.EndpointUserServiceUpdate).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorInvalidInput).
|
||||
SetError(err).
|
||||
Build(c)
|
||||
utils.HttpResponse(c, 400, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
// Get user info
|
||||
userData, err := new(data.User).GetByUserId(c, userId)
|
||||
if err != nil {
|
||||
errorCode := new(exception.Builder).
|
||||
SetStatus(exception.StatusUser).
|
||||
SetService(exception.ServiceUser).
|
||||
SetEndpoint(exception.EndpointUserServiceUpdate).
|
||||
SetType(exception.TypeCommon).
|
||||
SetOriginal(exception.CommonErrorUserNotFound).
|
||||
SetError(err).
|
||||
Build(c)
|
||||
utils.HttpResponse(c, 500, errorCode)
|
||||
return
|
||||
}
|
||||
|
||||
// if len(ReqInfo.Email) < 5 || len(ReqInfo.Email) >= 255 {
|
||||
// utils.HttpResponse(c, 400, "", "invilad email")
|
||||
// return
|
||||
// }
|
||||
// userData.Email = ReqInfo.Email
|
||||
|
||||
// utils.HttpResponse(c, 400, "", "invilad user name")
|
||||
// return
|
||||
|
||||
// if ReqInfo.Username != "" {
|
||||
// if len(ReqInfo.Username) < 5 || len(ReqInfo.Username) >= 255 {
|
||||
// errorCode := new(exception.Builder).
|
||||
// SetStatus(exception.StatusUser).
|
||||
// SetService(exception.ServiceUser).
|
||||
// SetEndpoint(exception.EndpointUserServiceUpdate).
|
||||
// SetType(exception.TypeCommon).
|
||||
// SetOriginal(exception.CommonErrorInvalidInput).
|
||||
// Build(c)
|
||||
// utils.HttpResponse(c, 400, errorCode)
|
||||
// return
|
||||
// }
|
||||
// userData.Username = ReqInfo.Username
|
||||
// }
|
||||
|
||||
// if ReqInfo.Nickname != "" {
|
||||
// if utf8.RuneCountInString(ReqInfo.Nickname) > 24 {
|
||||
// errorCode := new(exception.Builder).
|
||||
// SetStatus(exception.StatusUser).
|
||||
// SetService(exception.ServiceUser).
|
||||
// SetEndpoint(exception.EndpointUserServiceUpdate).
|
||||
// SetType(exception.TypeCommon).
|
||||
// SetOriginal(exception.CommonErrorInvalidInput).
|
||||
// Build(c)
|
||||
// utils.HttpResponse(c, 400, errorCode)
|
||||
// return
|
||||
// }
|
||||
// userData.Nickname = ReqInfo.Nickname
|
||||
// }
|
||||
|
||||
// if ReqInfo.Subtitle != "" {
|
||||
// if utf8.RuneCountInString(ReqInfo.Subtitle) > 32 {
|
||||
// errorCode := new(exception.Builder).
|
||||
// SetStatus(exception.StatusUser).
|
||||
// SetService(exception.ServiceUser).
|
||||
// SetEndpoint(exception.EndpointUserServiceUpdate).
|
||||
// SetType(exception.TypeCommon).
|
||||
// SetOriginal(exception.CommonErrorInvalidInput).
|
||||
// Build(c)
|
||||
// utils.HttpResponse(c, 400, errorCode)
|
||||
// return
|
||||
// }
|
||||
// userData.Subtitle = ReqInfo.Subtitle
|
||||
// }
|
||||
|
||||
// if ReqInfo.Avatar != "" {
|
||||
// _, err := url.ParseRequestURI(ReqInfo.Avatar)
|
||||
// if err != nil {
|
||||
// errorCode := new(exception.Builder).
|
||||
// SetStatus(exception.StatusUser).
|
||||
// SetService(exception.ServiceUser).
|
||||
// SetEndpoint(exception.EndpointUserServiceUpdate).
|
||||
// SetType(exception.TypeCommon).
|
||||
// SetOriginal(exception.CommonErrorInvalidInput).
|
||||
// SetError(err).
|
||||
// Build(c)
|
||||
// utils.HttpResponse(c, 400, errorCode)
|
||||
// return
|
||||
// }
|
||||
// userData.Avatar = ReqInfo.Avatar
|
||||
// }
|
||||
|
||||
// if ReqInfo.Bio != "" {
|
||||
// if !cryptography.IsBase64Std(ReqInfo.Bio) {
|
||||
// errorCode := new(exception.Builder).
|
||||
// SetStatus(exception.StatusUser).
|
||||
// SetService(exception.ServiceUser).
|
||||
// SetEndpoint(exception.EndpointUserServiceUpdate).
|
||||
// SetType(exception.TypeCommon).
|
||||
// SetOriginal(exception.CommonErrorInvalidInput).
|
||||
// Build(c)
|
||||
// utils.HttpResponse(c, 400, errorCode)
|
||||
// return
|
||||
// }
|
||||
// userData.Bio = ReqInfo.Bio
|
||||
// }
|
||||
|
||||
// Update user info
|
||||
userData.UpdateByUserID(c, userId)
|
||||
|
||||
errorCode :=
|
||||
Build(c)
|
||||
utils.HttpResponse(c, 200, errorCode)
|
||||
}
|
||||
Reference in New Issue
Block a user