answer/internal/schema/backyard_user_schema.go

183 lines
5.8 KiB
Go
Raw Normal View History

2022-09-27 17:59:05 +08:00
package schema
2023-08-14 12:13:28 +08:00
import (
"context"
"github.com/answerdev/answer/internal/base/constant"
2023-08-14 15:46:13 +08:00
"github.com/answerdev/answer/internal/base/handler"
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/base/translator"
"github.com/answerdev/answer/internal/base/validator"
"github.com/segmentfault/pacman/errors"
2023-08-14 12:13:28 +08:00
"strings"
)
2023-08-04 15:47:50 +08:00
2022-09-27 17:59:05 +08:00
// UpdateUserStatusReq update user request
type UpdateUserStatusReq struct {
UserID string `validate:"required" json:"user_id"`
Status string `validate:"required,oneof=normal suspended deleted inactive" json:"status" enums:"normal,suspended,deleted,inactive"`
LoginUserID string `json:"-"`
2022-09-27 17:59:05 +08:00
}
2023-08-04 15:47:50 +08:00
func (r *UpdateUserStatusReq) IsNormal() bool { return r.Status == constant.UserNormal }
func (r *UpdateUserStatusReq) IsSuspended() bool { return r.Status == constant.UserSuspended }
func (r *UpdateUserStatusReq) IsDeleted() bool { return r.Status == constant.UserDeleted }
func (r *UpdateUserStatusReq) IsInactive() bool { return r.Status == constant.UserInactive }
2022-09-27 17:59:05 +08:00
// GetUserPageReq get user list page request
type GetUserPageReq struct {
// page
Page int `validate:"omitempty,min=1" form:"page"`
// page size
PageSize int `validate:"omitempty,min=1" form:"page_size"`
// email
Query string `validate:"omitempty,gt=0,lte=100" form:"query"`
2022-09-27 17:59:05 +08:00
// user status
Status string `validate:"omitempty,oneof=suspended deleted inactive" form:"status"`
2022-11-29 15:10:57 +08:00
// staff, if staff is true means query admin or moderator
Staff bool `validate:"omitempty" form:"staff"`
2022-09-27 17:59:05 +08:00
}
2023-08-04 15:47:50 +08:00
func (r *GetUserPageReq) IsSuspended() bool { return r.Status == constant.UserSuspended }
func (r *GetUserPageReq) IsDeleted() bool { return r.Status == constant.UserDeleted }
func (r *GetUserPageReq) IsInactive() bool { return r.Status == constant.UserInactive }
2022-09-27 17:59:05 +08:00
// GetUserPageResp get user response
type GetUserPageResp struct {
// user id
UserID string `json:"user_id"`
// create time
CreatedAt int64 `json:"created_at"`
// delete time
DeletedAt int64 `json:"deleted_at"`
// suspended time
SuspendedAt int64 `json:"suspended_at"`
// username
Username string `json:"username"`
// email
EMail string `json:"e_mail"`
// rank
Rank int `json:"rank"`
// user status(normal,suspended,deleted,inactive)
Status string `json:"status"`
// display name
DisplayName string `json:"display_name"`
// avatar
Avatar string `json:"avatar"`
2022-11-29 15:10:57 +08:00
// role id
RoleID int `json:"role_id"`
// role name
RoleName string `json:"role_name"`
2022-09-27 17:59:05 +08:00
}
// GetUserInfoReq get user request
type GetUserInfoReq struct {
UserID string `validate:"required" json:"user_id"`
}
// GetUserInfoResp get user response
type GetUserInfoResp struct {
}
2022-11-29 15:10:57 +08:00
// UpdateUserRoleReq update user role request
type UpdateUserRoleReq struct {
// user id
UserID string `validate:"required" json:"user_id"`
// role id
RoleID int `validate:"required" json:"role_id"`
// login user id
LoginUserID string `json:"-"`
}
// AddUserReq add user request
type AddUserReq struct {
DisplayName string `validate:"required,gte=4,lte=30" json:"display_name"`
Email string `validate:"required,email,gt=0,lte=500" json:"email"`
Password string `validate:"required,gte=8,lte=32" json:"password"`
LoginUserID string `json:"-"`
}
2023-08-14 12:13:28 +08:00
// AddUsersReq add users request
type AddUsersReq struct {
// users info line by line
UsersStr string `json:"users"`
Users []*AddUserReq `json:"-"`
}
2023-08-14 15:46:13 +08:00
type AddUsersErrorData struct {
// optional. error field name.
Field string `json:"field"`
// must. error line number.
Line int `json:"line"`
// must. error content.
Content string `json:"content"`
// optional. error message.
ExtraMessage string `json:"extra_message"`
}
func (e *AddUsersErrorData) GetErrField(ctx context.Context) (errFields []*validator.FormErrorField) {
return append([]*validator.FormErrorField{}, &validator.FormErrorField{
ErrorField: "users",
ErrorMsg: translator.TrWithData(handler.GetLangByCtx(ctx), reason.AddBulkUsersFormatError, e),
})
2023-08-14 15:46:13 +08:00
}
func (req *AddUsersReq) ParseUsers(ctx context.Context) (errFields []*validator.FormErrorField, err error) {
2023-08-14 12:13:28 +08:00
req.UsersStr = strings.TrimSpace(req.UsersStr)
lines := strings.Split(req.UsersStr, "\n")
req.Users = make([]*AddUserReq, 0)
for i, line := range lines {
arr := strings.Split(line, ",")
if len(arr) != 3 {
2023-08-14 15:46:13 +08:00
errFields = append([]*validator.FormErrorField{}, &validator.FormErrorField{
ErrorField: "users",
ErrorMsg: translator.TrWithData(handler.GetLangByCtx(ctx), reason.AddBulkUsersFormatError,
&AddUsersErrorData{
Line: i + 1,
Content: line,
}),
})
return errFields, errors.BadRequest(reason.RequestFormatError)
2023-08-14 12:13:28 +08:00
}
req.Users = append(req.Users, &AddUserReq{
2023-08-14 15:46:13 +08:00
DisplayName: strings.TrimSpace(arr[0]),
Email: strings.TrimSpace(arr[1]),
Password: strings.TrimSpace(arr[2]),
})
}
// check users amount
if len(req.Users) <= 0 || len(req.Users) > constant.DefaultBulkUser {
errFields = append([]*validator.FormErrorField{}, &validator.FormErrorField{
ErrorField: "users",
ErrorMsg: translator.TrWithData(handler.GetLangByCtx(ctx), reason.AddBulkUsersAmountError,
map[string]int{
"MaxAmount": constant.DefaultBulkUser,
}),
2023-08-14 12:13:28 +08:00
})
2023-08-14 15:46:13 +08:00
return errFields, errors.BadRequest(reason.RequestFormatError)
2023-08-14 12:13:28 +08:00
}
2023-08-14 15:46:13 +08:00
return nil, nil
2023-08-14 12:13:28 +08:00
}
// UpdateUserPasswordReq update user password request
type UpdateUserPasswordReq struct {
UserID string `validate:"required" json:"user_id"`
Password string `validate:"required,gte=8,lte=32" json:"password"`
LoginUserID string `json:"-"`
}
// GetUserActivationReq get user activation
type GetUserActivationReq struct {
UserID string `validate:"required" form:"user_id"`
}
// GetUserActivationResp get user activation
type GetUserActivationResp struct {
ActivationURL string `json:"activation_url"`
}
// SendUserActivationReq send user activation
type SendUserActivationReq struct {
UserID string `validate:"required" json:"user_id"`
}