🐛 Fix filter by author requires internal id which is ridiculous

This commit is contained in:
LittleSheep 2024-07-26 16:32:01 +08:00
parent 6055ca4274
commit f91143ce9a

View File

@ -18,9 +18,13 @@ func listAttachment(c *fiber.Ctx) error {
tx := database.C
if author := c.QueryInt("authorId", 0); author > 0 {
tx = tx.Where("account_id = ?", author)
var author models.Account
if err := database.C.Where("name = ?", c.Query("author")).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
tx = tx.Where("account_id = ?", author.ID)
}
if usage := strings.Split(c.Query("usage"), " "); len(usage) > 0 {
tx = tx.Where("usage IN ?", usage)
}
@ -31,7 +35,7 @@ func listAttachment(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
var attachments []models.Attachment
if err := tx.Offset(offset).Limit(take).Find(&attachments).Error; err != nil {
if err := tx.Order("created_at DESC").Offset(offset).Limit(take).Find(&attachments).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}