feat(category): update list filter category
This commit is contained in:
@@ -63,6 +63,21 @@ func (r *CategoryRepositoryImpl) Delete(ctx context.Context, id uuid.UUID) error
|
||||
return r.db.WithContext(ctx).Delete(&entities.Category{}, "id = ?", id).Error
|
||||
}
|
||||
|
||||
// applyCategoryTypeFilter narrows the query by position in the category tree.
|
||||
// - "parent": top level categories only (no parent of their own)
|
||||
// - "child": leaf categories — sub categories plus top level categories that
|
||||
// have no sub categories, i.e. everything a product can be assigned to
|
||||
func applyCategoryTypeFilter(query *gorm.DB, value interface{}) *gorm.DB {
|
||||
switch value {
|
||||
case "parent":
|
||||
return query.Where("parent_id IS NULL")
|
||||
case "child":
|
||||
return query.Where("NOT EXISTS (SELECT 1 FROM categories AS sub WHERE sub.parent_id = categories.id)")
|
||||
default:
|
||||
return query
|
||||
}
|
||||
}
|
||||
|
||||
func (r *CategoryRepositoryImpl) List(ctx context.Context, filters map[string]interface{}, limit, offset int) ([]*entities.Category, int64, error) {
|
||||
var categories []*entities.Category
|
||||
var total int64
|
||||
@@ -77,6 +92,8 @@ func (r *CategoryRepositoryImpl) List(ctx context.Context, filters map[string]in
|
||||
case "outlet_id":
|
||||
// Include outlet-specific categories AND global categories (outlet_id IS NULL)
|
||||
query = query.Where("outlet_id = ? OR outlet_id IS NULL", value)
|
||||
case "type":
|
||||
query = applyCategoryTypeFilter(query, value)
|
||||
default:
|
||||
query = query.Where(key+" = ?", value)
|
||||
}
|
||||
@@ -101,6 +118,8 @@ func (r *CategoryRepositoryImpl) Count(ctx context.Context, filters map[string]i
|
||||
query = query.Where("name ILIKE ? OR description ILIKE ?", searchValue, searchValue)
|
||||
case "outlet_id":
|
||||
query = query.Where("outlet_id = ? OR outlet_id IS NULL", value)
|
||||
case "type":
|
||||
query = applyCategoryTypeFilter(query, value)
|
||||
default:
|
||||
query = query.Where(key+" = ?", value)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user