Get today's news

This commit is contained in:
LittleSheep 2025-01-26 13:20:50 +08:00
parent 690bbd4b43
commit 52bd374eeb
2 changed files with 27 additions and 0 deletions

View File

@ -19,6 +19,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
news := api.Group("/news").Name("News")
{
news.Get("/today", getTodayNews)
news.Get("/", listNewsArticles)
news.Get("/:hash", getNewsArticle)
}

View File

@ -1,11 +1,37 @@
package api
import (
"time"
"git.solsynth.dev/hypernet/reader/pkg/internal/database"
"git.solsynth.dev/hypernet/reader/pkg/internal/models"
"github.com/gofiber/fiber/v2"
)
func getTodayNews(c *fiber.Ctx) error {
tx := database.C
today := time.Now().Format("2006-01-02")
tx = tx.Where("DATE(COALESCE(published_at, created_at)) = ?", today)
var count int64
countTx := tx
if err := countTx.Model(&models.NewsArticle{}).Count(&count).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
var article models.NewsArticle
if err := tx.
Omit("Content").Order("COALESCE(published_at, created_at) DESC").
First(&article).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
return c.JSON(fiber.Map{
"count": count,
"data": article,
})
}
func listNewsArticles(c *fiber.Ctx) error {
take := c.QueryInt("take", 0)
offset := c.QueryInt("offset", 0)