diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go index a8fb759..31fac61 100644 --- a/pkg/internal/server/api/index.go +++ b/pkg/internal/server/api/index.go @@ -32,6 +32,7 @@ func MapAPIs(app *fiber.App, baseURL string) { posts := api.Group("/posts").Name("Posts API") { posts.Get("/", listPost) + posts.Get("/minimal", listPostMinimal) posts.Get("/drafts", listDraftPost) posts.Get("/:postId", getPost) posts.Post("/:postId/react", reactPost) diff --git a/pkg/internal/server/api/posts_api.go b/pkg/internal/server/api/posts_api.go index 608d71c..f5f62a1 100644 --- a/pkg/internal/server/api/posts_api.go +++ b/pkg/internal/server/api/posts_api.go @@ -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 { take := c.QueryInt("take", 0) offset := c.QueryInt("offset", 0) diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index 01dbd2c..272b3e0 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -220,6 +220,22 @@ func ListPost(tx *gorm.DB, take int, offset int, order any, noReact ...bool) ([] 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) { var err error for idx, category := range item.Categories {