Listing tags, get tag and get category

This commit is contained in:
LittleSheep 2024-07-31 01:25:51 +08:00
parent e9c7921d39
commit dbd71e8b1b
4 changed files with 70 additions and 0 deletions

View File

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

View File

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

View File

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

View File

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