Interactive/pkg/internal/server/api/recommendation_api.go

76 lines
2.0 KiB
Go
Raw Normal View History

2024-07-23 08:12:19 +00:00
package api
import (
"fmt"
2024-07-24 06:29:43 +00:00
2024-07-23 08:12:19 +00:00
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
"github.com/gofiber/fiber/v2"
)
func listRecommendationDefault(c *fiber.Ctx) error {
take := c.QueryInt("take", 0)
offset := c.QueryInt("offset", 0)
realmId := c.QueryInt("realmId", 0)
maxDownVote := c.QueryInt("maxDownVote", 3)
tx := database.C.Joins("Author").Where("\"Author\".total_downvote <= ?", maxDownVote)
2024-07-23 08:12:19 +00:00
tx = services.FilterPostDraft(tx)
if realmId > 0 {
if realm, err := services.GetRealmWithExtID(uint(realmId)); err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err))
} else {
tx = services.FilterPostWithRealm(tx, realm.ID)
}
}
countTx := tx
count, err := services.CountPost(countTx)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
items, err := services.ListPost(tx, take, offset, "published_at DESC")
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.JSON(fiber.Map{
"count": count,
"data": items,
})
}
func listRecommendationShuffle(c *fiber.Ctx) error {
take := c.QueryInt("take", 0)
offset := c.QueryInt("offset", 0)
realmId := c.QueryInt("realmId", 0)
maxDownVote := c.QueryInt("maxDownVote", 3)
tx := database.C.Joins("Author").Where("\"Author\".total_downvote <= ?", maxDownVote)
2024-07-23 08:12:19 +00:00
tx = services.FilterPostDraft(tx)
if realmId > 0 {
if realm, err := services.GetRealmWithExtID(uint(realmId)); err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err))
} else {
tx = services.FilterPostWithRealm(tx, realm.ID)
}
}
countTx := tx
count, err := services.CountPost(countTx)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
2024-07-24 06:37:21 +00:00
items, err := services.ListPost(tx, take, offset, "RANDOM()")
2024-07-23 08:12:19 +00:00
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.JSON(fiber.Map{
"count": count,
"data": items,
})
}