65 lines
1.8 KiB
Go
65 lines
1.8 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 CollectionHandler struct {
|
|
Repo *repository.CollectionRepository
|
|
}
|
|
|
|
func NewCollectionHandler() *CollectionHandler {
|
|
return &CollectionHandler{
|
|
Repo: repository.NewCollectionRepository(database.DB),
|
|
}
|
|
}
|
|
|
|
func (h *CollectionHandler) 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", "")
|
|
|
|
collections, err := h.Repo.FindAll(page, limit, offset, search)
|
|
if err != nil {
|
|
return c.Status(500).JSON(fiber.Map{"error": "Failed to fetch collections"})
|
|
}
|
|
return c.JSON(collections)
|
|
}
|
|
|
|
func (h *CollectionHandler) Create(c *fiber.Ctx) error {
|
|
var input models.Collection
|
|
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 collection"})
|
|
}
|
|
return c.JSON(input)
|
|
}
|
|
|
|
func (h *CollectionHandler) Update(c *fiber.Ctx) error {
|
|
id, _ := strconv.Atoi(c.Params("id"))
|
|
var input models.Collection
|
|
if err := c.BodyParser(&input); err != nil {
|
|
return c.Status(400).JSON(fiber.Map{"error": "Invalid input"})
|
|
}
|
|
if err := h.Repo.Update(uint(id), &input); err != nil {
|
|
return c.Status(500).JSON(fiber.Map{"error": "Failed to update collection"})
|
|
}
|
|
return c.JSON(fiber.Map{"message": "Collection updated"})
|
|
}
|
|
|
|
func (h *CollectionHandler) 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 collection"})
|
|
}
|
|
return c.JSON(fiber.Map{"message": "Collection deleted"})
|
|
}
|