From 7b8ca225a8ae05523d8e285968d65defdecb0e7a Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Mon, 22 Jul 2024 13:41:35 +0800 Subject: [PATCH] :wastebasket: Remove feed api --- pkg/internal/server/api/feed_api.go | 131 ---------------------------- pkg/internal/server/api/index.go | 9 +- 2 files changed, 1 insertion(+), 139 deletions(-) delete mode 100644 pkg/internal/server/api/feed_api.go diff --git a/pkg/internal/server/api/feed_api.go b/pkg/internal/server/api/feed_api.go deleted file mode 100644 index 2264a9e..0000000 --- a/pkg/internal/server/api/feed_api.go +++ /dev/null @@ -1,131 +0,0 @@ -package api - -import ( - "fmt" - "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" - jsoniter "github.com/json-iterator/go" -) - -type FeedRecord struct { - Type string `json:"type"` - Data map[string]any `json:"data"` - CreatedAt time.Time `json:"created_at"` -} - -func listFeed(c *fiber.Ctx) error { - take := c.QueryInt("take", 0) - offset := c.QueryInt("offset", 0) - realmId := c.QueryInt("realmId", 0) - - postTx := services.FilterPostDraft(database.C) - - if realmId > 0 { - if realm, err := services.GetRealmWithExtID(uint(realmId)); err != nil { - return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("realm was not found: %v", err)) - } else { - postTx = services.FilterPostWithRealm(postTx, realm.ID) - } - } - - if len(c.Query("authorId")) > 0 { - var author models.Account - if err := database.C.Where(&models.Account{Name: c.Query("authorId")}).First(&author).Error; err != nil { - return fiber.NewError(fiber.StatusNotFound, err.Error()) - } - postTx = postTx.Where("author_id = ?", author.ID) - } - - if len(c.Query("category")) > 0 { - postTx = services.FilterPostWithCategory(postTx, c.Query("category")) - } - if len(c.Query("tag")) > 0 { - postTx = services.FilterPostWithTag(postTx, c.Query("tag")) - } - - postCountTx := postTx - - postCount, err := services.CountPost(postCountTx) - 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()) - } - - 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)) - } - - return c.JSON(fiber.Map{ - "count": postCount, - "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) - postCountTx := postTx - - postCount, err := services.CountPost(postCountTx) - 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()) - } - - 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)) - } - - return c.JSON(fiber.Map{ - "count": postCount, - "data": feed, - }) -} diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go index 5760e24..5ff97c7 100644 --- a/pkg/internal/server/api/index.go +++ b/pkg/internal/server/api/index.go @@ -10,14 +10,6 @@ func MapAPIs(app *fiber.App, baseURL string) { api.Get("/users/me", getUserinfo) api.Get("/users/:accountId", getOthersInfo) - api.Get("/feed", listFeed) - - drafts := api.Group("/drafts").Name("Draft box API") - { - drafts.Get("/", listDraftMixed) - drafts.Get("/posts", listDraftPost) - } - stories := api.Group("/stories").Name("Story API") { stories.Post("/", createStory) @@ -32,6 +24,7 @@ func MapAPIs(app *fiber.App, baseURL string) { posts := api.Group("/posts").Name("Posts API") { posts.Get("/", listPost) + posts.Get("/drafts", listDraftPost) posts.Get("/:postId", getPost) posts.Post("/:postId/react", reactPost) posts.Delete("/:postId", deletePost)