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-30 04:07:53 +01:00
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
|
|
"error": "Cannot parse request",
|
|
|
|
"trace": fmt.Sprint(err),
|
|
|
|
})
|
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
|
|
|
}
|
|
|
|
|
2023-12-30 16:03:44 +01:00
|
|
|
return c.Status(fiber.StatusCreated).JSON(fiber.Map{
|
|
|
|
"id": id,
|
2023-12-30 04:07:53 +01:00
|
|
|
})
|
2023-12-27 20:12:39 +01:00
|
|
|
}
|
|
|
|
|
2023-12-30 16:03:44 +01:00
|
|
|
func GetProjectCards(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-30 16:03:44 +01:00
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
|
|
"error": "Invalid project_id",
|
|
|
|
"trace": fmt.Sprint(err),
|
|
|
|
})
|
2023-12-27 20:12:39 +01:00
|
|
|
}
|
|
|
|
|
2023-12-30 16:03:44 +01:00
|
|
|
exists, err := db.ExistProject(projectID)
|
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": "Error finding project",
|
|
|
|
"trace": fmt.Sprint(err),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
return c.SendStatus(fiber.StatusNotFound)
|
2023-12-27 20:12:39 +01:00
|
|
|
}
|
|
|
|
|
2023-12-30 16:03:44 +01:00
|
|
|
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)
|
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-30 16:03:44 +01:00
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
|
|
"error": "Invalid card ID",
|
|
|
|
"trace": fmt.Sprint(err),
|
|
|
|
})
|
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-30 16:03:44 +01:00
|
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
|
|
|
"error": "Invalid card ID",
|
|
|
|
"trace": fmt.Sprint(err),
|
|
|
|
})
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|