add pagination approval flows
This commit is contained in:
@@ -66,37 +66,64 @@ func (r *ApprovalFlowRepository) Delete(ctx context.Context, id uuid.UUID) error
|
||||
|
||||
type ListApprovalFlowsFilter struct {
|
||||
DepartmentID *uuid.UUID
|
||||
Search *string
|
||||
IsActive *bool
|
||||
}
|
||||
|
||||
func (r *ApprovalFlowRepository) List(ctx context.Context, filter ListApprovalFlowsFilter, limit, offset int) ([]entities.ApprovalFlow, int64, error) {
|
||||
db := DBFromContext(ctx, r.db)
|
||||
query := db.WithContext(ctx).Model(&entities.ApprovalFlow{})
|
||||
var list []entities.ApprovalFlow
|
||||
var total int64
|
||||
|
||||
// Build base query for counting
|
||||
countQuery := r.db.WithContext(ctx).Model(&entities.ApprovalFlow{})
|
||||
|
||||
if filter.DepartmentID != nil {
|
||||
query = query.Where("department_id = ?", *filter.DepartmentID)
|
||||
}
|
||||
if filter.IsActive != nil {
|
||||
query = query.Where("is_active = ?", *filter.IsActive)
|
||||
countQuery = countQuery.Where("department_id = ?", *filter.DepartmentID)
|
||||
}
|
||||
|
||||
var total int64
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
if filter.IsActive != nil {
|
||||
countQuery = countQuery.Where("is_active = ?", *filter.IsActive)
|
||||
}
|
||||
|
||||
if filter.Search != nil && *filter.Search != "" {
|
||||
like := "%" + *filter.Search + "%"
|
||||
countQuery = countQuery.Where("name ILIKE ? OR description ILIKE ?", like, like)
|
||||
}
|
||||
|
||||
// Get total count
|
||||
if err := countQuery.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// Build query for fetching data - BUAT QUERY BARU DARI AWAL
|
||||
dataQuery := r.db.WithContext(ctx).Model(&entities.ApprovalFlow{})
|
||||
|
||||
var list []entities.ApprovalFlow
|
||||
if err := query.
|
||||
if filter.DepartmentID != nil {
|
||||
dataQuery = dataQuery.Where("department_id = ?", *filter.DepartmentID)
|
||||
}
|
||||
|
||||
if filter.IsActive != nil {
|
||||
dataQuery = dataQuery.Where("is_active = ?", *filter.IsActive)
|
||||
}
|
||||
|
||||
if filter.Search != nil && *filter.Search != "" {
|
||||
like := "%" + *filter.Search + "%"
|
||||
dataQuery = dataQuery.Where("name ILIKE ? OR description ILIKE ?", like, like)
|
||||
}
|
||||
|
||||
// Fetch data with pagination and preloads
|
||||
if err := dataQuery.
|
||||
Order("created_at DESC").
|
||||
Limit(limit).
|
||||
Offset(offset).
|
||||
Preload("Department").
|
||||
Preload("Steps", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Order("step_order ASC, parallel_group ASC")
|
||||
}).
|
||||
Order("created_at DESC").
|
||||
Limit(limit).
|
||||
Offset(offset).
|
||||
Find(&list).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
return list, total, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user