diff --git a/pkg/internal/server/api/boost_api.go b/pkg/internal/server/api/boost_api.go index b3247bc..6f13bc8 100644 --- a/pkg/internal/server/api/boost_api.go +++ b/pkg/internal/server/api/boost_api.go @@ -78,6 +78,24 @@ func createBoost(c *fiber.Ctx) error { } } +func activateBoost(c *fiber.Ctx) error { + user := c.Locals("nex_user").(*sec.UserInfo) + boostId, _ := c.ParamsInt("boostId", 0) + + boost, err := services.GetBoostByID(uint(boostId)) + if err != nil { + return fiber.NewError(fiber.StatusInternalServerError, err.Error()) + } else if boost.AccountID != user.ID { + return fiber.NewError(fiber.StatusNotFound, "record not created by you") + } + + if err := services.ActivateBoost(boost); err != nil { + return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } else { + return c.JSON(boost) + } +} + func updateBoost(c *fiber.Ctx) error { user := c.Locals("nex_user").(*sec.UserInfo) boostId, _ := c.ParamsInt("boostId", 0) diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go index 1df7d79..fa1f960 100644 --- a/pkg/internal/server/api/index.go +++ b/pkg/internal/server/api/index.go @@ -13,9 +13,10 @@ func MapAPIs(app *fiber.App, baseURL string) { boost := api.Group("/boosts").Name("Boosts API") { boost.Get("/", listBoostByUser) - boost.Get("/:id", getBoost) + boost.Get("/:boostId", getBoost) boost.Post("/", sec.ValidatorMiddleware, createBoost) - boost.Put("/:id", sec.ValidatorMiddleware, updateBoost) + boost.Post("/:boostId/activate", sec.ValidatorMiddleware, activateBoost) + boost.Put("/:boostId", sec.ValidatorMiddleware, updateBoost) } pools := api.Group("/pools").Name("Pools API") diff --git a/pkg/internal/services/boost.go b/pkg/internal/services/boost.go index cf04a37..9a80718 100644 --- a/pkg/internal/services/boost.go +++ b/pkg/internal/services/boost.go @@ -84,24 +84,25 @@ func CreateBoost(user *sec.UserInfo, source models.Attachment, destination int) return boost, nil } -func ActivateBoost(boost models.AttachmentBoost) { +func ActivateBoost(boost models.AttachmentBoost) error { log.Debug().Any("boost", boost).Msg("Activating boost...") dests := cast.ToSlice(viper.Get("destinations")) if boost.Destination >= len(dests) { log.Warn().Any("boost", boost).Msg("Unable to activate boost, invalid destination...") database.C.Model(&boost).Update("status", models.BoostStatusError) - return + return fmt.Errorf("invalid destination: %d", boost.Destination) } if err := ReUploadFile(boost.Attachment, boost.Destination); err != nil { log.Warn().Any("boost", boost).Err(err).Msg("Unable to activate boost...") database.C.Model(&boost).Update("status", models.BoostStatusError) - return + return err } log.Info().Any("boost", boost).Msg("Boost was activated successfully.") database.C.Model(&boost).Update("status", models.BoostStatusActive) + return nil } func UpdateBoostStatus(boost models.AttachmentBoost, status int) (models.AttachmentBoost, error) {