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) return user.Create(payload)
} }
// FindUser ...
func (s Service) FindUser(userID string) (model.User, error) {
return user.FindUser(userID)
}
// UpdateUser ... // UpdateUser ...
func (s Service) UpdateUser(userID string, payload model.UserUpdateOptions) error { func (s Service) UpdateUser(userID string, payload model.UserUpdateOptions) error {
return user.UpdateByUserID(userID, payload) return user.UpdateByUserID(userID, payload)

View File

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

View File

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

View File

@ -42,6 +42,7 @@ func newRole(payload model.RoleCreateOptions) model.DBRole {
Name: payload.Name, Name: payload.Name,
Code: internal.GenerateCode(payload.Name), Code: internal.GenerateCode(payload.Name),
Level: payload.Level, Level: payload.Level,
IsAdmin: payload.IsAdmin,
CreatedAt: timeNow, CreatedAt: timeNow,
UpdatedAt: timeNow, UpdatedAt: timeNow,
} }
@ -80,6 +81,7 @@ func Update(roleID string, payload model.RoleUpdateOptions) error {
"name": payload.Name, "name": payload.Name,
"code": internal.GenerateCode(payload.Name), "code": internal.GenerateCode(payload.Name),
"level": payload.Level, "level": payload.Level,
"isAdmin": payload.IsAdmin,
"updatedAt": internal.Now(), "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 ... // All ...
func All(queryParams model.UserAllQuery) (r model.UserAll) { func All(queryParams model.UserAllQuery) (r model.UserAll) {
var ( var (
@ -132,6 +154,7 @@ func getResponse(ctx context.Context, user model.DBUser) model.User {
Role: model.RoleShort{ Role: model.RoleShort{
ID: roleRaw.ID.Hex(), ID: roleRaw.ID.Hex(),
Name: roleRaw.Name, Name: roleRaw.Name,
Level: roleRaw.Level,
IsAdmin: roleRaw.IsAdmin, IsAdmin: roleRaw.IsAdmin,
}, },
Other: user.Other, Other: user.Other,
@ -163,7 +186,7 @@ func UpdateByUserID(userID string, payload model.UserUpdateOptions) error {
// Find user exists or not // Find user exists or not
id, isValid := mongodb.NewIDFromString(userID) id, isValid := mongodb.NewIDFromString(userID)
if !isValid { if !isValid {
return errors.New("invalid role id data") return errors.New("invalid user id data")
} }
user, _ := findByID(ctx, id) user, _ := findByID(ctx, id)
if user.ID.IsZero() { if user.ID.IsZero() {