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-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),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|