From 615fe3ff6c19a37ac3c36824728c02119d590827 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 9 Nov 2024 00:47:19 +0800 Subject: [PATCH] :boom: Unified the publisher api --- pkg/internal/http/api/index.go | 4 ++-- pkg/internal/http/api/publishers_api.go | 8 ++++---- pkg/internal/services/posts.go | 11 +++++------ pkg/internal/services/publishers.go | 8 ++++++++ 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/pkg/internal/http/api/index.go b/pkg/internal/http/api/index.go index 9066fb6..10d40ad 100644 --- a/pkg/internal/http/api/index.go +++ b/pkg/internal/http/api/index.go @@ -14,8 +14,8 @@ func MapAPIs(app *fiber.App, baseURL string) { publishers.Post("/organization", createOrganizationPublisher) publishers.Get("/:name/pins", listPinnedPost) publishers.Get("/:name", getPublisher) - publishers.Put("/:publisherId", editPublisher) - publishers.Delete("/:publisherId", deletePublisher) + publishers.Put("/:name", editPublisher) + publishers.Delete("/:name", deletePublisher) } recommendations := api.Group("/recommendations").Name("Recommendations API") diff --git a/pkg/internal/http/api/publishers_api.go b/pkg/internal/http/api/publishers_api.go index 01be7aa..c7f6493 100644 --- a/pkg/internal/http/api/publishers_api.go +++ b/pkg/internal/http/api/publishers_api.go @@ -109,8 +109,8 @@ func editPublisher(c *fiber.Ctx) error { } user := c.Locals("user").(authm.Account) - id, _ := c.ParamsInt("publisherId", 0) - publisher, err := services.GetPublisher(uint(id), user.ID) + name := c.Params("name") + publisher, err := services.GetPublisherByName(name, user.ID) if err != nil { return fiber.NewError(fiber.StatusNotFound, err.Error()) } @@ -150,8 +150,8 @@ func deletePublisher(c *fiber.Ctx) error { } user := c.Locals("user").(authm.Account) - id, _ := c.ParamsInt("publisherId", 0) - publisher, err := services.GetPublisher(uint(id), user.ID) + name := c.Params("name") + publisher, err := services.GetPublisherByName(name, user.ID) if err != nil { return fiber.NewError(fiber.StatusNotFound, err.Error()) } diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index 186e9d8..e4c6dd8 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -110,14 +110,13 @@ func PreloadGeneral(tx *gorm.DB) *gorm.DB { return tx. Preload("Tags"). Preload("Categories"). - Preload("Realm"). - Preload("Author"). + Preload("Publisher"). Preload("ReplyTo"). - Preload("ReplyTo.Author"). + Preload("ReplyTo.Publisher"). Preload("ReplyTo.Tags"). Preload("ReplyTo.Categories"). Preload("RepostTo"). - Preload("RepostTo.Author"). + Preload("RepostTo.Publisher"). Preload("RepostTo.Tags"). Preload("RepostTo.Categories") } @@ -327,7 +326,7 @@ func NewPost(user models.Publisher, item models.Post) (models.Post, error) { var op models.Post if err := database.C. Where("id = ?", item.ReplyID). - Preload("Author"). + Preload("Publisher"). First(&op).Error; err == nil { if op.Publisher.AccountID != nil && op.Publisher.ID != user.ID { log.Debug().Uint("user", *op.Publisher.AccountID).Msg("Notifying the original poster their post got replied...") @@ -406,7 +405,7 @@ func ReactPost(user authm.Account, reaction models.Reaction) (bool, models.React var op models.Post if err := database.C. Where("id = ?", reaction.PostID). - Preload("Author"). + Preload("Publisher"). First(&op).Error; err != nil { return true, reaction, err } diff --git a/pkg/internal/services/publishers.go b/pkg/internal/services/publishers.go index a0f8d50..55265ea 100644 --- a/pkg/internal/services/publishers.go +++ b/pkg/internal/services/publishers.go @@ -15,6 +15,14 @@ func GetPublisher(id uint, userID uint) (models.Publisher, error) { return publisher, nil } +func GetPublisherByName(name string, userID uint) (models.Publisher, error) { + var publisher models.Publisher + if err := database.C.Where("name = ? AND account_id = ?", name, userID).First(&publisher).Error; err != nil { + return publisher, fmt.Errorf("unable to get publisher: %v", err) + } + return publisher, nil +} + func CreatePersonalPublisher(user authm.Account) (models.Publisher, error) { var publisher models.Publisher var count int64