Update License
This commit is contained in:
parent
2e1b62e33a
commit
d50cefbc7f
@ -53,7 +53,7 @@ func (r *LicenseRepository) GetAll(ctx context.Context, limit, offset int, statu
|
||||
var licenses []*entity.LicenseGetAll
|
||||
var total int64
|
||||
|
||||
// Define the main query with status calculation
|
||||
// Define the main query with status calculation and days to expire
|
||||
subQuery := r.db.WithContext(ctx).
|
||||
Table("licenses").
|
||||
Select(`licenses.*, partners.name as partner_name,
|
||||
@ -65,21 +65,32 @@ func (r *LicenseRepository) GetAll(ctx context.Context, limit, offset int, statu
|
||||
(GREATEST(licenses.end_date - CURRENT_DATE, 0)) as days_to_expire,
|
||||
users.name as created_by_name`).
|
||||
Joins("LEFT JOIN partners ON licenses.partner_id = partners.id").
|
||||
Joins("LEFT JOIN users ON licenses.created_by = users.id").
|
||||
Limit(limit).
|
||||
Offset(offset)
|
||||
Joins("LEFT JOIN users ON licenses.created_by = users.id")
|
||||
|
||||
// Wrap the main query as a subquery to filter by status
|
||||
// Wrap the main query as a subquery to filter by status and apply pagination
|
||||
query := r.db.Table("(?) as sub", subQuery)
|
||||
if statusFilter != "" {
|
||||
query = query.Where("license_status = ?", statusFilter)
|
||||
}
|
||||
|
||||
if limit > 0 {
|
||||
query = query.Limit(limit)
|
||||
}
|
||||
if offset > 0 {
|
||||
query = query.Offset(offset)
|
||||
}
|
||||
|
||||
if err := query.Find(&licenses).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
if err := r.db.Table("licenses").Count(&total).Error; err != nil {
|
||||
// Get the total count of records matching the filter
|
||||
countQuery := r.db.Table("(?) as sub", subQuery)
|
||||
if statusFilter != "" {
|
||||
countQuery = countQuery.Where("license_status = ?", statusFilter)
|
||||
}
|
||||
|
||||
if err := countQuery.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user