feat: profit sharing
This commit is contained in:
@@ -101,6 +101,8 @@ func (r *ProductRepositoryImpl) List(ctx context.Context, filters map[string]int
|
||||
query = query.Where("price >= ?", value)
|
||||
case "price_max":
|
||||
query = query.Where("price <= ?", value)
|
||||
case "category_id":
|
||||
query = query.Where("category_id IN (?)", r.categoryAndChildrenIDs(value))
|
||||
default:
|
||||
query = query.Where(key+" = ?", value)
|
||||
}
|
||||
@@ -114,6 +116,15 @@ func (r *ProductRepositoryImpl) List(ctx context.Context, filters map[string]int
|
||||
return products, total, err
|
||||
}
|
||||
|
||||
// categoryAndChildrenIDs builds a subquery resolving to the category itself plus its
|
||||
// direct children, so filtering by a parent category also returns the children's
|
||||
// products. For a category without children it resolves to just that category.
|
||||
func (r *ProductRepositoryImpl) categoryAndChildrenIDs(categoryID interface{}) *gorm.DB {
|
||||
return r.db.Model(&entities.Category{}).
|
||||
Select("id").
|
||||
Where("id = ? OR parent_id = ?", categoryID, categoryID)
|
||||
}
|
||||
|
||||
func (r *ProductRepositoryImpl) Count(ctx context.Context, filters map[string]interface{}) (int64, error) {
|
||||
var count int64
|
||||
query := r.db.WithContext(ctx).Model(&entities.Product{})
|
||||
@@ -127,6 +138,8 @@ func (r *ProductRepositoryImpl) Count(ctx context.Context, filters map[string]in
|
||||
query = query.Where("price >= ?", value)
|
||||
case "price_max":
|
||||
query = query.Where("price <= ?", value)
|
||||
case "category_id":
|
||||
query = query.Where("category_id IN (?)", r.categoryAndChildrenIDs(value))
|
||||
default:
|
||||
query = query.Where(key+" = ?", value)
|
||||
}
|
||||
@@ -232,6 +245,8 @@ func (r *ProductRepositoryImpl) ListWithOutletPrice(ctx context.Context, filters
|
||||
query = query.Where("products.price >= ?", value)
|
||||
case "price_max":
|
||||
query = query.Where("products.price <= ?", value)
|
||||
case "category_id":
|
||||
query = query.Where("products.category_id IN (?)", r.categoryAndChildrenIDs(value))
|
||||
default:
|
||||
query = query.Where("products."+key+" = ?", value)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user