2024-06-22 09:29:53 +00:00
|
|
|
package api
|
2024-03-03 11:10:08 +00:00
|
|
|
|
|
|
|
import (
|
2024-06-22 09:29:53 +00:00
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
|
|
|
|
"git.solsynth.dev/hydrogen/interactive/pkg/internal/services"
|
2024-03-03 11:10:08 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func listFeed(c *fiber.Ctx) error {
|
|
|
|
take := c.QueryInt("take", 0)
|
|
|
|
offset := c.QueryInt("offset", 0)
|
2024-05-15 11:45:49 +00:00
|
|
|
realmId := c.QueryInt("realmId", 0)
|
2024-03-03 11:10:08 +00:00
|
|
|
|
2024-05-15 11:45:49 +00:00
|
|
|
tx := database.C
|
|
|
|
if realmId > 0 {
|
|
|
|
tx = services.FilterWithRealm(tx, uint(realmId))
|
2024-03-03 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.Query("authorId")) > 0 {
|
2024-05-15 11:45:49 +00:00
|
|
|
var author models.Account
|
2024-03-03 11:10:08 +00:00
|
|
|
if err := database.C.Where(&models.Account{Name: c.Query("authorId")}).First(&author).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
2024-05-15 11:45:49 +00:00
|
|
|
tx = tx.Where("author_id = ?", author.ID)
|
2024-03-03 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
2024-05-15 11:45:49 +00:00
|
|
|
if len(c.Query("category")) > 0 {
|
|
|
|
tx = services.FilterPostWithCategory(tx, c.Query("category"))
|
2024-03-25 11:40:43 +00:00
|
|
|
}
|
2024-05-15 11:45:49 +00:00
|
|
|
if len(c.Query("tag")) > 0 {
|
|
|
|
tx = services.FilterPostWithTag(tx, c.Query("tag"))
|
2024-03-03 14:57:17 +00:00
|
|
|
}
|
|
|
|
|
2024-05-15 11:45:49 +00:00
|
|
|
count, err := services.CountPost(tx)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
2024-03-10 10:38:42 +00:00
|
|
|
}
|
|
|
|
|
2024-05-15 11:45:49 +00:00
|
|
|
items, err := services.ListPost(tx, take, offset)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
2024-03-03 13:24:08 +00:00
|
|
|
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"count": count,
|
2024-05-15 11:45:49 +00:00
|
|
|
"data": items,
|
2024-03-03 13:24:08 +00:00
|
|
|
})
|
2024-03-03 11:10:08 +00:00
|
|
|
}
|