add findUserMethod

This commit is contained in:
Hoang 2021-11-19 09:59:33 +07:00
parent 7033a27fc0
commit 14f3f94f98
5 changed files with 38 additions and 5 deletions

View File

@ -19,6 +19,11 @@ func (s Service) CreateUser(payload model.UserCreateOptions) (id string, err err
return user.Create(payload)
}
// FindUser ...
func (s Service) FindUser(userID string) (model.User, error) {
return user.FindUser(userID)
}
// UpdateUser ...
func (s Service) UpdateUser(userID string, payload model.UserUpdateOptions) error {
return user.UpdateByUserID(userID, payload)

View File

@ -8,14 +8,16 @@ import (
// RoleCreateOptions ...
type RoleCreateOptions struct {
Name string
Level int
Name string
Level int
IsAdmin bool
}
// RoleUpdateOptions ...
type RoleUpdateOptions struct {
Name string
Level int
Name string
Level int
IsAdmin bool
}
// RoleAllQuery ...

View File

@ -8,6 +8,7 @@ import (
type RoleShort struct {
ID string `json:"_id"`
Name string `json:"name"`
Level int `json:"level"`
IsAdmin bool `json:"isAdmin"`
}

View File

@ -42,6 +42,7 @@ func newRole(payload model.RoleCreateOptions) model.DBRole {
Name: payload.Name,
Code: internal.GenerateCode(payload.Name),
Level: payload.Level,
IsAdmin: payload.IsAdmin,
CreatedAt: timeNow,
UpdatedAt: timeNow,
}
@ -80,6 +81,7 @@ func Update(roleID string, payload model.RoleUpdateOptions) error {
"name": payload.Name,
"code": internal.GenerateCode(payload.Name),
"level": payload.Level,
"isAdmin": payload.IsAdmin,
"updatedAt": internal.Now(),
},
}

View File

@ -75,6 +75,28 @@ func newUser(payload model.UserCreateOptions) model.DBUser {
}
}
// FindUser ...
func FindUser(userID string) (r model.User, err error) {
var (
ctx = context.Background()
)
// Find user exists or not
id, isValid := mongodb.NewIDFromString(userID)
if !isValid {
err = errors.New("invalid user id data")
return
}
user, _ := findByID(ctx, id)
if user.ID.IsZero() {
err = errors.New("user not found")
return
}
r = getResponse(ctx, user)
return
}
// All ...
func All(queryParams model.UserAllQuery) (r model.UserAll) {
var (
@ -132,6 +154,7 @@ func getResponse(ctx context.Context, user model.DBUser) model.User {
Role: model.RoleShort{
ID: roleRaw.ID.Hex(),
Name: roleRaw.Name,
Level: roleRaw.Level,
IsAdmin: roleRaw.IsAdmin,
},
Other: user.Other,
@ -163,7 +186,7 @@ func UpdateByUserID(userID string, payload model.UserUpdateOptions) error {
// Find user exists or not
id, isValid := mongodb.NewIDFromString(userID)
if !isValid {
return errors.New("invalid role id data")
return errors.New("invalid user id data")
}
user, _ := findByID(ctx, id)
if user.ID.IsZero() {