From dbd71e8b1b17f86d950ab49fcd47ae7cda3a2c04 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Wed, 31 Jul 2024 01:25:51 +0800 Subject: [PATCH] :sparkles: Listing tags, get tag and get category --- pkg/internal/server/api/categories_api.go | 11 ++++++++ pkg/internal/server/api/index.go | 4 +++ pkg/internal/server/api/tags_api.go | 33 +++++++++++++++++++++++ pkg/internal/services/tags.go | 22 +++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 pkg/internal/server/api/tags_api.go create mode 100644 pkg/internal/services/tags.go 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 +}