⚗️ Testing with the new post listing v2

This commit is contained in:
2025-03-29 22:38:43 +08:00
parent 7a7d57c0d7
commit 3cd102046a
10 changed files with 220 additions and 21 deletions

View File

@ -20,7 +20,7 @@ func getCategory(c *fiber.Ctx) error {
}
func listCategories(c *fiber.Ctx) error {
take := c.QueryInt("take", 0)
take := c.QueryInt("take", 10)
offset := c.QueryInt("offset", 0)
probe := c.Query("probe")

View File

@ -59,6 +59,7 @@ func MapControllers(app *fiber.App, baseURL string) {
posts := api.Group("/posts").Name("Posts API")
{
posts.Get("/", listPost)
posts.Get("/v2", listPostV2)
posts.Get("/search", searchPost)
posts.Get("/minimal", listPostMinimal)
posts.Get("/drafts", listDraftPost)

View File

@ -15,6 +15,7 @@ import (
"git.solsynth.dev/hypernet/interactive/pkg/internal/http/exts"
"git.solsynth.dev/hypernet/interactive/pkg/internal/models"
"git.solsynth.dev/hypernet/interactive/pkg/internal/services"
"git.solsynth.dev/hypernet/interactive/pkg/internal/services/queries"
"github.com/gofiber/fiber/v2"
"github.com/samber/lo"
)
@ -63,7 +64,7 @@ func getPost(c *fiber.Ctx) error {
}
func searchPost(c *fiber.Ctx) error {
take := c.QueryInt("take", 0)
take := c.QueryInt("take", 10)
offset := c.QueryInt("offset", 0)
tx := database.C
@ -113,7 +114,7 @@ func searchPost(c *fiber.Ctx) error {
}
func listPost(c *fiber.Ctx) error {
take := c.QueryInt("take", 0)
take := c.QueryInt("take", 10)
offset := c.QueryInt("offset", 0)
tx := database.C
@ -153,8 +154,47 @@ func listPost(c *fiber.Ctx) error {
})
}
func listPostV2(c *fiber.Ctx) error {
take := c.QueryInt("take", 10)
offset := c.QueryInt("offset", 0)
tx := database.C
var err error
if tx, err = services.UniversalPostFilter(c, tx); err != nil {
return err
}
var userId *uint
if user, authenticated := c.Locals("user").(authm.Account); authenticated {
userId = &user.ID
}
countTx := tx
count, err := services.CountPost(countTx)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
items, err := queries.ListPostV2(tx, take, offset, "published_at DESC", userId)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
if c.QueryBool("truncate", true) {
for _, item := range items {
item = services.TruncatePostContent(item)
}
}
return c.JSON(fiber.Map{
"count": count,
"data": items,
})
}
func listPostMinimal(c *fiber.Ctx) error {
take := c.QueryInt("take", 0)
take := c.QueryInt("take", 10)
offset := c.QueryInt("offset", 0)
tx := database.C
@ -190,7 +230,7 @@ func listPostMinimal(c *fiber.Ctx) error {
}
func listDraftPost(c *fiber.Ctx) error {
take := c.QueryInt("take", 0)
take := c.QueryInt("take", 10)
offset := c.QueryInt("offset", 0)
if err := sec.EnsureAuthenticated(c); err != nil {

View File

@ -46,7 +46,7 @@ func listRecommendation(c *fiber.Ctx) error {
}
func listRecommendationShuffle(c *fiber.Ctx) error {
take := c.QueryInt("take", 0)
take := c.QueryInt("take", 10)
offset := c.QueryInt("offset", 0)
tx := database.C

View File

@ -11,7 +11,7 @@ import (
)
func listPostReplies(c *fiber.Ctx) error {
take := c.QueryInt("take", 0)
take := c.QueryInt("take", 10)
offset := c.QueryInt("offset", 0)
tx := database.C
@ -59,7 +59,7 @@ func listPostReplies(c *fiber.Ctx) error {
}
func listPostFeaturedReply(c *fiber.Ctx) error {
take := c.QueryInt("take", 0)
take := c.QueryInt("take", 10)
take = max(1, min(take, 3))
var userId *uint

View File

@ -18,7 +18,7 @@ func getTag(c *fiber.Ctx) error {
}
func listTags(c *fiber.Ctx) error {
take := c.QueryInt("take", 0)
take := c.QueryInt("take", 10)
offset := c.QueryInt("offset", 0)
probe := c.Query("probe")