focus/backend/handlers/projects.go

262 lines
5.8 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-30 04:07:53 +01:00
"fmt"
2023-12-30 19:42:45 +01:00
"strconv"
2024-01-04 14:13:27 +01:00
"time"
2023-12-30 04:07:53 +01:00
2023-12-27 22:23:10 +01:00
"git.bhasher.com/bhasher/focus/backend/db"
"git.bhasher.com/bhasher/focus/backend/types"
2024-01-04 14:13:27 +01:00
"git.bhasher.com/bhasher/focus/backend/utils"
2023-12-27 19:19:49 +01:00
"github.com/gofiber/fiber/v2"
)
2023-12-30 19:42:45 +01:00
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)
2023-12-31 05:53:46 +01:00
router.Get(":id/views", GetProjectViews)
2023-12-30 19:42:45 +01:00
return nil
}
2024-01-04 16:20:00 +01:00
var projectsLastEdit time.Time = time.Now().Truncate(time.Second);
2024-01-04 14:13:27 +01:00
2023-12-27 22:23:10 +01:00
func GetAllProjects(c *fiber.Ctx) error {
2024-01-04 16:20:00 +01:00
isCached, err := utils.Cache(c, &projectsLastEdit);
2024-01-04 14:13:27 +01:00
if err == nil && isCached {
return nil;
}
2023-12-27 22:23:10 +01:00
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),
})
}
2024-01-04 16:20:00 +01:00
2024-01-04 14:13:27 +01:00
err = c.Status(fiber.StatusOK).JSON(projects)
if err != nil {
return err;
}
return nil;
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
}
2024-01-04 16:20:00 +01:00
projectsLastEdit = time.Now().Truncate(time.Second);
if err = c.Status(fiber.StatusCreated).JSON(fiber.Map{
2023-12-30 16:03:44 +01:00
"id": id,
}); err != nil {
return err;
}
p.ID = id;
publish(fiber.Map{
"object": "project",
"action": "create",
"id": id,
"value": p,
2023-12-30 04:07:53 +01:00
})
return nil;
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)
}
2024-01-04 16:20:00 +01:00
projectsLastEdit = time.Now().Truncate(time.Second);
if err = c.SendStatus(fiber.StatusNoContent); err != nil {
return err;
}
publish(fiber.Map{
"object": "project",
"action": "update",
"id": id,
"value": p,
})
return nil;
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)
}
2024-01-04 16:20:00 +01:00
projectsLastEdit = time.Now().Truncate(time.Second);
if err = c.SendStatus(fiber.StatusNoContent); err != nil {
return err;
}
publish(fiber.Map{
"object": "project",
"action": "delete",
"id": id,
})
return nil;
2023-12-27 19:19:49 +01:00
}
2023-12-30 19:42:45 +01:00
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)
}
2023-12-31 05:53:46 +01:00
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)
}