2024-07-25 14:02:26 +00:00
|
|
|
package api
|
2024-07-25 14:12:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func listAttachment(c *fiber.Ctx) error {
|
|
|
|
take := c.QueryInt("take", 0)
|
|
|
|
offset := c.QueryInt("offset", 0)
|
|
|
|
|
|
|
|
if take > 100 {
|
|
|
|
take = 100
|
|
|
|
}
|
|
|
|
|
2024-08-02 19:22:45 +00:00
|
|
|
tx := database.C
|
2024-07-25 14:12:00 +00:00
|
|
|
|
2024-08-02 13:52:32 +00:00
|
|
|
if len(c.Query("id")) > 0 {
|
|
|
|
tx = tx.Where("id IN ?", strings.Split(c.Query("id"), ","))
|
2024-08-02 19:22:45 +00:00
|
|
|
} else {
|
|
|
|
// Do sort this when doesn't filter by the id
|
|
|
|
// Because the sort will mess up the result
|
|
|
|
tx = tx.Order("created_at DESC")
|
2024-08-02 13:52:32 +00:00
|
|
|
}
|
|
|
|
|
2024-07-26 08:42:16 +00:00
|
|
|
if len(c.Query("author")) > 0 {
|
|
|
|
var author models.Account
|
|
|
|
if err := database.C.Where("name = ?", c.Query("author")).First(&author).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
|
|
|
tx = tx.Where("account_id = ?", author.ID)
|
|
|
|
}
|
2024-07-25 14:12:00 +00:00
|
|
|
}
|
2024-07-26 08:32:01 +00:00
|
|
|
|
2024-07-26 14:46:33 +00:00
|
|
|
if usage := c.Query("usage"); len(usage) > 0 {
|
|
|
|
tx = tx.Where("usage IN ?", strings.Split(usage, " "))
|
2024-07-25 14:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var count int64
|
|
|
|
countTx := tx
|
|
|
|
if err := countTx.Model(&models.Attachment{}).Count(&count).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
var attachments []models.Attachment
|
2024-07-26 16:23:22 +00:00
|
|
|
if err := tx.Offset(offset).Limit(take).Preload("Account").Find(&attachments).Error; err != nil {
|
2024-07-25 14:12:00 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"count": count,
|
|
|
|
"data": attachments,
|
|
|
|
})
|
|
|
|
}
|