diff --git a/pkg/internal/server/api/boost_api.go b/pkg/internal/server/api/boost_api.go index 6472b81..b3247bc 100644 --- a/pkg/internal/server/api/boost_api.go +++ b/pkg/internal/server/api/boost_api.go @@ -9,7 +9,32 @@ import ( "github.com/gofiber/fiber/v2" ) -func listBoost(c *fiber.Ctx) error { +func listBoostByUser(c *fiber.Ctx) error { + user := c.Locals("nex_user").(*sec.UserInfo) + take := c.QueryInt("take", 0) + offset := c.QueryInt("offset", 0) + + if take > 100 { + take = 100 + } + + count, err := services.CountBoostByUser(user.ID) + if err != nil { + return fiber.NewError(fiber.StatusInternalServerError, err.Error()) + } + + boosts, err := services.ListBoostByUser(user.ID, take, offset) + if err != nil { + return fiber.NewError(fiber.StatusInternalServerError, err.Error()) + } + + return c.JSON(fiber.Map{ + "count": count, + "data": boosts, + }) +} + +func listBoostByAttachment(c *fiber.Ctx) error { attachmentId, _ := c.ParamsInt("attachmentId", 0) if boost, err := services.ListBoostByAttachment(uint(attachmentId)); err != nil { diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go index b732ac6..afbd8be 100644 --- a/pkg/internal/server/api/index.go +++ b/pkg/internal/server/api/index.go @@ -12,6 +12,7 @@ func MapAPIs(app *fiber.App, baseURL string) { { boost := api.Group("/boosts").Name("Boosts API") { + boost.Get("/", listBoostByUser) boost.Get("/:id", getBoost) boost.Post("/", sec.ValidatorMiddleware, createBoost) boost.Put("/:id", sec.ValidatorMiddleware, updateBoost) @@ -28,7 +29,7 @@ func MapAPIs(app *fiber.App, baseURL string) { attachments := api.Get("/attachments").Name("Attachments API") { - attachments.Get("/:attachmentId/boosts", listBoost) + attachments.Get("/:attachmentId/boosts", listBoostByAttachment) attachments.Get("/", listAttachment) attachments.Get("/:id/meta", getAttachmentMeta) diff --git a/pkg/internal/services/boost.go b/pkg/internal/services/boost.go index fdef9fb..8e586cc 100644 --- a/pkg/internal/services/boost.go +++ b/pkg/internal/services/boost.go @@ -13,6 +13,28 @@ import ( "github.com/spf13/viper" ) +func CountBoostByUser(userId uint) (int64, error) { + var count int64 + if err := database.C. + Model(&models.AttachmentBoost{}). + Where("account_id = ?", userId). + Count(&count).Error; err != nil { + return count, err + } + return count, nil +} + +func ListBoostByUser(userId uint, take, offset int) ([]models.AttachmentBoost, error) { + var boosts []models.AttachmentBoost + if err := database.C. + Where("account_id = ?", userId). + Limit(take).Offset(offset). + Find(&boosts).Error; err != nil { + return boosts, err + } + return boosts, nil +} + func ListBoostByAttachment(attachmentId uint) ([]models.AttachmentBoost, error) { var boosts []models.AttachmentBoost if err := database.C.Where("attachment_id = ?", attachmentId).Find(&boosts).Error; err != nil {