diff --git a/backend/handlers/cards.go b/backend/handlers/cards.go index c2fa794..c789890 100644 --- a/backend/handlers/cards.go +++ b/backend/handlers/cards.go @@ -9,6 +9,17 @@ import ( "github.com/gofiber/fiber/v2" ) +func cardsRouter(router fiber.Router) error { + router.Post("/", CreateCard) + router.Get("/:id", GetCard) + router.Put("/:id", UpdateCard) + router.Delete("/:id", DeleteCard) + + cardsTagsRouter(router.Group("/:card_id/tags")) + return nil + +} + func CreateCard(c *fiber.Ctx) error { card := types.Card{} if err := c.BodyParser(&card); err != nil { @@ -31,38 +42,6 @@ func CreateCard(c *fiber.Ctx) error { }) } -func GetProjectCards(c *fiber.Ctx) error { - projectID, err := strconv.Atoi(c.Params("project_id")) - if err != nil { - return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ - "error": "Invalid project_id", - "trace": fmt.Sprint(err), - }) - } - - 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 GetCard(c *fiber.Ctx) error { id, err := strconv.Atoi(c.Params("id")) if err != nil { diff --git a/backend/handlers/cardstags.go b/backend/handlers/cardstags.go index a5797c4..9b9e636 100644 --- a/backend/handlers/cardstags.go +++ b/backend/handlers/cardstags.go @@ -10,6 +10,15 @@ import ( "github.com/gofiber/fiber/v2" ) +func cardsTagsRouter(router fiber.Router) error { + router.Get("/", GetCardTags) + router.Delete("/", DeleteCardTags) + router.Post("/:tag_id", CreateCardTag) + router.Put("/:tag_id", UpdateCardTag) + router.Delete("/:tag_id", DeleteCardTag) + return nil +} + func CreateCardTag(c *fiber.Ctx) error { cardID, err := strconv.Atoi(c.Params("card_id")) if err != nil { diff --git a/backend/handlers/main.go b/backend/handlers/main.go new file mode 100644 index 0000000..7063c57 --- /dev/null +++ b/backend/handlers/main.go @@ -0,0 +1,24 @@ +package handlers + +import "github.com/gofiber/fiber/v2" + +func APIRouter(router fiber.Router) error { + return v1Router(router.Group("/v1")) +} + +func v1Router(router fiber.Router) error { + projectsRouter(router.Group("/projects")) + cardsRouter(router.Group("/cards")) + tagsRouter(router.Group("/tags")) + return nil +} + +// app.Post("/api/v1/cards", handlers.CreateCard) +// app.Get("/api/v1/cards/:id", handlers.GetCard) +// app.Put("/api/v1/cards/:id", handlers.UpdateCard) +// app.Delete("/api/v1/cards/:id", handlers.DeleteCard) + +// app.Post("/api/v1/tags", handlers.CreateTag) +// app.Get("/api/v1/tags/:id", handlers.GetTag) +// app.Delete("/api/v1/tags/:id", handlers.DeleteTag) +// app.Put("/api/v1/tags/:id", handlers.UpdateTag) diff --git a/backend/handlers/projects.go b/backend/handlers/projects.go index f2f677d..38a84b5 100644 --- a/backend/handlers/projects.go +++ b/backend/handlers/projects.go @@ -2,12 +2,24 @@ 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) + return nil +} + func GetAllProjects(c *fiber.Ctx) error { projects, err := db.GetAllProjects() if err != nil { @@ -105,3 +117,61 @@ func DeleteProject(c *fiber.Ctx) error { 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) +} diff --git a/backend/handlers/tags.go b/backend/handlers/tags.go index 17cb520..eba7f56 100644 --- a/backend/handlers/tags.go +++ b/backend/handlers/tags.go @@ -9,6 +9,13 @@ import ( "github.com/gofiber/fiber/v2" ) +func tagsRouter(router fiber.Router) { + router.Post("/", CreateTag) + router.Get("/:id", GetTag) + router.Delete("/:id", DeleteTag) + router.Put("/:id", UpdateTag) +} + func CreateTag(c *fiber.Ctx) error { tag := types.Tag{} if err := c.BodyParser(&tag); err != nil { @@ -26,35 +33,6 @@ func CreateTag(c *fiber.Ctx) error { return c.Status(fiber.StatusCreated).JSON(fiber.Map{"id": id}) } -func GetProjectTags(c *fiber.Ctx) error { - projectID, err := strconv.Atoi(c.Params("project_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 GetTag(c *fiber.Ctx) error { id, err := strconv.Atoi(c.Params("id")) if err != nil { diff --git a/backend/main.go b/backend/main.go index 4ecc44a..2a1d353 100644 --- a/backend/main.go +++ b/backend/main.go @@ -30,29 +30,7 @@ func main() { AllowHeaders: "Origin, Content-Type, Accept", })) - app.Post("/api/v1/projects", handlers.CreateProject) - app.Get("/api/v1/projects", handlers.GetAllProjects) - app.Get("/api/v1/projects/:id", handlers.GetProject) - app.Put("/api/v1/projects/:id", handlers.UpdateProject) - app.Delete("/api/v1/projects/:id", handlers.DeleteProject) - app.Get("/api/v1/projects/:project_id/cards", handlers.GetProjectCards) - app.Get("/api/v1/projects/:project_id/tags", handlers.GetProjectTags) - - app.Post("/api/v1/cards", handlers.CreateCard) - app.Get("/api/v1/cards/:id", handlers.GetCard) - app.Put("/api/v1/cards/:id", handlers.UpdateCard) - app.Delete("/api/v1/cards/:id", handlers.DeleteCard) - - app.Post("/api/v1/tags", handlers.CreateTag) - app.Get("/api/v1/tags/:id", handlers.GetTag) - app.Delete("/api/v1/tags/:id", handlers.DeleteTag) - app.Put("/api/v1/tags/:id", handlers.UpdateTag) - - app.Post("/api/v1/cards/:card_id/tags/:tag_id", handlers.CreateCardTag) - app.Get("/api/v1/cards/:card_id/tags", handlers.GetCardTags) - app.Put("/api/v1/cards/:card_id/tags/:tag_id", handlers.UpdateCardTag) - app.Delete("/api/v1/cards/:card_id/tags/:tag_id", handlers.DeleteCardTag) - app.Delete("/api/v1/cards/:card_id/tags", handlers.DeleteCardTags) + handlers.APIRouter(app.Group("/api")) log.Fatal(app.Listen(fmt.Sprintf(":%v", port))) }