focus/backend/handlers/projects.go

208 lines
5.0 KiB
Go
Raw Normal View History

2023-12-27 22:23:10 +01:00
package handlers
2023-12-27 19:19:49 +01:00
import (
2023-12-30 04:07:53 +01:00
"fmt"
2023-12-30 19:42:45 +01:00
"strconv"
2023-12-30 04:07:53 +01:00
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 19:19:49 +01:00
"github.com/gofiber/fiber/v2"
)
2023-12-30 19:42:45 +01:00
func projectsRouter(router fiber.Router) error {
router.Post("/", CreateProject)
router.Get("/", GetAllProjects)
router.Get("/:id", GetProject)
router.Put("/:id", UpdateProject)
router.Delete("/:id", DeleteProject)
router.Get(":id/cards", GetProjectCards)
router.Get(":id/tags", GetProjectTags)
2023-12-31 05:53:46 +01:00
router.Get(":id/views", GetProjectViews)
2023-12-30 19:42:45 +01:00
return nil
}
2023-12-27 22:23:10 +01:00
func GetAllProjects(c *fiber.Ctx) error {
projects, err := db.GetAllProjects()
2023-12-27 19:19:49 +01:00
if err != nil {
2023-12-30 04:07:53 +01:00
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Cannot retrieve projects",
"trace": fmt.Sprint(err),
})
}
2023-12-30 16:03:44 +01:00
return c.Status(fiber.StatusOK).JSON(projects)
2023-12-27 19:19:49 +01:00
}
2023-12-27 22:23:10 +01:00
func GetProject(c *fiber.Ctx) error {
2023-12-27 19:19:49 +01:00
id, err := c.ParamsInt("id")
if err != nil {
2023-12-27 20:12:39 +01:00
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid project ID"})
2023-12-27 19:19:49 +01:00
}
2023-12-27 22:23:10 +01:00
project, err := db.GetProject(id)
2023-12-27 19:19:49 +01:00
if err != nil {
2023-12-30 04:07:53 +01:00
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Error fetching project",
"trace": fmt.Sprint(err),
})
2023-12-27 19:19:49 +01:00
}
if project == nil {
2023-12-30 04:07:53 +01:00
return c.SendStatus(fiber.StatusNotFound)
2023-12-27 19:19:49 +01:00
}
2023-12-30 04:07:53 +01:00
2023-12-30 16:03:44 +01:00
return c.Status(fiber.StatusOK).JSON(project)
2023-12-27 19:19:49 +01:00
}
2023-12-27 22:23:10 +01:00
func CreateProject(c *fiber.Ctx) error {
p := new(types.Project)
2023-12-27 19:19:49 +01:00
if err := c.BodyParser(p); err != nil {
2023-12-27 20:12:39 +01:00
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Error parsing request"})
2023-12-27 19:19:49 +01:00
}
2023-12-27 22:23:10 +01:00
id, err := db.CreateProject(*p)
2023-12-27 19:19:49 +01:00
if err != nil {
2023-12-30 04:07:53 +01:00
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Error creating project",
"trace": fmt.Sprint(err),
})
2023-12-27 19:19:49 +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 19:19:49 +01:00
}
2023-12-27 22:23:10 +01:00
func UpdateProject(c *fiber.Ctx) error {
2023-12-27 19:19:49 +01:00
id, err := c.ParamsInt("id")
if err != nil {
2023-12-27 20:12:39 +01:00
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid project ID"})
2023-12-27 19:19:49 +01:00
}
2023-12-27 22:23:10 +01:00
p := types.Project{ID: id}
2023-12-27 19:19:49 +01:00
if err := c.BodyParser(&p); err != nil {
2023-12-27 20:12:39 +01:00
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Error parsing request"})
2023-12-27 19:19:49 +01:00
}
2023-12-30 16:03:44 +01:00
count, err := db.UpdateProject(p)
2023-12-30 04:07:53 +01:00
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
2023-12-30 16:03:44 +01:00
"error": "Error updating project",
2023-12-30 04:07:53 +01:00
"trace": fmt.Sprint(err),
})
}
2023-12-30 16:03:44 +01:00
if count == 0 {
2023-12-30 04:07:53 +01:00
return c.SendStatus(fiber.StatusNotFound)
}
return c.SendStatus(fiber.StatusNoContent)
2023-12-27 19:19:49 +01:00
}
2023-12-27 22:23:10 +01:00
func DeleteProject(c *fiber.Ctx) error {
2023-12-27 19:19:49 +01:00
id, err := c.ParamsInt("id")
if err != nil {
2023-12-27 20:12:39 +01:00
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid project ID"})
2023-12-27 19:19:49 +01:00
}
2023-12-30 16:03:44 +01:00
count, err := db.DeleteProject(id)
2023-12-30 04:07:53 +01:00
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
2023-12-30 16:03:44 +01:00
"error": "Error deleting project",
2023-12-30 04:07:53 +01:00
"trace": fmt.Sprint(err),
})
}
2023-12-30 16:03:44 +01:00
if count == 0 {
2023-12-30 04:07:53 +01:00
return c.SendStatus(fiber.StatusNotFound)
}
return c.SendStatus(fiber.StatusNoContent)
2023-12-27 19:19:49 +01:00
}
2023-12-30 19:42:45 +01:00
func GetProjectCards(c *fiber.Ctx) error {
projectID, err := strconv.Atoi(c.Params("id"))
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid project ID"})
}
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 GetProjectTags(c *fiber.Ctx) error {
projectID, err := strconv.Atoi(c.Params("id"))
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid project ID"})
}
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)
}
tags, err := db.GetProjectTags(projectID)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Cannot retrieve tags",
"trace": fmt.Sprint(err),
})
}
return c.Status(fiber.StatusOK).JSON(tags)
}
2023-12-31 05:53:46 +01:00
func GetProjectViews(c *fiber.Ctx) error {
projectID, err := strconv.Atoi(c.Params("id"))
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid project ID"})
}
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)
}
views, err := db.GetProjectViews(projectID)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"error": "Cannot retrieve views",
"trace": fmt.Sprint(err),
})
}
return c.Status(fiber.StatusOK).JSON(views)
}