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-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-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-29 01:49:09 +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
|
|
|
}
|
|
|
|
|
|
|
|
return c.Status(fiber.StatusCreated).JSON(fiber.Map{"id": id})
|
|
|
|
}
|
|
|
|
|
2023-12-27 22:23:10 +01:00
|
|
|
func GetAllCardsOf(c *fiber.Ctx) error {
|
2023-12-29 01:49:09 +01:00
|
|
|
projectID, err := strconv.Atoi(c.Params("project_id"))
|
2023-12-27 20:12:39 +01:00
|
|
|
if err != nil {
|
2023-12-29 01:49:09 +01:00
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"status": "error", "error": "Invalid project_id"})
|
2023-12-27 20:12:39 +01:00
|
|
|
}
|
|
|
|
|
2023-12-29 01:49:09 +01:00
|
|
|
projects, err := db.GetAllCardsOf(projectID)
|
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
|
|
|
}
|
|
|
|
|
2023-12-29 01:49:09 +01:00
|
|
|
return c.JSON(fiber.Map{"status": "ok", "data": projects})
|
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-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)
|
|
|
|
}
|