113 lines
3.5 KiB
Go
113 lines
3.5 KiB
Go
package controllers
|
|
|
|
import (
|
|
"math"
|
|
"strconv"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/sujit-baniya/fiber-boilerplate/pkg/models"
|
|
)
|
|
|
|
// DocCategoriasIndex renderiza el panel de gestión de categorías de documentación.
|
|
func DocCategoriasIndex(c *fiber.Ctx) error {
|
|
data := fiber.Map{
|
|
"user": c.Locals("user").(map[string]interface{}),
|
|
"modules": c.Locals("userModules"),
|
|
}
|
|
if err := c.Render("docs/categorias", data, "layouts/main"); err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetDocCategorias devuelve la lista paginada de categorías en JSON.
|
|
func GetDocCategorias(c *fiber.Ctx) error {
|
|
page, _ := strconv.Atoi(c.Query("page", "1"))
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
search := c.Query("search", "")
|
|
limit := 10
|
|
offset := (page - 1) * limit
|
|
|
|
items, total, err := models.GetAllDocCategorias(limit, offset, search)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
return c.JSON(fiber.Map{
|
|
"items": items,
|
|
"total": total,
|
|
"totalPages": int(math.Ceil(float64(total) / float64(limit))),
|
|
"page": page,
|
|
"limit": limit,
|
|
})
|
|
}
|
|
|
|
// CreateDocCategoria crea una nueva categoría global.
|
|
func CreateDocCategoria(c *fiber.Ctx) error {
|
|
type Req struct {
|
|
Nombre string `json:"nombre"`
|
|
Slug string `json:"slug"`
|
|
Descripcion string `json:"descripcion"`
|
|
Orden int `json:"orden"`
|
|
}
|
|
var req Req
|
|
if err := c.BodyParser(&req); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Datos inválidos"})
|
|
}
|
|
if req.Nombre == "" || req.Slug == "" {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Nombre y slug son obligatorios"})
|
|
}
|
|
item := &models.DocCategoria{
|
|
Nombre: req.Nombre,
|
|
Slug: req.Slug,
|
|
Descripcion: req.Descripcion,
|
|
Orden: req.Orden,
|
|
}
|
|
if err := models.CreateDocCategoria(item); err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
return c.JSON(fiber.Map{"message": "Categoría creada", "item": item})
|
|
}
|
|
|
|
// UpdateDocCategoria actualiza una categoría existente.
|
|
func UpdateDocCategoria(c *fiber.Ctx) error {
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 32)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "ID inválido"})
|
|
}
|
|
type Req struct {
|
|
Nombre string `json:"nombre"`
|
|
Slug string `json:"slug"`
|
|
Descripcion string `json:"descripcion"`
|
|
Orden int `json:"orden"`
|
|
}
|
|
var req Req
|
|
if err := c.BodyParser(&req); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Datos inválidos"})
|
|
}
|
|
item := &models.DocCategoria{
|
|
Nombre: req.Nombre,
|
|
Slug: req.Slug,
|
|
Descripcion: req.Descripcion,
|
|
Orden: req.Orden,
|
|
}
|
|
item.ID = uint(id)
|
|
if err := models.UpdateDocCategoria(item); err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
return c.JSON(fiber.Map{"message": "Categoría actualizada"})
|
|
}
|
|
|
|
// DeleteDocCategoria elimina una categoría.
|
|
func DeleteDocCategoria(c *fiber.Ctx) error {
|
|
id, err := strconv.ParseUint(c.Params("id"), 10, 32)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "ID inválido"})
|
|
}
|
|
if err := models.DeleteDocCategoria(uint(id)); err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
|
|
}
|
|
return c.JSON(fiber.Map{"message": "Categoría eliminada"})
|
|
}
|