Update Filters, Limit, Search, Page

This commit is contained in:
ahmadafriadi 2025-06-27 15:29:08 +07:00
parent a9c8e6c1a6
commit 8873095102
18 changed files with 202 additions and 38 deletions

View File

@ -20,11 +20,12 @@ func NewCollectionHandler() *CollectionHandler {
}
func (h *CollectionHandler) GetAll(c *fiber.Ctx) error {
page := c.QueryInt("page", 1)
limit := c.QueryInt("limit", 10) // default 10
offset := c.QueryInt("offset", 0) // default 0
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"})
}

View File

@ -20,11 +20,12 @@ func NewColourHandler() *ColourHandler {
}
func (h *ColourHandler) GetAll(c *fiber.Ctx) error {
page := c.QueryInt("page", 1)
limit := c.QueryInt("limit", 10) // default 10
offset := c.QueryInt("offset", 0) // default 0
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"})
}

View File

@ -20,11 +20,12 @@ func NewCategoryHandler() *CategoryHandler {
}
func (h *CategoryHandler) GetAll(c *fiber.Ctx) error {
page := c.QueryInt("page", 1)
limit := c.QueryInt("limit", 10) // default 10
offset := c.QueryInt("offset", 0) // default 0
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"})
}

View File

@ -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 {

View File

@ -21,15 +21,16 @@ func NewProductHandler() *ProductHandler {
}
func (h *ProductHandler) GetAll(c *fiber.Ctx) error {
page := c.QueryInt("page", 1)
limit := c.QueryInt("limit", 10) // default 10
offset := c.QueryInt("offset", 0) // default 0
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"})
}

View File

@ -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"})
}

View File

@ -20,11 +20,12 @@ func NewSizeHandler() *SizeHandler {
}
func (h *SizeHandler) GetAll(c *fiber.Ctx) error {
page := c.QueryInt("page", 1)
limit := c.QueryInt("limit", 10) // default 10
offset := c.QueryInt("offset", 0) // default 0
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"})
}

View File

@ -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"})
}

View File

@ -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"})
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 {

View File

@ -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
}

View File

@ -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

View File

@ -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
}

View File

@ -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
}