Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions backend/modules/alerts/repository/alert_tag_pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package repository
import (
"context"
"errors"
"fmt"
"strings"

"github.com/google/uuid"
Expand Down Expand Up @@ -142,21 +141,22 @@ func (r *pgAlertTagRuleRepository) List(ctx context.Context, f dto.AlertTagRuleF

where, args := buildTagRuleWhere(f)

table := domain.AlertTagRule{}.TableName()

countSQL := fmt.Sprintf(`SELECT COUNT(*) FROM %s WHERE %s`, table, where)
q := r.db.WithContext(ctx).Model(&domain.AlertTagRule{})

q.Where(where,args)

var total int64
if err := r.db.WithContext(ctx).Raw(countSQL, args...).Scan(&total).Error; err != nil {
if err := q.Count(&total).Error; err != nil {
return nil, 0, err
}

offset := (f.Page - 1) * f.Size
dataSQL := fmt.Sprintf(
`SELECT * FROM %s WHERE %s ORDER BY id ASC LIMIT %d OFFSET %d`,
table, where, f.Size, offset,
)
var rows []domain.AlertTagRule
if err := r.db.WithContext(ctx).Raw(dataSQL, args...).Scan(&rows).Error; err != nil {
if err := q.
Order("id ASC").
Offset((f.Page - 1) * f.Size).
Limit(f.Size).
Find(&rows).Error; err != nil {
return nil, 0, err
}
return rows, total, nil
Expand Down
Loading