From 6280433307a2804deb97d0d5b3aae9fefa33c9c9 Mon Sep 17 00:00:00 2001 From: qwqcode Date: Sat, 3 Feb 2024 15:31:03 +0800 Subject: [PATCH] fix(api/comments): counting comments issue in PostgreSQL db (#760) --- server/handler/comments_get/base.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/server/handler/comments_get/base.go b/server/handler/comments_get/base.go index f2447c069..dffa4da20 100644 --- a/server/handler/comments_get/base.go +++ b/server/handler/comments_get/base.go @@ -29,6 +29,11 @@ type QueryOptions struct { } // Get query scope by params +// +// Please be aware that only `WHERE` conditions are permissible in this function. +// For `ORDER BY`, `LIMIT`, and `OFFSET`, please utilize separate functions, as this +// function is invoked in both `Find` and `Count`. `ORDER BY`, `LIMIT`, and `OFFSET` cannot +// be employed within `Count`. func GetQueryScopes(dao *dao.Dao, opts QueryOptions) func(*gorm.DB) *gorm.DB { return func(q *gorm.DB) *gorm.DB { // Basic scope @@ -39,9 +44,6 @@ func GetQueryScopes(dao *dao.Dao, opts QueryOptions) func(*gorm.DB) *gorm.DB { q.Scopes(SearchScope(dao, opts.Search)) } - // Sort by - q.Order(GetSortSQL(opts.Scope, opts.SortBy)) - // Scopes scopes := map[Scope]func(*gorm.DB) *gorm.DB{ ScopePage: PageScopeQuery(opts.PagePayload, PageScopeOpts{ @@ -79,6 +81,9 @@ func FindComments(dao *dao.Dao, opts QueryOptions, pg FindOptions) []entity.Comm q := dao.DB().Model(&entity.Comment{}). Scopes(GetQueryScopes(dao, opts)) + // Sort Rule + q.Order(GetSortSQL(opts.Scope, opts.SortBy)) + q.Offset(pg.Offset). Limit(pg.Limit)