package handlers import ( "fmt" "strconv" "git.bhasher.com/bhasher/focus/backend/db" "git.bhasher.com/bhasher/focus/backend/types" "github.com/gofiber/fiber/v2" ) 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 } func CreateCard(c *fiber.Ctx) error { card := types.Card{} if err := c.BodyParser(&card); err != nil { return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Cannot parse request"}) } id, err := db.CreateCard(card) if err != nil { return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ "error": "Cannot create card", "trace": fmt.Sprint(err), }) } return c.Status(fiber.StatusCreated).JSON(fiber.Map{ "id": id, }) } func GetCard(c *fiber.Ctx) error { id, err := strconv.Atoi(c.Params("id")) if err != nil { return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid card ID"}) } card, err := db.GetCard(id) if err != nil { return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ "error": "Cannot retrieve card", "trace": fmt.Sprint(err), }) } if card == nil { return c.SendStatus(fiber.StatusNotFound) } return c.Status(fiber.StatusOK).JSON(card) } func DeleteCard(c *fiber.Ctx) error { id, err := strconv.Atoi(c.Params("id")) if err != nil { return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid card ID"}) } count, err := db.DeleteCard(id) if err != nil { return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ "error": "Cannot delete card", "trace": fmt.Sprint(err), }) } if count == 0 { return c.SendStatus(fiber.StatusNotFound) } return c.SendStatus(fiber.StatusNoContent) } func UpdateCard(c *fiber.Ctx) error { id, err := strconv.Atoi(c.Params("id")) if err != nil { return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid card ID"}) } card := types.Card{ID: id} if err := c.BodyParser(&card); err != nil { return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Cannot parse request"}) } count, err := db.UpdateCard(card) if err != nil { return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ "error": "Cannot update card", "trace": fmt.Sprint(err), }) } if count == 0 { return c.SendStatus(fiber.StatusNotFound) } return c.SendStatus(fiber.StatusNoContent) }