Skip to content

Commit

Permalink
fix(api/comments): counting comments issue in PostgreSQL db (#760)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwqcode committed Feb 3, 2024
1 parent 2a6f0c5 commit 6280433
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions server/handler/comments_get/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 6280433

Please sign in to comment.