💥 Unified the publisher api

This commit is contained in:
LittleSheep 2024-11-09 00:47:19 +08:00
parent f9a01bff70
commit 615fe3ff6c
4 changed files with 19 additions and 12 deletions

View File

@ -14,8 +14,8 @@ func MapAPIs(app *fiber.App, baseURL string) {
publishers.Post("/organization", createOrganizationPublisher) publishers.Post("/organization", createOrganizationPublisher)
publishers.Get("/:name/pins", listPinnedPost) publishers.Get("/:name/pins", listPinnedPost)
publishers.Get("/:name", getPublisher) publishers.Get("/:name", getPublisher)
publishers.Put("/:publisherId", editPublisher) publishers.Put("/:name", editPublisher)
publishers.Delete("/:publisherId", deletePublisher) publishers.Delete("/:name", deletePublisher)
} }
recommendations := api.Group("/recommendations").Name("Recommendations API") recommendations := api.Group("/recommendations").Name("Recommendations API")

View File

@ -109,8 +109,8 @@ func editPublisher(c *fiber.Ctx) error {
} }
user := c.Locals("user").(authm.Account) user := c.Locals("user").(authm.Account)
id, _ := c.ParamsInt("publisherId", 0) name := c.Params("name")
publisher, err := services.GetPublisher(uint(id), user.ID) publisher, err := services.GetPublisherByName(name, user.ID)
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error()) return fiber.NewError(fiber.StatusNotFound, err.Error())
} }
@ -150,8 +150,8 @@ func deletePublisher(c *fiber.Ctx) error {
} }
user := c.Locals("user").(authm.Account) user := c.Locals("user").(authm.Account)
id, _ := c.ParamsInt("publisherId", 0) name := c.Params("name")
publisher, err := services.GetPublisher(uint(id), user.ID) publisher, err := services.GetPublisherByName(name, user.ID)
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error()) return fiber.NewError(fiber.StatusNotFound, err.Error())
} }

View File

@ -110,14 +110,13 @@ func PreloadGeneral(tx *gorm.DB) *gorm.DB {
return tx. return tx.
Preload("Tags"). Preload("Tags").
Preload("Categories"). Preload("Categories").
Preload("Realm"). Preload("Publisher").
Preload("Author").
Preload("ReplyTo"). Preload("ReplyTo").
Preload("ReplyTo.Author"). Preload("ReplyTo.Publisher").
Preload("ReplyTo.Tags"). Preload("ReplyTo.Tags").
Preload("ReplyTo.Categories"). Preload("ReplyTo.Categories").
Preload("RepostTo"). Preload("RepostTo").
Preload("RepostTo.Author"). Preload("RepostTo.Publisher").
Preload("RepostTo.Tags"). Preload("RepostTo.Tags").
Preload("RepostTo.Categories") Preload("RepostTo.Categories")
} }
@ -327,7 +326,7 @@ func NewPost(user models.Publisher, item models.Post) (models.Post, error) {
var op models.Post var op models.Post
if err := database.C. if err := database.C.
Where("id = ?", item.ReplyID). Where("id = ?", item.ReplyID).
Preload("Author"). Preload("Publisher").
First(&op).Error; err == nil { First(&op).Error; err == nil {
if op.Publisher.AccountID != nil && op.Publisher.ID != user.ID { 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...") 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 var op models.Post
if err := database.C. if err := database.C.
Where("id = ?", reaction.PostID). Where("id = ?", reaction.PostID).
Preload("Author"). Preload("Publisher").
First(&op).Error; err != nil { First(&op).Error; err != nil {
return true, reaction, err return true, reaction, err
} }

View File

@ -15,6 +15,14 @@ func GetPublisher(id uint, userID uint) (models.Publisher, error) {
return publisher, nil 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) { func CreatePersonalPublisher(user authm.Account) (models.Publisher, error) {
var publisher models.Publisher var publisher models.Publisher
var count int64 var count int64