Handler groups

This commit is contained in:
Brieuc Dubois 2023-12-30 19:42:45 +01:00
parent 236bea14dc
commit 0809efbf81
6 changed files with 122 additions and 84 deletions

View File

@ -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 {

View File

@ -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 {

24
backend/handlers/main.go Normal file
View File

@ -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)

View File

@ -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)
}

View File

@ -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 {

View File

@ -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)))
}