usermngmt/model/user_request.go

152 lines
2.8 KiB
Go
Raw Permalink Normal View History

2021-11-10 02:54:49 +00:00
package model
2021-11-10 01:44:22 +00:00
import (
"errors"
2021-12-28 08:37:57 +00:00
"go.mongodb.org/mongo-driver/bson"
2023-04-05 10:54:05 +00:00
"git.selly.red/Selly-Modules/usermngmt/internal"
2021-11-10 01:44:22 +00:00
)
2021-11-10 04:01:39 +00:00
// UserCreateOptions ...
type UserCreateOptions struct {
2021-11-17 06:32:29 +00:00
Name string
Phone string
Email string
Password string
Status string
RoleID string
RequireToChangePassword bool
Other interface{}
2021-11-22 10:35:21 +00:00
Avatar interface{} // if not, pass default file object
2021-11-10 01:44:22 +00:00
}
2021-11-10 04:01:39 +00:00
// UserUpdateOptions ...
type UserUpdateOptions struct {
2021-11-10 01:44:22 +00:00
Name string
Phone string
Email string
RoleID string
2021-11-19 04:17:58 +00:00
Other map[string]interface{}
2021-11-10 01:44:22 +00:00
}
// ChangePasswordOptions ...
type ChangePasswordOptions struct {
OldPassword string
NewPassword string
}
2021-11-10 04:01:39 +00:00
// UserAllQuery ...
type UserAllQuery struct {
2021-11-10 01:44:22 +00:00
Page int64
Limit int64
Keyword string
RoleID string
Status string
2021-11-16 07:26:50 +00:00
Sort interface{}
2021-11-19 04:00:18 +00:00
Other map[string]interface{} // query fields in other object
2021-12-28 08:37:57 +00:00
Cond bson.M
2021-11-19 04:00:18 +00:00
}
2022-03-25 07:16:52 +00:00
// UserByPermissionQuery ...
type UserByPermissionQuery struct {
Page int64
Limit int64
Keyword string
Status string
Permission string // permission code
Sort interface{}
Other map[string]interface{} // query fields in other object
Cond bson.M
}
2021-11-19 04:00:18 +00:00
// UserCountQuery ...
type UserCountQuery struct {
RoleID string
Other map[string]interface{} // query fields in other object
2021-11-10 01:44:22 +00:00
}
// Validate ...
2021-11-10 04:01:39 +00:00
func (co UserCreateOptions) Validate() error {
2021-11-10 01:44:22 +00:00
// Name
if co.Name == "" {
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidName)
2021-11-10 01:44:22 +00:00
}
// Phone
if co.Phone == "" {
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidPhoneNumber)
2021-11-10 01:44:22 +00:00
}
// Email
if co.Email == "" {
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidEmail)
2021-11-10 01:44:22 +00:00
}
// Password
if co.Password == "" {
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidPassword)
2021-11-10 01:44:22 +00:00
}
// Status
if co.Status == "" {
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidStatus)
2021-11-10 01:44:22 +00:00
}
// RoleID
if co.RoleID == "" {
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidRole)
2021-11-10 01:44:22 +00:00
}
return nil
}
// Validate ...
2021-11-10 04:01:39 +00:00
func (uo UserUpdateOptions) Validate() error {
2021-11-10 01:44:22 +00:00
// Name
if uo.Name == "" {
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidName)
2021-11-10 01:44:22 +00:00
}
// Phone
if uo.Phone == "" {
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidPhoneNumber)
2021-11-10 01:44:22 +00:00
}
// Email
if uo.Email == "" {
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidEmail)
2021-11-10 01:44:22 +00:00
}
// RoleID
if uo.RoleID == "" {
2021-12-07 07:28:52 +00:00
return errors.New(internal.ErrorInvalidRole)
2021-11-10 01:44:22 +00:00
}
return nil
}
// Validate ...
func (co ChangePasswordOptions) Validate() error {
// OldPassword, NewPassword
2021-12-07 07:28:52 +00:00
if co.OldPassword == "" {
return errors.New(internal.ErrorInvalidOldPassword)
}
if co.NewPassword == "" {
return errors.New(internal.ErrorInvalidNewPassword)
2021-11-10 01:44:22 +00:00
}
return nil
}
2022-03-25 07:16:52 +00:00
// Validate ...
func (q UserByPermissionQuery) Validate() error {
// OldPassword, NewPassword
if q.Permission == "" {
return errors.New(internal.ErrorInvalidPermission)
}
return nil
}