focus/backend/handlers/lists.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 CreateList(c *fiber.Ctx) error {
list := types.List{}
2023-12-27 20:12:39 +01:00
if err := c.BodyParser(&list); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Cannot parse request"})
}
2023-12-27 22:23:10 +01:00
id, err := db.CreateList(list)
2023-12-27 20:12:39 +01:00
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Cannot create list"})
}
return c.Status(fiber.StatusCreated).JSON(fiber.Map{"id": id})
}
2023-12-27 22:23:10 +01:00
func GetAllListsOf(c *fiber.Ctx) error {
projectID, err := strconv.Atoi(c.Params("project_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 project ID"})
2023-12-27 20:12:39 +01:00
}
2023-12-27 22:23:10 +01:00
lists, err := db.GetAllListsOf(projectID)
2023-12-27 20:12:39 +01:00
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Cannot retrieve lists"})
}
return c.JSON(lists)
}
2023-12-27 22:23:10 +01:00
func GetList(c *fiber.Ctx) error {
2023-12-27 20:12:39 +01:00
id, err := strconv.Atoi(c.Params("id"))
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid list ID"})
}
2023-12-27 22:23:10 +01:00
list, err := db.GetList(id)
2023-12-27 20:12:39 +01:00
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Cannot retrieve list"})
}
if list == nil {
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "List not found"})
}
return c.JSON(list)
}
2023-12-27 22:23:10 +01:00
func DeleteList(c *fiber.Ctx) error {
2023-12-27 20:12:39 +01:00
id, err := strconv.Atoi(c.Params("id"))
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid list ID"})
}
2023-12-27 22:23:10 +01:00
err = db.DeleteList(id)
2023-12-27 20:12:39 +01:00
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Cannot delete list"})
}
return c.SendStatus(fiber.StatusNoContent)
}
2023-12-27 22:23:10 +01:00
func UpdateList(c *fiber.Ctx) error {
2023-12-27 20:12:39 +01:00
id, err := strconv.Atoi(c.Params("id"))
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid list ID"})
}
2023-12-27 22:23:10 +01:00
list := types.List{ID: id}
2023-12-27 20:12:39 +01:00
if err := c.BodyParser(&list); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Cannot parse request"})
}
2023-12-27 22:23:10 +01:00
err = db.UpdateList(list)
2023-12-27 20:12:39 +01:00
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Cannot update list"})
}
return c.SendStatus(fiber.StatusOK)
}