diff --git a/pkg/server/posts_api.go b/pkg/server/posts_api.go index 1dee6d9..26f6418 100644 --- a/pkg/server/posts_api.go +++ b/pkg/server/posts_api.go @@ -54,11 +54,18 @@ func listPost(c *fiber.Ctx) error { take := c.QueryInt("take", 0) offset := c.QueryInt("offset", 0) + realmId := c.QueryInt("realmId", 0) + tx := database.C. - Where("realm_id IS NULL"). Where("published_at <= ? OR published_at IS NULL", time.Now()). Order("created_at desc") + if realmId > 0 { + tx = tx.Where(&models.Post{RealmID: lo.ToPtr(uint(realmId))}) + } else { + tx = tx.Where("realm_id IS NULL") + } + var author models.Account if len(c.Query("authorId")) > 0 { if err := database.C.Where(&models.Account{Name: c.Query("authorId")}).First(&author).Error; err != nil { diff --git a/pkg/server/realms_api.go b/pkg/server/realms_api.go index 928702d..e48c969 100644 --- a/pkg/server/realms_api.go +++ b/pkg/server/realms_api.go @@ -5,8 +5,6 @@ import ( "code.smartsheep.studio/hydrogen/interactive/pkg/models" "code.smartsheep.studio/hydrogen/interactive/pkg/services" "github.com/gofiber/fiber/v2" - "github.com/samber/lo" - "time" ) func getRealm(c *fiber.Ctx) error { @@ -42,40 +40,6 @@ func listOwnedRealm(c *fiber.Ctx) error { return c.JSON(realms) } -func listPostInRealm(c *fiber.Ctx) error { - take := c.QueryInt("take", 0) - offset := c.QueryInt("offset", 0) - authorId := c.QueryInt("authorId", 0) - - realmId, _ := c.ParamsInt("realmId", 0) - - tx := database.C. - Where(&models.Post{RealmID: lo.ToPtr(uint(realmId))}). - Where("published_at <= ? OR published_at IS NULL", time.Now()). - Order("created_at desc") - - if authorId > 0 { - tx = tx.Where(&models.Post{AuthorID: uint(authorId)}) - } - - var count int64 - if err := tx. - Model(&models.Post{}). - Count(&count).Error; err != nil { - return fiber.NewError(fiber.StatusInternalServerError, err.Error()) - } - - posts, err := services.ListPost(tx, take, offset) - if err != nil { - return fiber.NewError(fiber.StatusBadRequest, err.Error()) - } - - return c.JSON(fiber.Map{ - "count": count, - "data": posts, - }) -} - func createRealm(c *fiber.Ctx) error { user := c.Locals("principal").(models.Account) if user.PowerLevel < 10 { diff --git a/pkg/server/startup.go b/pkg/server/startup.go index 1dceea4..0edae32 100644 --- a/pkg/server/startup.go +++ b/pkg/server/startup.go @@ -79,7 +79,6 @@ func NewServer() { api.Get("/realms", listRealm) api.Get("/realms/me", auth, listOwnedRealm) api.Get("/realms/:realmId", getRealm) - api.Get("/realms/:realmId/posts", listPostInRealm) api.Post("/realms", auth, createRealm) api.Put("/realms/:realmId", auth, editRealm) api.Delete("/realms/:realmId", auth, deleteRealm) diff --git a/pkg/view/src/pages/realms/realm.tsx b/pkg/view/src/pages/realms/realm.tsx index 2492c00..80451c3 100644 --- a/pkg/view/src/pages/realms/realm.tsx +++ b/pkg/view/src/pages/realms/realm.tsx @@ -29,9 +29,10 @@ export default function RealmPage() { async function readPosts(pn?: number) { if (pn) setPage(pn); - const res = await fetch(`/api/realms/${params["realmId"]}/posts?` + new URLSearchParams({ + const res = await fetch(`/api/posts?` + new URLSearchParams({ take: (10).toString(), - offset: ((page() - 1) * 10).toString() + offset: ((page() - 1) * 10).toString(), + realmId: params["realmId"], })); if (res.status !== 200) { setError(await res.text());