From 14f3f94f980752f4d2569e0ab560469fbb75128b Mon Sep 17 00:00:00 2001 From: Hoang Date: Fri, 19 Nov 2021 09:59:33 +0700 Subject: [PATCH] add findUserMethod --- action.go | 5 +++++ model/role_request.go | 10 ++++++---- model/role_response.go | 1 + role/handle.go | 2 ++ user/handle.go | 25 ++++++++++++++++++++++++- 5 files changed, 38 insertions(+), 5 deletions(-) diff --git a/action.go b/action.go index 3b50fdd..6d483d3 100644 --- a/action.go +++ b/action.go @@ -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) diff --git a/model/role_request.go b/model/role_request.go index a1d97a6..2aacc70 100644 --- a/model/role_request.go +++ b/model/role_request.go @@ -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 ... diff --git a/model/role_response.go b/model/role_response.go index 4223479..aee2f59 100644 --- a/model/role_response.go +++ b/model/role_response.go @@ -8,6 +8,7 @@ import ( type RoleShort struct { ID string `json:"_id"` Name string `json:"name"` + Level int `json:"level"` IsAdmin bool `json:"isAdmin"` } diff --git a/role/handle.go b/role/handle.go index 79923d3..87d8d78 100644 --- a/role/handle.go +++ b/role/handle.go @@ -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(), }, } diff --git a/user/handle.go b/user/handle.go index 8fec3c8..e874e52 100644 --- a/user/handle.go +++ b/user/handle.go @@ -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() {