BE-MiniERP/modules/inventory/handler/product_component_handler.go
2025-06-22 23:23:26 +07:00

48 lines
1.3 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 {
result, err := h.Repo.FindAll()
if err != nil {
return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch components"})
}
return c.JSON(result)
}
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"})
}