88 lines
2.3 KiB
Go
88 lines
2.3 KiB
Go
package handlers
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"git.bhasher.com/bhasher/focus/backend/db"
|
|
"git.bhasher.com/bhasher/focus/backend/types"
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func CreateList(c *fiber.Ctx) error {
|
|
list := types.List{}
|
|
if err := c.BodyParser(&list); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Cannot parse request"})
|
|
}
|
|
|
|
id, err := db.CreateList(list)
|
|
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})
|
|
}
|
|
|
|
func GetAllListsOf(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"})
|
|
}
|
|
|
|
lists, err := db.GetAllListsOf(projectID)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Cannot retrieve lists"})
|
|
}
|
|
|
|
return c.JSON(lists)
|
|
}
|
|
|
|
func GetList(c *fiber.Ctx) error {
|
|
id, err := strconv.Atoi(c.Params("id"))
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid list ID"})
|
|
}
|
|
|
|
list, err := db.GetList(id)
|
|
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)
|
|
}
|
|
|
|
func DeleteList(c *fiber.Ctx) error {
|
|
id, err := strconv.Atoi(c.Params("id"))
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid list ID"})
|
|
}
|
|
|
|
err = db.DeleteList(id)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Cannot delete list"})
|
|
}
|
|
|
|
return c.SendStatus(fiber.StatusNoContent)
|
|
}
|
|
|
|
func UpdateList(c *fiber.Ctx) error {
|
|
id, err := strconv.Atoi(c.Params("id"))
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid list ID"})
|
|
}
|
|
|
|
list := types.List{ID: id}
|
|
if err := c.BodyParser(&list); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Cannot parse request"})
|
|
}
|
|
|
|
err = db.UpdateList(list)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Cannot update list"})
|
|
}
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
}
|