Search for tags

This commit is contained in:
LittleSheep 2024-07-31 01:29:09 +08:00
parent dbd71e8b1b
commit 6638a9bf14
3 changed files with 18 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package api package api
import ( import (
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services" "git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
@ -19,12 +20,19 @@ func getTag(c *fiber.Ctx) error {
func listTags(c *fiber.Ctx) error { func listTags(c *fiber.Ctx) error {
take := c.QueryInt("take", 0) take := c.QueryInt("take", 0)
offset := c.QueryInt("offset", 0) offset := c.QueryInt("offset", 0)
probe := c.Query("probe")
if take > 100 { if take > 100 {
take = 100 take = 100
} }
tags, err := services.ListTags(take, offset) var tags []models.Tag
var err error
if len(probe) > 0 {
tags, err = services.SearchTags(take, offset, probe)
} else {
tags, err = services.ListTags(take, offset)
}
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} }

View File

@ -11,7 +11,6 @@ import (
func ListCategory() ([]models.Category, error) { func ListCategory() ([]models.Category, error) {
var categories []models.Category var categories []models.Category
err := database.C.Find(&categories).Error err := database.C.Find(&categories).Error
return categories, err return categories, err

View File

@ -7,12 +7,20 @@ import (
func ListTags(take int, offset int) ([]models.Tag, error) { func ListTags(take int, offset int) ([]models.Tag, error) {
var tags []models.Tag var tags []models.Tag
err := database.C.Offset(offset).Limit(take).Find(&tags).Error err := database.C.Offset(offset).Limit(take).Find(&tags).Error
return tags, err return tags, err
} }
func SearchTags(take int, offset int, probe string) ([]models.Tag, error) {
probe = "%" + probe + "%"
var tags []models.Tag
err := database.C.Where("alias LIKE ?", probe).Offset(offset).Limit(take).Find(&tags).Error
return tags, err
}
func GetTag(alias string) (models.Tag, error) { func GetTag(alias string) (models.Tag, error) {
var tag models.Tag var tag models.Tag
if err := database.C.Where(models.Tag{Alias: alias}).First(&tag).Error; err != nil { if err := database.C.Where(models.Tag{Alias: alias}).First(&tag).Error; err != nil {