focus/backend/handlers/projects.go

145 lines
3.4 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-27 22:23:10 +01:00
"git.bhasher.com/bhasher/focus/backend/db"
"git.bhasher.com/bhasher/focus/backend/types"
2023-12-30 04:07:53 +01:00
"git.bhasher.com/bhasher/focus/backend/utils"
2023-12-27 19:19:49 +01:00
"github.com/gofiber/fiber/v2"
)
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),
})
}
halProjects := make([]fiber.Map, len(projects))
for i, p := range projects {
halProjects[i] = fiber.Map{
"project": p,
"_links": utils.HALProjectLinks(p.ID),
}
2023-12-27 19:19:49 +01:00
}
2023-12-30 04:07:53 +01:00
return utils.SendHAL(c, fiber.StatusOK, fiber.Map{
"_links": fiber.Map{
"self": fiber.Map{"href": "/api/projects"},
},
"_embedded": fiber.Map{
"projects": halProjects,
},
})
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
return utils.SendHAL(c, fiber.StatusOK, fiber.Map{
"project": project,
"_links": utils.HALProjectLinks(project.ID),
})
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 04:07:53 +01:00
c.Location(fmt.Sprintf("/api/projects/%v", id))
return utils.SendHAL(c, fiber.StatusCreated, fiber.Map{
"id": id,
"_links": utils.HALProjectLinks(id),
})
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 04:07:53 +01:00
exists, err := db.ExistProject(id)
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)
}
2023-12-27 22:23:10 +01:00
err = db.UpdateProject(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 updating project",
"trace": fmt.Sprint(err),
})
2023-12-27 19:19:49 +01:00
}
2023-12-30 04:07:53 +01:00
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 04:07:53 +01:00
exists, err := db.ExistProject(id)
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)
}
2023-12-27 22:23:10 +01:00
err = db.DeleteProject(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 deleting project",
"trace": fmt.Sprint(err),
})
2023-12-27 19:19:49 +01:00
}
2023-12-30 04:07:53 +01:00
return c.SendStatus(fiber.StatusNoContent)
2023-12-27 19:19:49 +01:00
}