usermngmt/model/user_request.go

152 lines
2.8 KiB
Go

package model
import (
"errors"
"go.mongodb.org/mongo-driver/bson"
"git.selly.red/Selly-Modules/usermngmt/internal"
)
// UserCreateOptions ...
type UserCreateOptions struct {
Name string
Phone string
Email string
Password string
Status string
RoleID string
RequireToChangePassword bool
Other interface{}
Avatar interface{} // if not, pass default file object
}
// UserUpdateOptions ...
type UserUpdateOptions struct {
Name string
Phone string
Email string
RoleID string
Other map[string]interface{}
}
// ChangePasswordOptions ...
type ChangePasswordOptions struct {
OldPassword string
NewPassword string
}
// UserAllQuery ...
type UserAllQuery struct {
Page int64
Limit int64
Keyword string
RoleID string
Status string
Sort interface{}
Other map[string]interface{} // query fields in other object
Cond bson.M
}
// 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
}
// UserCountQuery ...
type UserCountQuery struct {
RoleID string
Other map[string]interface{} // query fields in other object
}
// Validate ...
func (co UserCreateOptions) Validate() error {
// Name
if co.Name == "" {
return errors.New(internal.ErrorInvalidName)
}
// Phone
if co.Phone == "" {
return errors.New(internal.ErrorInvalidPhoneNumber)
}
// Email
if co.Email == "" {
return errors.New(internal.ErrorInvalidEmail)
}
// Password
if co.Password == "" {
return errors.New(internal.ErrorInvalidPassword)
}
// Status
if co.Status == "" {
return errors.New(internal.ErrorInvalidStatus)
}
// RoleID
if co.RoleID == "" {
return errors.New(internal.ErrorInvalidRole)
}
return nil
}
// Validate ...
func (uo UserUpdateOptions) Validate() error {
// Name
if uo.Name == "" {
return errors.New(internal.ErrorInvalidName)
}
// Phone
if uo.Phone == "" {
return errors.New(internal.ErrorInvalidPhoneNumber)
}
// Email
if uo.Email == "" {
return errors.New(internal.ErrorInvalidEmail)
}
// RoleID
if uo.RoleID == "" {
return errors.New(internal.ErrorInvalidRole)
}
return nil
}
// Validate ...
func (co ChangePasswordOptions) Validate() error {
// OldPassword, NewPassword
if co.OldPassword == "" {
return errors.New(internal.ErrorInvalidOldPassword)
}
if co.NewPassword == "" {
return errors.New(internal.ErrorInvalidNewPassword)
}
return nil
}
// Validate ...
func (q UserByPermissionQuery) Validate() error {
// OldPassword, NewPassword
if q.Permission == "" {
return errors.New(internal.ErrorInvalidPermission)
}
return nil
}