focus/backend/handlers/cards.go

159 lines
3.2 KiB
Go
Raw Normal View History

2023-12-27 22:23:10 +01:00
package handlers
2023-12-27 20:12:39 +01:00
import (
2023-12-29 01:49:09 +01:00
"fmt"
2023-12-27 20:12:39 +01:00
"strconv"
2023-12-27 22:23:10 +01:00
"git.bhasher.com/bhasher/focus/backend/db"
"git.bhasher.com/bhasher/focus/backend/types"
2023-12-27 20:12:39 +01:00
"github.com/gofiber/fiber/v2"
)
2023-12-30 19:42:45 +01:00
func cardsRouter(router fiber.Router) error {
router.Post("/", CreateCard)
router.Get("/:id", GetCard)
router.Put("/:id", UpdateCard)
router.Delete("/:id", DeleteCard)
cardsTagsRouter(router.Group("/:card_id/tags"))
return nil
}
2023-12-29 03:08:08 +01:00
func CreateCard(c *fiber.Ctx) error {
2023-12-27 22:23:10 +01:00
card := types.Card{}
if err := c.BodyParser(&card); err != nil {
2023-12-31 05:53:46 +01:00
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Cannot parse request"})
2023-12-27 20:12:39 +01:00
}
2023-12-27 22:23:10 +01:00
id, err := db.CreateCard(card)
2023-12-27 20:12:39 +01:00
if err != nil {
2023-12-30 04:07:53 +01:00
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Cannot create card",
"trace": fmt.Sprint(err),
})
2023-12-27 20:12:39 +01:00
}
2024-01-18 04:10:28 +01:00
if err := c.Status(fiber.StatusCreated).JSON(fiber.Map{
2023-12-30 16:03:44 +01:00
"id": id,
2024-01-18 04:10:28 +01:00
}); err != nil {
return err
}
card.ID = id;
source := c.Get("X-Request-Source");
if source == "" {
return nil;
}
publish(fiber.Map{
"object": "card",
"action": "create",
"data": card,
"X-Request-Source": source,
2023-12-30 04:07:53 +01:00
})
2024-01-18 04:10:28 +01:00
return nil;
2023-12-27 20:12:39 +01:00
}
2023-12-27 22:23:10 +01:00
func GetCard(c *fiber.Ctx) error {
2023-12-27 20:12:39 +01:00
id, err := strconv.Atoi(c.Params("id"))
if err != nil {
2023-12-31 05:53:46 +01:00
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid card ID"})
2023-12-27 20:12:39 +01:00
}
2023-12-27 22:23:10 +01:00
card, err := db.GetCard(id)
2023-12-27 20:12:39 +01:00
if err != nil {
2023-12-30 16:03:44 +01:00
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Cannot retrieve card",
"trace": fmt.Sprint(err),
})
2023-12-27 20:12:39 +01:00
}
2023-12-27 22:23:10 +01:00
if card == nil {
2023-12-30 16:03:44 +01:00
return c.SendStatus(fiber.StatusNotFound)
2023-12-27 20:12:39 +01:00
}
2023-12-30 16:03:44 +01:00
return c.Status(fiber.StatusOK).JSON(card)
2023-12-27 20:12:39 +01:00
}
2023-12-27 22:23:10 +01:00
func DeleteCard(c *fiber.Ctx) error {
2023-12-27 20:12:39 +01:00
id, err := strconv.Atoi(c.Params("id"))
if err != nil {
2023-12-31 05:53:46 +01:00
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid card ID"})
2023-12-27 20:12:39 +01:00
}
2023-12-30 16:03:44 +01:00
count, err := db.DeleteCard(id)
2023-12-27 20:12:39 +01:00
if err != nil {
2023-12-30 16:03:44 +01:00
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Cannot delete card",
"trace": fmt.Sprint(err),
})
}
if count == 0 {
return c.SendStatus(fiber.StatusNotFound)
2023-12-27 20:12:39 +01:00
}
2024-01-18 04:10:28 +01:00
if err := c.SendStatus(fiber.StatusNoContent); err != nil {
return err
}
source := c.Get("X-Request-Source");
if source == "" {
return nil;
}
publish(fiber.Map{
"object": "card",
"action": "delete",
"id": id,
"X-Request-Source": source,
});
return nil;
2023-12-27 20:12:39 +01:00
}
2023-12-27 22:23:10 +01:00
func UpdateCard(c *fiber.Ctx) error {
2023-12-27 20:12:39 +01:00
id, err := strconv.Atoi(c.Params("id"))
if err != nil {
2023-12-27 22:23:10 +01:00
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid card ID"})
2023-12-27 20:12:39 +01:00
}
2023-12-27 22:23:10 +01:00
card := types.Card{ID: id}
if err := c.BodyParser(&card); err != nil {
2023-12-27 20:12:39 +01:00
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Cannot parse request"})
}
2023-12-30 16:03:44 +01:00
count, err := db.UpdateCard(card)
2023-12-27 20:12:39 +01:00
if err != nil {
2023-12-30 16:03:44 +01:00
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Cannot update card",
"trace": fmt.Sprint(err),
})
}
if count == 0 {
return c.SendStatus(fiber.StatusNotFound)
2023-12-27 20:12:39 +01:00
}
2024-01-18 04:10:28 +01:00
if err := c.SendStatus(fiber.StatusNoContent); err != nil {
return err
}
source := c.Get("X-Request-Source");
if source == "" {
return nil;
}
publish(fiber.Map{
"object": "card",
"action": "update",
"id": id,
"changes": card,
"X-Request-Source": source,
})
return nil;
2023-12-27 20:12:39 +01:00
}