✨ Listing tags, get tag and get category
This commit is contained in:
parent
e9c7921d39
commit
dbd71e8b1b
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
33
pkg/internal/server/api/tags_api.go
Normal file
33
pkg/internal/server/api/tags_api.go
Normal 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)
|
||||
}
|
22
pkg/internal/services/tags.go
Normal file
22
pkg/internal/services/tags.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user