208 lines
5.0 KiB
Go
208 lines
5.0 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"git.bhasher.com/bhasher/focus/backend/db"
|
|
"git.bhasher.com/bhasher/focus/backend/types"
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func projectsRouter(router fiber.Router) error {
|
|
router.Post("/", CreateProject)
|
|
router.Get("/", GetAllProjects)
|
|
router.Get("/:id", GetProject)
|
|
router.Put("/:id", UpdateProject)
|
|
router.Delete("/:id", DeleteProject)
|
|
router.Get(":id/cards", GetProjectCards)
|
|
router.Get(":id/tags", GetProjectTags)
|
|
router.Get(":id/views", GetProjectViews)
|
|
return nil
|
|
}
|
|
|
|
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)
|
|
}
|
|
|
|
func GetProjectCards(c *fiber.Ctx) error {
|
|
projectID, err := strconv.Atoi(c.Params("id"))
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid project ID"})
|
|
}
|
|
|
|
exists, err := db.ExistProject(projectID)
|
|
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)
|
|
}
|
|
|
|
cards, err := db.GetProjectsCards(projectID)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"error": "Cannot retrieve cards",
|
|
"trace": fmt.Sprint(err),
|
|
})
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(cards)
|
|
}
|
|
|
|
func GetProjectTags(c *fiber.Ctx) error {
|
|
projectID, err := strconv.Atoi(c.Params("id"))
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid project ID"})
|
|
}
|
|
|
|
exists, err := db.ExistProject(projectID)
|
|
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)
|
|
}
|
|
|
|
tags, err := db.GetProjectTags(projectID)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"error": "Cannot retrieve tags",
|
|
"trace": fmt.Sprint(err),
|
|
})
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(tags)
|
|
}
|
|
|
|
func GetProjectViews(c *fiber.Ctx) error {
|
|
projectID, err := strconv.Atoi(c.Params("id"))
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid project ID"})
|
|
}
|
|
|
|
exists, err := db.ExistProject(projectID)
|
|
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)
|
|
}
|
|
|
|
views, err := db.GetProjectViews(projectID)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
|
"error": "Cannot retrieve views",
|
|
"trace": fmt.Sprint(err),
|
|
})
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(views)
|
|
}
|