Update Filters, Limit, Search
This commit is contained in:
parent
9b1f58d783
commit
a9c8e6c1a6
@ -20,7 +20,11 @@ func NewCollectionHandler() *CollectionHandler {
|
||||
}
|
||||
|
||||
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 {
|
||||
return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch collections"})
|
||||
}
|
||||
|
||||
@ -20,7 +20,11 @@ func NewColourHandler() *ColourHandler {
|
||||
}
|
||||
|
||||
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 {
|
||||
return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch colours"})
|
||||
}
|
||||
|
||||
@ -20,7 +20,11 @@ func NewCategoryHandler() *CategoryHandler {
|
||||
}
|
||||
|
||||
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 {
|
||||
return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch categories"})
|
||||
}
|
||||
|
||||
@ -21,7 +21,15 @@ func NewProductHandler() *ProductHandler {
|
||||
}
|
||||
|
||||
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 {
|
||||
return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch products"})
|
||||
}
|
||||
|
||||
@ -20,7 +20,11 @@ func NewSizeHandler() *SizeHandler {
|
||||
}
|
||||
|
||||
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 {
|
||||
return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch sizes"})
|
||||
}
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user