package handlers import ( "fmt" "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", "trace": fmt.Sprint(err), }) } return c.Status(fiber.StatusOK).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", "trace": fmt.Sprint(err), }) } if project == nil { return c.SendStatus(fiber.StatusNotFound) } return c.Status(fiber.StatusOK).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", "trace": fmt.Sprint(err), }) } 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"}) } count, err := db.UpdateProject(p) if err != nil { return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ "error": "Error updating project", "trace": fmt.Sprint(err), }) } if count == 0 { return c.SendStatus(fiber.StatusNotFound) } return c.SendStatus(fiber.StatusNoContent) } 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"}) } count, err := db.DeleteProject(id) if err != nil { return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ "error": "Error deleting project", "trace": fmt.Sprint(err), }) } if count == 0 { return c.SendStatus(fiber.StatusNotFound) } return c.SendStatus(fiber.StatusNoContent) }