From ad3257dabf4fd526203cf723b3847e1c94258755 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Tue, 9 Jul 2024 21:07:46 +0800 Subject: [PATCH] :sparkles: Mixed draft list API --- pkg/internal/server/api/feed_api.go | 66 +++++++++++++++++++++++++++++ pkg/internal/server/api/index.go | 1 + 2 files changed, 67 insertions(+) diff --git a/pkg/internal/server/api/feed_api.go b/pkg/internal/server/api/feed_api.go index dc3121c..b6ab05e 100644 --- a/pkg/internal/server/api/feed_api.go +++ b/pkg/internal/server/api/feed_api.go @@ -6,6 +6,7 @@ import ( "time" "git.solsynth.dev/hydrogen/interactive/pkg/internal/database" + "git.solsynth.dev/hydrogen/interactive/pkg/internal/gap" "git.solsynth.dev/hydrogen/interactive/pkg/internal/models" "git.solsynth.dev/hydrogen/interactive/pkg/internal/services" "github.com/gofiber/fiber/v2" @@ -105,3 +106,68 @@ func listFeed(c *fiber.Ctx) error { "data": feed, }) } + +func listDraftMixed(c *fiber.Ctx) error { + take := c.QueryInt("take", 0) + offset := c.QueryInt("offset", 0) + + if err := gap.H.EnsureAuthenticated(c); err != nil { + return err + } + user := c.Locals("user").(models.Account) + + postTx := services.FilterPostWithAuthorDraft(database.C, user.ID) + articleTx := services.FilterArticleWithAuthorDraft(database.C, user.ID) + + postCountTx := postTx + articleCountTx := articleTx + + postCount, err := services.CountPost(postCountTx) + if err != nil { + return fiber.NewError(fiber.StatusInternalServerError, err.Error()) + } + articleCount, err := services.CountArticle(articleCountTx) + if err != nil { + return fiber.NewError(fiber.StatusInternalServerError, err.Error()) + } + + postItems, err := services.ListPost(postTx, take, offset) + if err != nil { + return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } + articleItems, err := services.ListArticle(articleTx, take, offset) + if err != nil { + return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } + + var feed []FeedRecord + + encodeToFeed := func(t string, in any, createdAt time.Time) FeedRecord { + var result map[string]any + raw, _ := jsoniter.Marshal(in) + jsoniter.Unmarshal(raw, &result) + + return FeedRecord{ + Type: t, + Data: result, + CreatedAt: createdAt, + } + } + + for _, post := range postItems { + feed = append(feed, encodeToFeed("post", post, post.CreatedAt)) + } + + for _, article := range articleItems { + feed = append(feed, encodeToFeed("article", article, article.CreatedAt)) + } + + sort.Slice(feed, func(i, j int) bool { + return feed[i].CreatedAt.After(feed[j].CreatedAt) + }) + + return c.JSON(fiber.Map{ + "count": postCount + articleCount, + "data": feed, + }) +} diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go index 27214be..1fb7504 100644 --- a/pkg/internal/server/api/index.go +++ b/pkg/internal/server/api/index.go @@ -14,6 +14,7 @@ func MapAPIs(app *fiber.App) { drafts := api.Group("/drafts").Name("Draft box API") { + drafts.Get("/", listDraftMixed) drafts.Get("/posts", listDraftPost) drafts.Get("/articles", listDraftArticle) }