2024-06-22 09:29:53 +00:00
|
|
|
package api
|
2024-02-14 14:03:45 +00:00
|
|
|
|
|
|
|
import (
|
2024-06-22 09:29:53 +00:00
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
|
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/server/exts"
|
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
|
2024-02-14 14:03:45 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
2024-07-30 17:25:51 +00:00
|
|
|
func getCategory(c *fiber.Ctx) error {
|
|
|
|
alias := c.Params("category")
|
|
|
|
|
|
|
|
category, err := services.GetCategory(alias)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(category)
|
|
|
|
}
|
|
|
|
|
2024-03-02 17:23:11 +00:00
|
|
|
func listCategories(c *fiber.Ctx) error {
|
2024-02-14 14:03:45 +00:00
|
|
|
categories, err := services.ListCategory()
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(categories)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newCategory(c *fiber.Ctx) error {
|
2024-09-11 15:42:46 +00:00
|
|
|
if err := gap.H.EnsureGrantedPerm(c, "CreatePostCategories", true); err != nil {
|
2024-06-22 09:29:53 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-02-14 14:03:45 +00:00
|
|
|
|
|
|
|
var data struct {
|
|
|
|
Alias string `json:"alias" validate:"required"`
|
|
|
|
Name string `json:"name" validate:"required"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
}
|
|
|
|
|
2024-06-22 09:29:53 +00:00
|
|
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
2024-02-14 14:03:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
category, err := services.NewCategory(data.Alias, data.Name, data.Description)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(category)
|
|
|
|
}
|
|
|
|
|
|
|
|
func editCategory(c *fiber.Ctx) error {
|
2024-09-11 15:42:46 +00:00
|
|
|
if err := gap.H.EnsureGrantedPerm(c, "CreatePostCategories", true); err != nil {
|
2024-06-22 09:29:53 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-02-14 14:03:45 +00:00
|
|
|
|
|
|
|
id, _ := c.ParamsInt("categoryId", 0)
|
|
|
|
category, err := services.GetCategoryWithID(uint(id))
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
var data struct {
|
|
|
|
Alias string `json:"alias" validate:"required"`
|
|
|
|
Name string `json:"name" validate:"required"`
|
|
|
|
Description string `json:"description"`
|
|
|
|
}
|
|
|
|
|
2024-06-22 09:29:53 +00:00
|
|
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
2024-02-14 14:03:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
category, err = services.EditCategory(category, data.Alias, data.Name, data.Description)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(category)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteCategory(c *fiber.Ctx) error {
|
2024-09-11 15:42:46 +00:00
|
|
|
if err := gap.H.EnsureGrantedPerm(c, "CreatePostCategories", true); err != nil {
|
2024-06-22 09:29:53 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-02-14 14:03:45 +00:00
|
|
|
|
|
|
|
id, _ := c.ParamsInt("categoryId", 0)
|
|
|
|
category, err := services.GetCategoryWithID(uint(id))
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := services.DeleteCategory(category); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(category)
|
|
|
|
}
|