focus/backend/handlers/cards.go

88 lines
2.3 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 (
"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-27 22:23:10 +01:00
func CreateCards(c *fiber.Ctx) error {
card := types.Card{}
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-27 22:23:10 +01:00
id, err := db.CreateCard(card)
2023-12-27 20:12:39 +01:00
if err != nil {
2023-12-27 22:23:10 +01:00
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Cannot create card"})
2023-12-27 20:12:39 +01:00
}
return c.Status(fiber.StatusCreated).JSON(fiber.Map{"id": id})
}
2023-12-27 22:23:10 +01:00
func GetAllCardsOf(c *fiber.Ctx) error {
listID, err := strconv.Atoi(c.Params("list_id"))
2023-12-27 20:12:39 +01:00
if err != nil {
2023-12-27 22:23:10 +01:00
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid list ID"})
2023-12-27 20:12:39 +01:00
}
2023-12-27 22:23:10 +01:00
lists, err := db.GetAllListsOf(listID)
2023-12-27 20:12:39 +01:00
if err != nil {
2023-12-27 22:23:10 +01:00
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Cannot retrieve cards"})
2023-12-27 20:12:39 +01:00
}
return c.JSON(lists)
}
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-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, err := db.GetCard(id)
2023-12-27 20:12:39 +01:00
if err != nil {
2023-12-27 22:23:10 +01:00
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Cannot retrieve card"})
2023-12-27 20:12:39 +01:00
}
2023-12-27 22:23:10 +01:00
if card == nil {
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "Card not found"})
2023-12-27 20:12:39 +01:00
}
2023-12-27 22:23:10 +01:00
return c.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-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
err = db.DeleteCard(id)
2023-12-27 20:12:39 +01:00
if err != nil {
2023-12-27 22:23:10 +01:00
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Cannot delete card"})
2023-12-27 20:12:39 +01:00
}
return c.SendStatus(fiber.StatusNoContent)
}
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-27 22:23:10 +01:00
err = db.UpdateCard(card)
2023-12-27 20:12:39 +01:00
if err != nil {
2023-12-27 22:23:10 +01:00
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Cannot update card"})
2023-12-27 20:12:39 +01:00
}
return c.SendStatus(fiber.StatusOK)
}