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 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", "trace": fmt.Sprint(err), }) } 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 GetProjectCards(c *fiber.Ctx) error { projectID, err := strconv.Atoi(c.Params("project_id")) if err != nil { return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ "error": "Invalid project_id", "trace": fmt.Sprint(err), }) } exists, err := db.ExistProject(projectID) if err != nil { return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ "error": "Error finding project", "trace": fmt.Sprint(err), }) } if !exists { return c.SendStatus(fiber.StatusNotFound) } cards, err := db.GetProjectsCards(projectID) if err != nil { return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ "error": "Cannot retrieve cards", "trace": fmt.Sprint(err), }) } return c.Status(fiber.StatusOK).JSON(cards) } 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", "trace": fmt.Sprint(err), }) } 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", "trace": fmt.Sprint(err), }) } 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.StatusOK) }