✨ Recommendation API
This commit is contained in:
@ -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)
|
||||
|
@ -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())
|
||||
}
|
||||
|
75
pkg/internal/server/api/recommendation_api.go
Normal file
75
pkg/internal/server/api/recommendation_api.go
Normal 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,
|
||||
})
|
||||
}
|
@ -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())
|
||||
}
|
||||
|
Reference in New Issue
Block a user