diff --git a/pkg/internal/server/api/boost_api.go b/pkg/internal/server/api/boost_api.go index 2a72569..6472b81 100644 --- a/pkg/internal/server/api/boost_api.go +++ b/pkg/internal/server/api/boost_api.go @@ -9,10 +9,20 @@ import ( "github.com/gofiber/fiber/v2" ) -func getBoost(c *fiber.Ctx) error { - id, _ := c.ParamsInt("id", 0) +func listBoost(c *fiber.Ctx) error { + attachmentId, _ := c.ParamsInt("attachmentId", 0) - if boost, err := services.GetBoostByID(uint(id)); err != nil { + if boost, err := services.ListBoostByAttachment(uint(attachmentId)); err != nil { + return fiber.NewError(fiber.StatusInternalServerError, err.Error()) + } else { + return c.JSON(boost) + } +} + +func getBoost(c *fiber.Ctx) error { + boostId, _ := c.ParamsInt("boostId", 0) + + if boost, err := services.GetBoostByID(uint(boostId)); err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } else { return c.JSON(boost) @@ -45,7 +55,7 @@ func createBoost(c *fiber.Ctx) error { func updateBoost(c *fiber.Ctx) error { user := c.Locals("nex_user").(*sec.UserInfo) - id, _ := c.ParamsInt("id", 0) + boostId, _ := c.ParamsInt("boostId", 0) var data struct { Status int `json:"status" validate:"required"` @@ -55,7 +65,7 @@ func updateBoost(c *fiber.Ctx) error { return err } - boost, err := services.GetBoostByID(uint(id)) + boost, err := services.GetBoostByID(uint(boostId)) if err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } else if boost.AccountID != user.ID { @@ -71,9 +81,9 @@ func updateBoost(c *fiber.Ctx) error { func deleteBoost(c *fiber.Ctx) error { user := c.Locals("nex_user").(*sec.UserInfo) - id, _ := c.ParamsInt("id", 0) + boostId, _ := c.ParamsInt("boostId", 0) - boost, err := services.GetBoostByID(uint(id)) + boost, err := services.GetBoostByID(uint(boostId)) if err != nil { return fiber.NewError(fiber.StatusInternalServerError, err.Error()) } else if boost.AccountID != user.ID { diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go index c65d70a..b732ac6 100644 --- a/pkg/internal/server/api/index.go +++ b/pkg/internal/server/api/index.go @@ -28,6 +28,8 @@ func MapAPIs(app *fiber.App, baseURL string) { attachments := api.Get("/attachments").Name("Attachments API") { + attachments.Get("/:attachmentId/boosts", listBoost) + attachments.Get("/", listAttachment) attachments.Get("/:id/meta", getAttachmentMeta) attachments.Get("/:id", openAttachment) diff --git a/pkg/internal/services/boost.go b/pkg/internal/services/boost.go index 1a4d9d2..fdef9fb 100644 --- a/pkg/internal/services/boost.go +++ b/pkg/internal/services/boost.go @@ -13,6 +13,14 @@ import ( "github.com/spf13/viper" ) +func ListBoostByAttachment(attachmentId uint) ([]models.AttachmentBoost, error) { + var boosts []models.AttachmentBoost + if err := database.C.Where("attachment_id = ?", attachmentId).Find(&boosts).Error; err != nil { + return boosts, err + } + return boosts, nil +} + func GetBoostByID(id uint) (models.AttachmentBoost, error) { var boost models.AttachmentBoost if err := database.C.