focus/backend/handlers/projects.go

80 lines
2.1 KiB
Go

package handlers
import (
"git.bhasher.com/bhasher/focus/backend/db"
"git.bhasher.com/bhasher/focus/backend/types"
"github.com/gofiber/fiber/v2"
)
func GetAllProjects(c *fiber.Ctx) error {
projects, err := db.GetAllProjects()
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Cannot retrieve projects"})
}
return c.JSON(projects)
}
func GetProject(c *fiber.Ctx) error {
id, err := c.ParamsInt("id")
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid project ID"})
}
project, err := db.GetProject(id)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Error fetching project"})
}
if project == nil {
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "Project not found"})
}
return c.JSON(project)
}
func CreateProject(c *fiber.Ctx) error {
p := new(types.Project)
if err := c.BodyParser(p); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Error parsing request"})
}
id, err := db.CreateProject(*p)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Error creating project"})
}
return c.Status(fiber.StatusCreated).JSON(fiber.Map{"id": id})
}
func UpdateProject(c *fiber.Ctx) error {
id, err := c.ParamsInt("id")
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid project ID"})
}
p := types.Project{ID: id}
if err := c.BodyParser(&p); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Error parsing request"})
}
err = db.UpdateProject(p)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Error updating project"})
}
return c.SendStatus(fiber.StatusOK)
}
func DeleteProject(c *fiber.Ctx) error {
id, err := c.ParamsInt("id")
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid project ID"})
}
err = db.DeleteProject(id)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": "Error deleting project"})
}
return c.SendStatus(fiber.StatusOK)
}