⚡ List post with minimal data
This commit is contained in:
parent
8aa627ef43
commit
2fbf7c4808
@ -32,6 +32,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
|
|||||||
posts := api.Group("/posts").Name("Posts API")
|
posts := api.Group("/posts").Name("Posts API")
|
||||||
{
|
{
|
||||||
posts.Get("/", listPost)
|
posts.Get("/", listPost)
|
||||||
|
posts.Get("/minimal", listPostMinimal)
|
||||||
posts.Get("/drafts", listDraftPost)
|
posts.Get("/drafts", listDraftPost)
|
||||||
posts.Get("/:postId", getPost)
|
posts.Get("/:postId", getPost)
|
||||||
posts.Post("/:postId/react", reactPost)
|
posts.Post("/:postId/react", reactPost)
|
||||||
|
@ -85,6 +85,59 @@ func listPost(c *fiber.Ctx) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func listPostMinimal(c *fiber.Ctx) error {
|
||||||
|
take := c.QueryInt("take", 0)
|
||||||
|
offset := c.QueryInt("offset", 0)
|
||||||
|
realmId := c.QueryInt("realmId", 0)
|
||||||
|
|
||||||
|
tx := services.FilterPostDraft(database.C)
|
||||||
|
|
||||||
|
if user, authenticated := c.Locals("user").(models.Account); authenticated {
|
||||||
|
tx = services.FilterPostWithUserContext(tx, &user)
|
||||||
|
} else {
|
||||||
|
tx = services.FilterPostWithUserContext(tx, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(c.Query("author")) > 0 {
|
||||||
|
var author models.Account
|
||||||
|
if err := database.C.Where(&models.Account{Name: c.Query("author")}).First(&author).Error; err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||||
|
}
|
||||||
|
tx = tx.Where("author_id = ?", author.ID)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(c.Query("category")) > 0 {
|
||||||
|
tx = services.FilterPostWithCategory(tx, c.Query("category"))
|
||||||
|
}
|
||||||
|
if len(c.Query("tag")) > 0 {
|
||||||
|
tx = services.FilterPostWithTag(tx, c.Query("tag"))
|
||||||
|
}
|
||||||
|
|
||||||
|
countTx := tx
|
||||||
|
count, err := services.CountPost(countTx)
|
||||||
|
if err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
items, err := services.ListPostMinimal(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 listDraftPost(c *fiber.Ctx) error {
|
func listDraftPost(c *fiber.Ctx) error {
|
||||||
take := c.QueryInt("take", 0)
|
take := c.QueryInt("take", 0)
|
||||||
offset := c.QueryInt("offset", 0)
|
offset := c.QueryInt("offset", 0)
|
||||||
|
@ -220,6 +220,22 @@ func ListPost(tx *gorm.DB, take int, offset int, order any, noReact ...bool) ([]
|
|||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ListPostMinimal(tx *gorm.DB, take int, offset int, order any) ([]*models.Post, error) {
|
||||||
|
if take > 500 {
|
||||||
|
take = 500
|
||||||
|
}
|
||||||
|
|
||||||
|
var items []*models.Post
|
||||||
|
if err := tx.
|
||||||
|
Limit(take).Offset(offset).
|
||||||
|
Order(order).
|
||||||
|
Find(&items).Error; err != nil {
|
||||||
|
return items, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return items, nil
|
||||||
|
}
|
||||||
|
|
||||||
func EnsurePostCategoriesAndTags(item models.Post) (models.Post, error) {
|
func EnsurePostCategoriesAndTags(item models.Post) (models.Post, error) {
|
||||||
var err error
|
var err error
|
||||||
for idx, category := range item.Categories {
|
for idx, category := range item.Categories {
|
||||||
|
Loading…
Reference in New Issue
Block a user