♻️ Use unified post list api

This commit is contained in:
LittleSheep 2024-02-09 11:43:21 +08:00
parent 86e184a803
commit 798e78ff8e
4 changed files with 11 additions and 40 deletions

View File

@ -54,11 +54,18 @@ func listPost(c *fiber.Ctx) error {
take := c.QueryInt("take", 0) take := c.QueryInt("take", 0)
offset := c.QueryInt("offset", 0) offset := c.QueryInt("offset", 0)
realmId := c.QueryInt("realmId", 0)
tx := database.C. tx := database.C.
Where("realm_id IS NULL").
Where("published_at <= ? OR published_at IS NULL", time.Now()). Where("published_at <= ? OR published_at IS NULL", time.Now()).
Order("created_at desc") 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 var author models.Account
if len(c.Query("authorId")) > 0 { if len(c.Query("authorId")) > 0 {
if err := database.C.Where(&models.Account{Name: c.Query("authorId")}).First(&author).Error; err != nil { if err := database.C.Where(&models.Account{Name: c.Query("authorId")}).First(&author).Error; err != nil {

View File

@ -5,8 +5,6 @@ import (
"code.smartsheep.studio/hydrogen/interactive/pkg/models" "code.smartsheep.studio/hydrogen/interactive/pkg/models"
"code.smartsheep.studio/hydrogen/interactive/pkg/services" "code.smartsheep.studio/hydrogen/interactive/pkg/services"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/samber/lo"
"time"
) )
func getRealm(c *fiber.Ctx) error { func getRealm(c *fiber.Ctx) error {
@ -42,40 +40,6 @@ func listOwnedRealm(c *fiber.Ctx) error {
return c.JSON(realms) 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 { func createRealm(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account) user := c.Locals("principal").(models.Account)
if user.PowerLevel < 10 { if user.PowerLevel < 10 {

View File

@ -79,7 +79,6 @@ func NewServer() {
api.Get("/realms", listRealm) api.Get("/realms", listRealm)
api.Get("/realms/me", auth, listOwnedRealm) api.Get("/realms/me", auth, listOwnedRealm)
api.Get("/realms/:realmId", getRealm) api.Get("/realms/:realmId", getRealm)
api.Get("/realms/:realmId/posts", listPostInRealm)
api.Post("/realms", auth, createRealm) api.Post("/realms", auth, createRealm)
api.Put("/realms/:realmId", auth, editRealm) api.Put("/realms/:realmId", auth, editRealm)
api.Delete("/realms/:realmId", auth, deleteRealm) api.Delete("/realms/:realmId", auth, deleteRealm)

View File

@ -29,9 +29,10 @@ export default function RealmPage() {
async function readPosts(pn?: number) { async function readPosts(pn?: number) {
if (pn) setPage(pn); 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(), take: (10).toString(),
offset: ((page() - 1) * 10).toString() offset: ((page() - 1) * 10).toString(),
realmId: params["realmId"],
})); }));
if (res.status !== 200) { if (res.status !== 200) {
setError(await res.text()); setError(await res.text());