2024-06-22 09:29:53 +00:00
|
|
|
package api
|
2024-05-15 11:45:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-11-02 05:41:51 +00:00
|
|
|
"git.solsynth.dev/hypernet/interactive/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hypernet/interactive/pkg/internal/models"
|
|
|
|
"git.solsynth.dev/hypernet/interactive/pkg/internal/services"
|
2024-05-15 11:45:49 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
2024-07-03 14:16:23 +00:00
|
|
|
func listPostReplies(c *fiber.Ctx) error {
|
2024-05-15 11:45:49 +00:00
|
|
|
take := c.QueryInt("take", 0)
|
|
|
|
offset := c.QueryInt("offset", 0)
|
|
|
|
|
|
|
|
tx := database.C
|
|
|
|
var post models.Post
|
2024-07-23 09:59:11 +00:00
|
|
|
if err := database.C.Where("id = ?", c.Params("postId")).First(&post).Error; err != nil {
|
2024-05-15 11:45:49 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to find post: %v", err))
|
|
|
|
} else {
|
|
|
|
tx = services.FilterPostReply(tx, post.ID)
|
|
|
|
}
|
|
|
|
|
2024-07-26 08:15:08 +00:00
|
|
|
if len(c.Query("author")) > 0 {
|
2024-10-31 14:41:32 +00:00
|
|
|
var author models.Publisher
|
2024-10-31 14:48:51 +00:00
|
|
|
if err := database.C.Where("name = ?", c.Query("author")).First(&author).Error; err != nil {
|
2024-05-15 11:45:49 +00:00
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
2024-11-02 15:47:44 +00:00
|
|
|
tx = tx.Where("publisher_id = ?", author.ID)
|
2024-05-15 11:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"))
|
|
|
|
}
|
|
|
|
|
|
|
|
count, err := services.CountPost(tx)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-07-23 08:12:19 +00:00
|
|
|
items, err := services.ListPost(tx, take, offset, "published_at DESC")
|
2024-05-15 11:45:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"count": count,
|
|
|
|
"data": items,
|
|
|
|
})
|
|
|
|
}
|
2024-09-16 13:24:48 +00:00
|
|
|
|
|
|
|
func listPostFeaturedReply(c *fiber.Ctx) error {
|
|
|
|
take := c.QueryInt("take", 0)
|
|
|
|
take = max(1, min(take, 3))
|
|
|
|
|
|
|
|
tx := database.C
|
|
|
|
var post models.Post
|
|
|
|
if err := database.C.Where("id = ?", c.Params("postId")).First(&post).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to find post: %v", err))
|
|
|
|
} else {
|
|
|
|
tx = services.FilterPostReply(tx, post.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.Query("author")) > 0 {
|
2024-10-31 14:41:32 +00:00
|
|
|
var author models.Publisher
|
2024-10-31 14:48:51 +00:00
|
|
|
if err := database.C.Where("name = ?", c.Query("author")).First(&author).Error; err != nil {
|
2024-09-16 13:24:48 +00:00
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
2024-11-02 15:47:44 +00:00
|
|
|
tx = tx.Where("publisher_id = ?", author.ID)
|
2024-09-16 13:24:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"))
|
|
|
|
}
|
|
|
|
|
|
|
|
items, err := services.ListPost(tx, take, 0, "(COALESCE(total_upvote, 0) - COALESCE(total_downvote, 0)) DESC, published_at DESC")
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(items)
|
|
|
|
}
|