Update Filters, Limit, Search

This commit is contained in:
ahmadafriadi 2025-06-27 14:30:55 +07:00
parent 9b1f58d783
commit a9c8e6c1a6
10 changed files with 105 additions and 16 deletions

View File

@ -20,7 +20,11 @@ func NewCollectionHandler() *CollectionHandler {
} }
func (h *CollectionHandler) GetAll(c *fiber.Ctx) error { func (h *CollectionHandler) GetAll(c *fiber.Ctx) error {
collections, err := h.Repo.FindAll() limit := c.QueryInt("limit", 10) // default 10
offset := c.QueryInt("offset", 0) // default 0
search := c.Query("search", "")
collections, err := h.Repo.FindAll(limit, offset, search)
if err != nil { if err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch collections"}) return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch collections"})
} }

View File

@ -20,7 +20,11 @@ func NewColourHandler() *ColourHandler {
} }
func (h *ColourHandler) GetAll(c *fiber.Ctx) error { func (h *ColourHandler) GetAll(c *fiber.Ctx) error {
colours, err := h.Repo.FindAll() limit := c.QueryInt("limit", 10) // default 10
offset := c.QueryInt("offset", 0) // default 0
search := c.Query("search", "")
colours, err := h.Repo.FindAll(limit, offset, search)
if err != nil { if err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch colours"}) return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch colours"})
} }

View File

@ -20,7 +20,11 @@ func NewCategoryHandler() *CategoryHandler {
} }
func (h *CategoryHandler) GetAll(c *fiber.Ctx) error { func (h *CategoryHandler) GetAll(c *fiber.Ctx) error {
categories, err := h.Repo.FindAll() limit := c.QueryInt("limit", 10) // default 10
offset := c.QueryInt("offset", 0) // default 0
search := c.Query("search", "")
categories, err := h.Repo.FindAll(limit, offset, search)
if err != nil { if err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch categories"}) return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch categories"})
} }

View File

@ -21,7 +21,15 @@ func NewProductHandler() *ProductHandler {
} }
func (h *ProductHandler) GetAll(c *fiber.Ctx) error { func (h *ProductHandler) GetAll(c *fiber.Ctx) error {
products, err := h.Repo.FindAll() limit := c.QueryInt("limit", 10) // default 10
offset := c.QueryInt("offset", 0) // default 0
search := c.Query("search", "")
categoryID := c.QueryInt("category_id", 0)
collectionID := c.QueryInt("collection_id", 0)
colourID := c.QueryInt("colour_id", 0)
sizeID := c.QueryInt("size_id", 0)
products, err := h.Repo.FindAll(limit, offset, search, uint(categoryID), uint(collectionID), uint(colourID), uint(sizeID))
if err != nil { if err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch products"}) return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch products"})
} }

View File

@ -20,7 +20,11 @@ func NewSizeHandler() *SizeHandler {
} }
func (h *SizeHandler) GetAll(c *fiber.Ctx) error { func (h *SizeHandler) GetAll(c *fiber.Ctx) error {
sizes, err := h.Repo.FindAll() limit := c.QueryInt("limit", 10) // default 10
offset := c.QueryInt("offset", 0) // default 0
search := c.Query("search", "")
sizes, err := h.Repo.FindAll(limit, offset, search)
if err != nil { if err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch sizes"}) return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch sizes"})
} }

View File

@ -14,9 +14,20 @@ func NewCollectionRepository(db *gorm.DB) *CollectionRepository {
return &CollectionRepository{DB: db} 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 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 return collections, err
} }

View File

@ -14,9 +14,20 @@ func NewColourRepository(db *gorm.DB) *ColourRepository {
return &ColourRepository{DB: db} 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 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 return colours, err
} }

View File

@ -16,14 +16,37 @@ func NewProductRepository(db *gorm.DB) *ProductRepository {
return &ProductRepository{DB: db} 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 var products []models.Product
err := r.DB.
query := r.DB.Model(&models.Product{}).
Preload("Category"). Preload("Category").
Preload("Collection"). Preload("Collection").
Preload("Colour"). 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 Find(&products).Error
return products, err return products, err
} }

View File

@ -14,9 +14,18 @@ func NewCategoryRepository(db *gorm.DB) *CategoryRepository {
return &CategoryRepository{DB: db} 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 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 return categories, err
} }

View File

@ -14,9 +14,20 @@ func NewSizeRepository(db *gorm.DB) *SizeRepository {
return &SizeRepository{DB: db} 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 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 return sizes, err
} }