Recommendation API

This commit is contained in:
2024-07-23 16:12:19 +08:00
parent 8429d72ad1
commit 2cf24c4724
7 changed files with 143 additions and 44 deletions

View File

@ -10,6 +10,12 @@ func MapAPIs(app *fiber.App, baseURL string) {
api.Get("/users/me", getUserinfo)
api.Get("/users/:accountId", getOthersInfo)
recommendations := api.Group("/recommendations").Name("Recommendations API")
{
recommendations.Get("/", listRecommendationDefault)
recommendations.Get("/shuffle", listRecommendationShuffle)
}
stories := api.Group("/stories").Name("Story API")
{
stories.Post("/", createStory)

View File

@ -66,7 +66,7 @@ func listPost(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
items, err := services.ListPost(tx, take, offset)
items, err := services.ListPost(tx, take, offset, "published_at DESC")
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
@ -93,7 +93,7 @@ func listDraftPost(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
items, err := services.ListPost(tx, take, offset, true)
items, err := services.ListPost(tx, take, offset, "created_at DESC", true)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}

View File

@ -0,0 +1,75 @@
package api
import (
"fmt"
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
)
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("accounts.total_downvote <= ?", maxDownVote)
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("accounts.total_downvote <= ?", maxDownVote)
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, gorm.Expr("RAND()"))
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.JSON(fiber.Map{
"count": count,
"data": items,
})
}

View File

@ -40,7 +40,7 @@ func listPostReplies(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
items, err := services.ListPost(tx, take, offset)
items, err := services.ListPost(tx, take, offset, "published_at DESC")
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}