From 14ad6b51d812bf52085534c070293e7b8d64fd19 Mon Sep 17 00:00:00 2001 From: Hoang Date: Fri, 19 Nov 2021 11:00:18 +0700 Subject: [PATCH] add count method --- action.go | 5 +++++ model/user_request.go | 8 +++++++- user/handle.go | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/action.go b/action.go index 6d483d3..5eb61ba 100644 --- a/action.go +++ b/action.go @@ -44,6 +44,11 @@ func (s Service) GetAllUsers(query model.UserAllQuery) model.UserAll { return user.All(query) } +// CountAllUsers ... +func (s Service) CountAllUsers(query model.UserCountQuery) int64 { + return user.Count(query) +} + // ChangeAllUsersStatus ... func (s Service) ChangeAllUsersStatus(roleID, status string) error { return user.ChangeAllUsersStatus(roleID, status) diff --git a/model/user_request.go b/model/user_request.go index 05f36d8..19037cf 100644 --- a/model/user_request.go +++ b/model/user_request.go @@ -41,7 +41,13 @@ type UserAllQuery struct { RoleID string Status string Sort interface{} - Other map[string]interface{} // query fields in other object + Other map[string]interface{} // query fields in other object +} + +// UserCountQuery ... +type UserCountQuery struct { + RoleID string + Other map[string]interface{} // query fields in other object } // Validate ... diff --git a/user/handle.go b/user/handle.go index e874e52..01959a3 100644 --- a/user/handle.go +++ b/user/handle.go @@ -143,6 +143,24 @@ func All(queryParams model.UserAllQuery) (r model.UserAll) { return } +// Count ... +func Count(queryParams model.UserCountQuery) int64 { + var ( + ctx = context.Background() + cond = bson.M{} + ) + query := model.CommonQuery{ + RoleID: queryParams.RoleID, + Other: queryParams.Other, + } + + // Assign condition + query.AssignRoleID(cond) + query.AssignOther(cond) + + return countByCondition(ctx, cond) +} + func getResponse(ctx context.Context, user model.DBUser) model.User { roleRaw, _ := roleFindByID(ctx, user.RoleID) return model.User{