Frontend move to union feed

This commit is contained in:
2024-03-03 21:24:08 +08:00
parent e1822e5363
commit 1725724758
15 changed files with 199 additions and 47 deletions

View File

@ -22,11 +22,11 @@ func contextArticle() *services.PostTypeContext[models.Article] {
}
func getArticle(c *fiber.Ctx) error {
id, _ := c.ParamsInt("articleId", 0)
alias := c.Params("articleId")
mx := contextArticle().FilterPublishedAt(time.Now())
item, err := mx.Get(uint(id))
item, err := mx.GetViaAlias(alias)
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}

View File

@ -23,11 +23,11 @@ func contextComment() *services.PostTypeContext[models.Comment] {
}
func getComment(c *fiber.Ctx) error {
id, _ := c.ParamsInt("commentId", 0)
alias := c.Params("commentId")
mx := contextComment().FilterPublishedAt(time.Now())
item, err := mx.Get(uint(id))
item, err := mx.GetViaAlias(alias)
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}

View File

@ -12,6 +12,7 @@ import (
type FeedItem struct {
models.BaseModel
Alias string `json:"alias"`
Title string `json:"title"`
Description string `json:"description"`
Content string `json:"content"`
@ -25,8 +26,8 @@ type FeedItem struct {
}
const (
queryArticle = "id, created_at, updated_at, title, content, description, realm_id, author_id, 'article' as model_type"
queryMoment = "id, created_at, updated_at, NULL as title, content, NULL as description, realm_id, author_id, 'moment' as model_type"
queryArticle = "id, created_at, updated_at, alias, title, NULL as content, description, realm_id, author_id, 'article' as model_type"
queryMoment = "id, created_at, updated_at, alias, NULL as title, content, NULL as description, realm_id, author_id, 'moment' as model_type"
)
func listFeed(c *fiber.Ctx) error {
@ -83,5 +84,14 @@ func listFeed(c *fiber.Ctx) error {
offset,
).Scan(&result)
return c.JSON(result)
var count int64
database.C.Raw(`SELECT COUNT(*) FROM (? UNION ALL ?) as feed`,
database.C.Select(queryArticle).Model(&models.Article{}),
database.C.Select(queryMoment).Model(&models.Moment{}),
).Scan(&count)
return c.JSON(fiber.Map{
"count": count,
"data": result,
})
}

View File

@ -22,11 +22,11 @@ func contextMoment() *services.PostTypeContext[models.Moment] {
}
func getMoment(c *fiber.Ctx) error {
id, _ := c.ParamsInt("momentId", 0)
alias := c.Params("momentId")
mx := contextMoment().FilterPublishedAt(time.Now())
item, err := mx.Get(uint(id))
item, err := mx.GetViaAlias(alias)
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}