diff --git a/pkg/internal/server/api/categories_api.go b/pkg/internal/server/api/categories_api.go index b011759..f194df4 100644 --- a/pkg/internal/server/api/categories_api.go +++ b/pkg/internal/server/api/categories_api.go @@ -8,6 +8,17 @@ import ( "github.com/gofiber/fiber/v2" ) +func getCategory(c *fiber.Ctx) error { + alias := c.Params("category") + + category, err := services.GetCategory(alias) + if err != nil { + return fiber.NewError(fiber.StatusNotFound, err.Error()) + } + + return c.JSON(category) +} + func listCategories(c *fiber.Ctx) error { categories, err := services.ListCategory() if err != nil { diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go index 500633d..a8fb759 100644 --- a/pkg/internal/server/api/index.go +++ b/pkg/internal/server/api/index.go @@ -42,8 +42,12 @@ func MapAPIs(app *fiber.App, baseURL string) { } api.Get("/categories", listCategories) + api.Get("/categories/:category", getCategory) api.Post("/categories", newCategory) api.Put("/categories/:categoryId", editCategory) api.Delete("/categories/:categoryId", deleteCategory) + + api.Get("/tags", listTags) + api.Get("/tags/:tag", getTag) } } diff --git a/pkg/internal/server/api/tags_api.go b/pkg/internal/server/api/tags_api.go new file mode 100644 index 0000000..7df827b --- /dev/null +++ b/pkg/internal/server/api/tags_api.go @@ -0,0 +1,33 @@ +package api + +import ( + "git.solsynth.dev/hydrogen/interactive/pkg/internal/services" + "github.com/gofiber/fiber/v2" +) + +func getTag(c *fiber.Ctx) error { + alias := c.Params("tag") + + tag, err := services.GetTag(alias) + if err != nil { + return fiber.NewError(fiber.StatusNotFound, err.Error()) + } + + return c.JSON(tag) +} + +func listTags(c *fiber.Ctx) error { + take := c.QueryInt("take", 0) + offset := c.QueryInt("offset", 0) + + if take > 100 { + take = 100 + } + + tags, err := services.ListTags(take, offset) + if err != nil { + return fiber.NewError(fiber.StatusInternalServerError, err.Error()) + } + + return c.JSON(tags) +} diff --git a/pkg/internal/services/tags.go b/pkg/internal/services/tags.go new file mode 100644 index 0000000..cdd95c1 --- /dev/null +++ b/pkg/internal/services/tags.go @@ -0,0 +1,22 @@ +package services + +import ( + "git.solsynth.dev/hydrogen/interactive/pkg/internal/database" + "git.solsynth.dev/hydrogen/interactive/pkg/internal/models" +) + +func ListTags(take int, offset int) ([]models.Tag, error) { + var tags []models.Tag + + err := database.C.Offset(offset).Limit(take).Find(&tags).Error + + return tags, err +} + +func GetTag(alias string) (models.Tag, error) { + var tag models.Tag + if err := database.C.Where(models.Tag{Alias: alias}).First(&tag).Error; err != nil { + return tag, err + } + return tag, nil +}