From 88730951021092353496009ec6e9646fc2485233 Mon Sep 17 00:00:00 2001 From: ahmadafriadi Date: Fri, 27 Jun 2025 15:29:08 +0700 Subject: [PATCH] Update Filters, Limit, Search, Page --- .../inventory/handler/collection_handler.go | 7 ++-- modules/inventory/handler/colour_handler.go | 7 ++-- .../handler/product_category_handler.go | 7 ++-- .../handler/product_component_handler.go | 11 ++++-- modules/inventory/handler/product_handler.go | 7 ++-- .../handler/production_order_handler.go | 35 ++++++++++++++++++- modules/inventory/handler/size_handler.go | 7 ++-- .../handler/stock_movement_handler.go | 30 +++++++++++++++- .../inventory/handler/warehouse_handler.go | 7 +++- .../inventory/repository/collection_repo.go | 3 +- modules/inventory/repository/colour__repo.go | 3 +- modules/inventory/repository/product__repo.go | 3 +- .../repository/product_category_repo.go | 3 +- .../repository/product_component__repo.go | 30 ++++++++++++---- .../repository/production_order_repo.go | 33 +++++++++++++++-- modules/inventory/repository/size_repo.go | 3 +- .../repository/stock_movement_repo.go | 30 ++++++++++++++-- .../inventory/repository/warehouse_repo.go | 14 ++++++-- 18 files changed, 202 insertions(+), 38 deletions(-) diff --git a/modules/inventory/handler/collection_handler.go b/modules/inventory/handler/collection_handler.go index 6cffb89..dfc4a06 100644 --- a/modules/inventory/handler/collection_handler.go +++ b/modules/inventory/handler/collection_handler.go @@ -20,11 +20,12 @@ func NewCollectionHandler() *CollectionHandler { } func (h *CollectionHandler) GetAll(c *fiber.Ctx) error { - limit := c.QueryInt("limit", 10) // default 10 - offset := c.QueryInt("offset", 0) // default 0 + page := c.QueryInt("page", 1) + limit := c.QueryInt("limit", 10) // default 10 + offset := (page - 1) * limit search := c.Query("search", "") - collections, err := h.Repo.FindAll(limit, offset, search) + collections, err := h.Repo.FindAll(page, limit, offset, search) if err != nil { return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch collections"}) } diff --git a/modules/inventory/handler/colour_handler.go b/modules/inventory/handler/colour_handler.go index 79e15be..41a991f 100644 --- a/modules/inventory/handler/colour_handler.go +++ b/modules/inventory/handler/colour_handler.go @@ -20,11 +20,12 @@ func NewColourHandler() *ColourHandler { } func (h *ColourHandler) GetAll(c *fiber.Ctx) error { - limit := c.QueryInt("limit", 10) // default 10 - offset := c.QueryInt("offset", 0) // default 0 + page := c.QueryInt("page", 1) + limit := c.QueryInt("limit", 10) // default 10 + offset := (page - 1) * limit search := c.Query("search", "") - colours, err := h.Repo.FindAll(limit, offset, search) + colours, err := h.Repo.FindAll(page, limit, offset, search) if err != nil { return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch colours"}) } diff --git a/modules/inventory/handler/product_category_handler.go b/modules/inventory/handler/product_category_handler.go index 36f7ccd..ce27ee7 100644 --- a/modules/inventory/handler/product_category_handler.go +++ b/modules/inventory/handler/product_category_handler.go @@ -20,11 +20,12 @@ func NewCategoryHandler() *CategoryHandler { } func (h *CategoryHandler) GetAll(c *fiber.Ctx) error { - limit := c.QueryInt("limit", 10) // default 10 - offset := c.QueryInt("offset", 0) // default 0 + page := c.QueryInt("page", 1) + limit := c.QueryInt("limit", 10) // default 10 + offset := (page - 1) * limit search := c.Query("search", "") - categories, err := h.Repo.FindAll(limit, offset, search) + categories, err := h.Repo.FindAll(page, limit, offset, search) if err != nil { return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch categories"}) } diff --git a/modules/inventory/handler/product_component_handler.go b/modules/inventory/handler/product_component_handler.go index a801a5a..96ea531 100644 --- a/modules/inventory/handler/product_component_handler.go +++ b/modules/inventory/handler/product_component_handler.go @@ -20,11 +20,18 @@ func NewProductComponentHandler() *ProductComponentHandler { } func (h *ProductComponentHandler) GetAll(c *fiber.Ctx) error { - result, err := h.Repo.FindAll() + page := c.QueryInt("page", 1) + limit := c.QueryInt("limit", 10) // default 10 + offset := (page - 1) * limit + search := c.Query("search", "") + productID := c.QueryInt("product_id", 0) + componenID := c.QueryInt("componen_id", 0) + + components, err := h.Repo.FindAll(page, limit, offset, search, uint(productID), uint(componenID)) if err != nil { return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch components"}) } - return c.JSON(result) + return c.JSON(components) } func (h *ProductComponentHandler) Create(c *fiber.Ctx) error { diff --git a/modules/inventory/handler/product_handler.go b/modules/inventory/handler/product_handler.go index 0d683fd..6a9049e 100644 --- a/modules/inventory/handler/product_handler.go +++ b/modules/inventory/handler/product_handler.go @@ -21,15 +21,16 @@ func NewProductHandler() *ProductHandler { } func (h *ProductHandler) GetAll(c *fiber.Ctx) error { - limit := c.QueryInt("limit", 10) // default 10 - offset := c.QueryInt("offset", 0) // default 0 + page := c.QueryInt("page", 1) + limit := c.QueryInt("limit", 10) // default 10 + offset := (page - 1) * limit 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)) + products, err := h.Repo.FindAll(page, 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"}) } diff --git a/modules/inventory/handler/production_order_handler.go b/modules/inventory/handler/production_order_handler.go index be829f4..9f9b38d 100644 --- a/modules/inventory/handler/production_order_handler.go +++ b/modules/inventory/handler/production_order_handler.go @@ -5,6 +5,7 @@ import ( "BE-MiniERP/modules/inventory/models" "BE-MiniERP/modules/inventory/repository" "strconv" + "time" "github.com/gofiber/fiber/v2" ) @@ -20,7 +21,39 @@ func NewProductionOrderHandler() *ProductionOrderHandler { } func (h *ProductionOrderHandler) GetAll(c *fiber.Ctx) error { - data, err := h.Repo.FindAll() + page := c.QueryInt("page", 1) + limit := c.QueryInt("limit", 10) // default 10 + offset := (page - 1) * limit + search := c.Query("search", "") + productID := c.QueryInt("product_id", 0) + startDateStr := c.Query("start_date", "") + endDateStr := c.Query("end_date", "") + warehouseID := c.QueryInt("origin_id", 0) + + var startDate, endDate time.Time + var err error + + // Parse start_date + if startDateStr != "" { + startDate, err = time.Parse("2006-01-02", startDateStr) + if err != nil { + return c.Status(400).JSON(fiber.Map{ + "error": "Invalid start_date format. Use YYYY-MM-DD", + }) + } + } + + // Parse end_date + if endDateStr != "" { + endDate, err = time.Parse("2006-01-02", endDateStr) + if err != nil { + return c.Status(400).JSON(fiber.Map{ + "error": "Invalid end_date format. Use YYYY-MM-DD", + }) + } + } + + data, err := h.Repo.FindAll(page, limit, offset, search, uint(productID), uint(warehouseID), startDate, endDate) if err != nil { return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch production orders"}) } diff --git a/modules/inventory/handler/size_handler.go b/modules/inventory/handler/size_handler.go index f49d12c..2b0f83e 100644 --- a/modules/inventory/handler/size_handler.go +++ b/modules/inventory/handler/size_handler.go @@ -20,11 +20,12 @@ func NewSizeHandler() *SizeHandler { } func (h *SizeHandler) GetAll(c *fiber.Ctx) error { - limit := c.QueryInt("limit", 10) // default 10 - offset := c.QueryInt("offset", 0) // default 0 + page := c.QueryInt("page", 1) + limit := c.QueryInt("limit", 10) // default 10 + offset := (page - 1) * limit search := c.Query("search", "") - sizes, err := h.Repo.FindAll(limit, offset, search) + sizes, err := h.Repo.FindAll(page, limit, offset, search) if err != nil { return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch sizes"}) } diff --git a/modules/inventory/handler/stock_movement_handler.go b/modules/inventory/handler/stock_movement_handler.go index e8e4040..9c0ac3d 100644 --- a/modules/inventory/handler/stock_movement_handler.go +++ b/modules/inventory/handler/stock_movement_handler.go @@ -4,6 +4,7 @@ import ( "BE-MiniERP/database" "BE-MiniERP/modules/inventory/models" "BE-MiniERP/modules/inventory/repository" + "time" "github.com/gofiber/fiber/v2" ) @@ -19,7 +20,34 @@ func NewStockMovementHandler() *StockMovementHandler { } func (h *StockMovementHandler) GetAll(c *fiber.Ctx) error { - data, err := h.Repo.FindAll() + page := c.QueryInt("page", 1) + limit := c.QueryInt("limit", 10) // default 10 + offset := (page - 1) * limit + search := c.Query("search", "") + productID := c.QueryInt("product_id", 0) + dateStr := c.Query("date", "") + originID := c.QueryInt("origin_id", 0) + destinationID := c.QueryInt("destination_id", 0) + + var selectedDate time.Time + var err error + + if dateStr != "" { + selectedDate, err = time.Parse("2006-01-02", dateStr) + if err != nil { + return c.Status(400).JSON(fiber.Map{ + "error": "Invalid date format. Use YYYY-MM-DD", + }) + } + } + + data, err := h.Repo.FindAll( + page, limit, offset, + search, + uint(productID), uint(originID), uint(destinationID), + selectedDate, + ) + if err != nil { return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch stock movements"}) } diff --git a/modules/inventory/handler/warehouse_handler.go b/modules/inventory/handler/warehouse_handler.go index 879af0b..19e1960 100644 --- a/modules/inventory/handler/warehouse_handler.go +++ b/modules/inventory/handler/warehouse_handler.go @@ -20,7 +20,12 @@ func NewWarehouseHandler() *WarehouseHandler { } func (h *WarehouseHandler) GetAll(c *fiber.Ctx) error { - warehouses, err := h.Repo.FindAll() + page := c.QueryInt("page", 1) + limit := c.QueryInt("limit", 10) // default 10 + offset := (page - 1) * limit + search := c.Query("search", "") + + warehouses, err := h.Repo.FindAll(page, limit, offset, search) if err != nil { return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch warehouses"}) } diff --git a/modules/inventory/repository/collection_repo.go b/modules/inventory/repository/collection_repo.go index 503d7e9..ce50776 100644 --- a/modules/inventory/repository/collection_repo.go +++ b/modules/inventory/repository/collection_repo.go @@ -14,7 +14,7 @@ func NewCollectionRepository(db *gorm.DB) *CollectionRepository { return &CollectionRepository{DB: db} } -func (r *CollectionRepository) FindAll(limit, offset int, search string) ([]models.Collection, error) { +func (r *CollectionRepository) FindAll(page, limit, offset int, search string) ([]models.Collection, error) { var collections []models.Collection query := r.DB.Model(&models.Collection{}) @@ -24,6 +24,7 @@ func (r *CollectionRepository) FindAll(limit, offset int, search string) ([]mode } err := query. + Order("created_at DESC"). Limit(limit). Offset(offset). Find(&collections).Error diff --git a/modules/inventory/repository/colour__repo.go b/modules/inventory/repository/colour__repo.go index 9f0c4ed..63c7fd3 100644 --- a/modules/inventory/repository/colour__repo.go +++ b/modules/inventory/repository/colour__repo.go @@ -14,7 +14,7 @@ func NewColourRepository(db *gorm.DB) *ColourRepository { return &ColourRepository{DB: db} } -func (r *ColourRepository) FindAll(limit, offset int, search string) ([]models.Colour, error) { +func (r *ColourRepository) FindAll(page, limit, offset int, search string) ([]models.Colour, error) { var colours []models.Colour query := r.DB.Model(&models.Colour{}) @@ -24,6 +24,7 @@ func (r *ColourRepository) FindAll(limit, offset int, search string) ([]models.C } err := query. + Order("created_at DESC"). Limit(limit). Offset(offset). Find(&colours).Error diff --git a/modules/inventory/repository/product__repo.go b/modules/inventory/repository/product__repo.go index e208049..a12225a 100644 --- a/modules/inventory/repository/product__repo.go +++ b/modules/inventory/repository/product__repo.go @@ -16,7 +16,7 @@ func NewProductRepository(db *gorm.DB) *ProductRepository { return &ProductRepository{DB: db} } -func (r *ProductRepository) FindAll(limit, offset int, search string, categoryID, collectionID, colourID, sizeID uint) ([]models.Product, error) { +func (r *ProductRepository) FindAll(page, limit, offset int, search string, categoryID, collectionID, colourID, sizeID uint) ([]models.Product, error) { var products []models.Product query := r.DB.Model(&models.Product{}). @@ -43,6 +43,7 @@ func (r *ProductRepository) FindAll(limit, offset int, search string, categoryID } err := query. + Order("created_at DESC"). Limit(limit). Offset(offset). Find(&products).Error diff --git a/modules/inventory/repository/product_category_repo.go b/modules/inventory/repository/product_category_repo.go index db7ef97..9531ddf 100644 --- a/modules/inventory/repository/product_category_repo.go +++ b/modules/inventory/repository/product_category_repo.go @@ -14,7 +14,7 @@ func NewCategoryRepository(db *gorm.DB) *CategoryRepository { return &CategoryRepository{DB: db} } -func (r *CategoryRepository) FindAll(limit, offset int, search string) ([]models.ProductCategory, error) { +func (r *CategoryRepository) FindAll(page, limit, offset int, search string) ([]models.ProductCategory, error) { var categories []models.ProductCategory query := r.DB.Model(&models.ProductCategory{}) @@ -23,6 +23,7 @@ func (r *CategoryRepository) FindAll(limit, offset int, search string) ([]models } err := query. + Order("created_at DESC"). Limit(limit). Offset(offset). Find(&categories).Error diff --git a/modules/inventory/repository/product_component__repo.go b/modules/inventory/repository/product_component__repo.go index a7ed63c..c831b64 100644 --- a/modules/inventory/repository/product_component__repo.go +++ b/modules/inventory/repository/product_component__repo.go @@ -14,13 +14,31 @@ func NewProductComponentRepository(db *gorm.DB) *ProductComponentRepository { return &ProductComponentRepository{DB: db} } -func (r *ProductComponentRepository) FindAll() ([]models.ProductComponent, error) { - var data []models.ProductComponent - err := r.DB. +func (r *ProductComponentRepository) FindAll(page, limit, offset int, search string, productID, componenID uint) ([]models.ProductComponent, error) { + var components []models.ProductComponent + + query := r.DB.Model(&models.ProductComponent{}). Preload("Product"). - Preload("Component"). - Find(&data).Error - return data, err + Preload("Component") + + if search != "" { + query = query.Where("product_id ILIKE ? OR componen_id ILIKE ?", "%"+search+"%", "%"+search+"%") + } + + if productID != 0 { + query = query.Where("product_id = ?", productID) + } + if componenID != 0 { + query = query.Where("componen_id = ?", componenID) + } + + err := query. + Order("created_at DESC"). + Limit(limit). + Offset(offset). + Find(&components).Error + + return components, err } func (r *ProductComponentRepository) Create(pc *models.ProductComponent) error { diff --git a/modules/inventory/repository/production_order_repo.go b/modules/inventory/repository/production_order_repo.go index 592ff20..876b36c 100644 --- a/modules/inventory/repository/production_order_repo.go +++ b/modules/inventory/repository/production_order_repo.go @@ -2,6 +2,7 @@ package repository import ( "BE-MiniERP/modules/inventory/models" + "time" "gorm.io/gorm" ) @@ -14,9 +15,37 @@ func NewProductionOrderRepository(db *gorm.DB) *ProductionOrderRepository { return &ProductionOrderRepository{DB: db} } -func (r *ProductionOrderRepository) FindAll() ([]models.ProductionOrder, error) { +func (r *ProductionOrderRepository) FindAll(page, limit, offset int, search string, productID, warehouseID uint, startDate, endDate time.Time) ([]models.ProductionOrder, error) { var orders []models.ProductionOrder - err := r.DB.Preload("Product").Find(&orders).Error + + query := r.DB.Model(&models.ProductionOrder{}). + Preload("Product"). + Preload("OriginWarehouse") + + if search != "" { + query = query.Where("status ILIKE ?", "%"+search+"%") + } + + if productID != 0 { + query = query.Where("product_id = ?", productID) + } + + if warehouseID != 0 { + query = query.Where("origin_warehouse_id = ?", warehouseID) + } + + if !startDate.IsZero() { + query = query.Where("created_at >= ?", startDate) + } + if !endDate.IsZero() { + query = query.Where("created_at <= ?", endDate) + } + err := query. + Order("created_at DESC"). + Limit(limit). + Offset(offset). + Find(&orders).Error + return orders, err } diff --git a/modules/inventory/repository/size_repo.go b/modules/inventory/repository/size_repo.go index 76fb6b1..53ac8fa 100644 --- a/modules/inventory/repository/size_repo.go +++ b/modules/inventory/repository/size_repo.go @@ -14,7 +14,7 @@ func NewSizeRepository(db *gorm.DB) *SizeRepository { return &SizeRepository{DB: db} } -func (r *SizeRepository) FindAll(limit, offset int, search string) ([]models.Size, error) { +func (r *SizeRepository) FindAll(page, limit, offset int, search string) ([]models.Size, error) { var sizes []models.Size query := r.DB.Model(&models.Size{}) @@ -24,6 +24,7 @@ func (r *SizeRepository) FindAll(limit, offset int, search string) ([]models.Siz } err := query. + Order("created_at DESC"). Limit(limit). Offset(offset). Find(&sizes).Error diff --git a/modules/inventory/repository/stock_movement_repo.go b/modules/inventory/repository/stock_movement_repo.go index accb1d5..2c1763c 100644 --- a/modules/inventory/repository/stock_movement_repo.go +++ b/modules/inventory/repository/stock_movement_repo.go @@ -2,6 +2,7 @@ package repository import ( "BE-MiniERP/modules/inventory/models" + "time" "gorm.io/gorm" ) @@ -14,12 +15,35 @@ func NewStockMovementRepository(db *gorm.DB) *StockMovementRepository { return &StockMovementRepository{DB: db} } -func (r *StockMovementRepository) FindAll() ([]models.StockMovement, error) { +func (r *StockMovementRepository) FindAll(page, limit, offset int, search string, productID, originID, destinationID uint, selectedDate time.Time) ([]models.StockMovement, error) { var movements []models.StockMovement - err := r.DB. + query := r.DB.Model(&models.StockMovement{}). Preload("Product"). Preload("OriginWarehouse"). - Preload("DestinationWarehouse"). + Preload("DestinationWarehouse") + + if search != "" { + query = query.Where("stock_type ILIKE ?", "%"+search+"%") + } + + if productID != 0 { + query = query.Where("product_id = ?", productID) + } + if originID != 0 { + query = query.Where("origin_warehouse_id = ?", originID) + } + if destinationID != 0 { + query = query.Where("destination_warehouse_id = ?", destinationID) + } + + if !selectedDate.IsZero() { + query = query.Where("DATE(created_at) = ?", selectedDate.Format("2006-01-02")) + } + + err := query. + Order("created_at DESC"). + Limit(limit). + Offset(offset). Find(&movements).Error return movements, err } diff --git a/modules/inventory/repository/warehouse_repo.go b/modules/inventory/repository/warehouse_repo.go index ed3949d..171a561 100644 --- a/modules/inventory/repository/warehouse_repo.go +++ b/modules/inventory/repository/warehouse_repo.go @@ -14,9 +14,19 @@ func NewWarehouseRepository(db *gorm.DB) *WarehouseRepository { return &WarehouseRepository{DB: db} } -func (r *WarehouseRepository) FindAll() ([]models.Warehouse, error) { +func (r *WarehouseRepository) FindAll(page, limit, offset int, search string) ([]models.Warehouse, error) { var warehouses []models.Warehouse - err := r.DB.Find(&warehouses).Error + query := r.DB.Model(&models.Warehouse{}) + + if search != "" { + query = query.Where("name ILIKE ? OR kode ILIKE ? OR stackholder ILIKE ? OR number ILIKE ?", "%"+search+"%", "%"+search+"%", "%"+search+"%", "%"+search+"%") + } + + err := query. + Order("created_at DESC"). + Limit(limit). + Offset(offset). + Find(&warehouses).Error return warehouses, err }