focus/backend/handlers/projects.go

80 lines
2.1 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-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-27 20:12:39 +01:00
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Cannot retrieve projects"})
2023-12-27 19:19:49 +01:00
}
return c.JSON(projects)
}
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-27 20:12:39 +01:00
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Error fetching project"})
2023-12-27 19:19:49 +01:00
}
if project == nil {
2023-12-27 20:12:39 +01:00
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "Project not found"})
2023-12-27 19:19:49 +01:00
}
return c.JSON(project)
}
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-27 20:12:39 +01:00
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Error creating project"})
2023-12-27 19:19:49 +01:00
}
2023-12-27 20:12:39 +01:00
return c.Status(fiber.StatusCreated).JSON(fiber.Map{"id": 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-27 22:23:10 +01:00
err = db.UpdateProject(p)
2023-12-27 19:19:49 +01:00
if err != nil {
2023-12-27 20:12:39 +01:00
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Error updating project"})
2023-12-27 19:19:49 +01:00
}
return c.SendStatus(fiber.StatusOK)
}
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-27 22:23:10 +01:00
err = db.DeleteProject(id)
2023-12-27 19:19:49 +01:00
if err != nil {
2023-12-27 20:12:39 +01:00
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Error deleting project"})
2023-12-27 19:19:49 +01:00
}
2023-12-27 20:12:39 +01:00
return c.SendStatus(fiber.StatusOK)
2023-12-27 19:19:49 +01:00
}