Update Filters, Limit, Search

This commit is contained in:
2025-06-27 14:30:55 +07:00
parent 9b1f58d783
commit a9c8e6c1a6
10 changed files with 105 additions and 16 deletions
@@ -14,9 +14,20 @@ func NewCollectionRepository(db *gorm.DB) *CollectionRepository {
return &CollectionRepository{DB: db}
}
func (r *CollectionRepository) FindAll() ([]models.Collection, error) {
func (r *CollectionRepository) FindAll(limit, offset int, search string) ([]models.Collection, error) {
var collections []models.Collection
err := r.DB.Find(&collections).Error
query := r.DB.Model(&models.Collection{})
if search != "" {
query = query.Where("name ILIKE ? OR code ILIKE ?", "%"+search+"%", "%"+search+"%")
}
err := query.
Limit(limit).
Offset(offset).
Find(&collections).Error
return collections, err
}
+13 -2
View File
@@ -14,9 +14,20 @@ func NewColourRepository(db *gorm.DB) *ColourRepository {
return &ColourRepository{DB: db}
}
func (r *ColourRepository) FindAll() ([]models.Colour, error) {
func (r *ColourRepository) FindAll(limit, offset int, search string) ([]models.Colour, error) {
var colours []models.Colour
err := r.DB.Find(&colours).Error
query := r.DB.Model(&models.Colour{})
if search != "" {
query = query.Where("name ILIKE ? OR code ILIKE ? OR hex ILIKE ?", "%"+search+"%", "%"+search+"%", "%"+search+"%")
}
err := query.
Limit(limit).
Offset(offset).
Find(&colours).Error
return colours, err
}
+26 -3
View File
@@ -16,14 +16,37 @@ func NewProductRepository(db *gorm.DB) *ProductRepository {
return &ProductRepository{DB: db}
}
func (r *ProductRepository) FindAll() ([]models.Product, error) {
func (r *ProductRepository) FindAll(limit, offset int, search string, categoryID, collectionID, colourID, sizeID uint) ([]models.Product, error) {
var products []models.Product
err := r.DB.
query := r.DB.Model(&models.Product{}).
Preload("Category").
Preload("Collection").
Preload("Colour").
Preload("Size").
Preload("Size")
if search != "" {
query = query.Where("name ILIKE ? OR sku ILIKE ?", "%"+search+"%", "%"+search+"%")
}
if categoryID != 0 {
query = query.Where("category_id = ?", categoryID)
}
if collectionID != 0 {
query = query.Where("collection_id = ?", collectionID)
}
if colourID != 0 {
query = query.Where("colour_id = ?", colourID)
}
if sizeID != 0 {
query = query.Where("size_id = ?", sizeID)
}
err := query.
Limit(limit).
Offset(offset).
Find(&products).Error
return products, err
}
@@ -14,9 +14,18 @@ func NewCategoryRepository(db *gorm.DB) *CategoryRepository {
return &CategoryRepository{DB: db}
}
func (r *CategoryRepository) FindAll() ([]models.ProductCategory, error) {
func (r *CategoryRepository) FindAll(limit, offset int, search string) ([]models.ProductCategory, error) {
var categories []models.ProductCategory
err := r.DB.Find(&categories).Error
query := r.DB.Model(&models.ProductCategory{})
if search != "" {
query = query.Where("name ILIKE ? OR code ILIKE", "%"+search+"%", "%"+search+"%")
}
err := query.
Limit(limit).
Offset(offset).
Find(&categories).Error
return categories, err
}
+13 -2
View File
@@ -14,9 +14,20 @@ func NewSizeRepository(db *gorm.DB) *SizeRepository {
return &SizeRepository{DB: db}
}
func (r *SizeRepository) FindAll() ([]models.Size, error) {
func (r *SizeRepository) FindAll(limit, offset int, search string) ([]models.Size, error) {
var sizes []models.Size
err := r.DB.Find(&sizes).Error
query := r.DB.Model(&models.Size{})
if search != "" {
query = query.Where("name ILIKE ? OR code ILIKE ?", "%"+search+"%", "%"+search+"%")
}
err := query.
Limit(limit).
Offset(offset).
Find(&sizes).Error
return sizes, err
}