From 7033a27fc0cfa41279c6db655cfabc26372e8281 Mon Sep 17 00:00:00 2001 From: Hoang Date: Thu, 18 Nov 2021 10:30:12 +0700 Subject: [PATCH] add other query --- model/query.go | 11 +++++++++++ model/user_request.go | 1 + user/handle.go | 2 ++ 3 files changed, 14 insertions(+) diff --git a/model/query.go b/model/query.go index b2af79d..8c01bbb 100644 --- a/model/query.go +++ b/model/query.go @@ -14,6 +14,7 @@ type CommonQuery struct { RoleID string Status string Sort interface{} + Other map[string]interface{} } // AssignKeyword ... @@ -57,3 +58,13 @@ func (q *CommonQuery) SetDefaultLimit() { q.Limit = 20 } } + +// AssignOther ... +func (q *CommonQuery) AssignOther(cond bson.M) { + // Query fields in other object + if len(q.Other) > 0 { + for key, value := range q.Other { + cond["other."+key] = value + } + } +} diff --git a/model/user_request.go b/model/user_request.go index 3c7f3f6..05f36d8 100644 --- a/model/user_request.go +++ b/model/user_request.go @@ -41,6 +41,7 @@ type UserAllQuery struct { RoleID string Status string Sort interface{} + Other map[string]interface{} // query fields in other object } // Validate ... diff --git a/user/handle.go b/user/handle.go index 3d62dca..8fec3c8 100644 --- a/user/handle.go +++ b/user/handle.go @@ -89,6 +89,7 @@ func All(queryParams model.UserAllQuery) (r model.UserAll) { RoleID: queryParams.RoleID, Status: queryParams.Status, Sort: queryParams.Sort, + Other: queryParams.Other, } // Assign condition @@ -96,6 +97,7 @@ func All(queryParams model.UserAllQuery) (r model.UserAll) { query.AssignKeyword(cond) query.AssignRoleID(cond) query.AssignStatus(cond) + query.AssignOther(cond) wg.Add(1) go func() {