✨ Able to filter news by source
This commit is contained in:
parent
d9fe0c6789
commit
01b0cbce3e
@ -9,15 +9,23 @@ import (
|
|||||||
func listNewsArticles(c *fiber.Ctx) error {
|
func listNewsArticles(c *fiber.Ctx) error {
|
||||||
take := c.QueryInt("take", 0)
|
take := c.QueryInt("take", 0)
|
||||||
offset := c.QueryInt("offset", 0)
|
offset := c.QueryInt("offset", 0)
|
||||||
|
source := c.Query("source")
|
||||||
|
|
||||||
|
tx := database.C
|
||||||
|
|
||||||
|
if len(source) > 0 {
|
||||||
|
tx = tx.Where("source = ?", source)
|
||||||
|
}
|
||||||
|
|
||||||
var count int64
|
var count int64
|
||||||
if err := database.C.Model(&models.NewsArticle{}).Count(&count).Error; err != nil {
|
countTx := tx
|
||||||
|
if err := countTx.Model(&models.NewsArticle{}).Count(&count).Error; err != nil {
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
var articles []models.NewsArticle
|
var articles []models.NewsArticle
|
||||||
if err := database.C.Limit(take).Offset(offset).
|
if err := tx.Limit(take).Offset(offset).
|
||||||
Omit("Content").Order("created_at DESC").
|
Omit("Content").Order("COALESCE(published_at, created_at) DESC").
|
||||||
Find(&articles).Error; err != nil {
|
Find(&articles).Error; err != nil {
|
||||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user