BE-MiniERP/modules/inventory/handler/product_component_handler.go

55 lines
1.6 KiB
Go

package handler
import (
"BE-MiniERP/database"
"BE-MiniERP/modules/inventory/models"
"BE-MiniERP/modules/inventory/repository"
"strconv"
"github.com/gofiber/fiber/v2"
)
type ProductComponentHandler struct {
Repo *repository.ProductComponentRepository
}
func NewProductComponentHandler() *ProductComponentHandler {
return &ProductComponentHandler{
Repo: repository.NewProductComponentRepository(database.DB),
}
}
func (h *ProductComponentHandler) GetAll(c *fiber.Ctx) error {
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(components)
}
func (h *ProductComponentHandler) Create(c *fiber.Ctx) error {
var input models.ProductComponent
if err := c.BodyParser(&input); err != nil {
return c.Status(400).JSON(fiber.Map{"error": "Invalid input"})
}
if err := h.Repo.Create(&input); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to create component entry"})
}
return c.JSON(input)
}
func (h *ProductComponentHandler) Delete(c *fiber.Ctx) error {
id, _ := strconv.Atoi(c.Params("id"))
if err := h.Repo.Delete(uint(id)); err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to delete component entry"})
}
return c.JSON(fiber.Map{"message": "Deleted successfully"})
}