✨ News getter api
This commit is contained in:
parent
a006e15682
commit
d91192a11a
@ -3,7 +3,7 @@ package api
|
|||||||
import (
|
import (
|
||||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||||
"git.solsynth.dev/hypernet/reader/pkg/internal/services"
|
"git.solsynth.dev/hypernet/reader/pkg/internal/services"
|
||||||
"github.com/gofiber/fiber"
|
"github.com/gofiber/fiber/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func adminTriggerScanTask(c *fiber.Ctx) error {
|
func adminTriggerScanTask(c *fiber.Ctx) error {
|
||||||
|
@ -8,12 +8,19 @@ import (
|
|||||||
func MapAPIs(app *fiber.App, baseURL string) {
|
func MapAPIs(app *fiber.App, baseURL string) {
|
||||||
api := app.Group(baseURL).Name("API")
|
api := app.Group(baseURL).Name("API")
|
||||||
{
|
{
|
||||||
|
api.Get("/well-known/sources", getNewsSources)
|
||||||
|
|
||||||
admin := api.Group("/admin").Name("Admin")
|
admin := api.Group("/admin").Name("Admin")
|
||||||
{
|
{
|
||||||
admin.Post("/scan", sec.ValidatorMiddleware, adminTriggerScanTask)
|
admin.Post("/scan", sec.ValidatorMiddleware, adminTriggerScanTask)
|
||||||
}
|
}
|
||||||
|
|
||||||
api.Get("/well-known/sources", getNewsSources)
|
|
||||||
api.Get("/link/*", getLinkMeta)
|
api.Get("/link/*", getLinkMeta)
|
||||||
|
|
||||||
|
news := api.Group("/news").Name("News")
|
||||||
|
{
|
||||||
|
news.Get("/", listNewsArticles)
|
||||||
|
news.Get("/:hash", getNewsArticle)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
38
pkg/internal/server/api/news_api.go
Normal file
38
pkg/internal/server/api/news_api.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.solsynth.dev/hypernet/reader/pkg/internal/database"
|
||||||
|
"git.solsynth.dev/hypernet/reader/pkg/internal/models"
|
||||||
|
"github.com/gofiber/fiber/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func listNewsArticles(c *fiber.Ctx) error {
|
||||||
|
take := c.QueryInt("take", 0)
|
||||||
|
offset := c.QueryInt("offset", 0)
|
||||||
|
|
||||||
|
var count int64
|
||||||
|
if err := database.C.Model(&models.NewsArticle{}).Count(&count).Error; err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
var articles []models.NewsArticle
|
||||||
|
if err := database.C.Limit(take).Offset(offset).Omit("Content").Find(&articles).Error; err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(fiber.Map{
|
||||||
|
"count": count,
|
||||||
|
"data": articles,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func getNewsArticle(c *fiber.Ctx) error {
|
||||||
|
hash := c.Params("hash")
|
||||||
|
|
||||||
|
var article models.NewsArticle
|
||||||
|
if err := database.C.Where("hash = ?", hash).First(&article).Error; err != nil {
|
||||||
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(article)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user