List boosts by attachment

This commit is contained in:
LittleSheep 2024-12-28 22:08:45 +08:00
parent b73f5e1912
commit 1a5787d3c2
3 changed files with 27 additions and 7 deletions

View File

@ -9,10 +9,20 @@ import (
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
) )
func getBoost(c *fiber.Ctx) error { func listBoost(c *fiber.Ctx) error {
id, _ := c.ParamsInt("id", 0) 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()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else { } else {
return c.JSON(boost) return c.JSON(boost)
@ -45,7 +55,7 @@ func createBoost(c *fiber.Ctx) error {
func updateBoost(c *fiber.Ctx) error { func updateBoost(c *fiber.Ctx) error {
user := c.Locals("nex_user").(*sec.UserInfo) user := c.Locals("nex_user").(*sec.UserInfo)
id, _ := c.ParamsInt("id", 0) boostId, _ := c.ParamsInt("boostId", 0)
var data struct { var data struct {
Status int `json:"status" validate:"required"` Status int `json:"status" validate:"required"`
@ -55,7 +65,7 @@ func updateBoost(c *fiber.Ctx) error {
return err return err
} }
boost, err := services.GetBoostByID(uint(id)) boost, err := services.GetBoostByID(uint(boostId))
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else if boost.AccountID != user.ID { } else if boost.AccountID != user.ID {
@ -71,9 +81,9 @@ func updateBoost(c *fiber.Ctx) error {
func deleteBoost(c *fiber.Ctx) error { func deleteBoost(c *fiber.Ctx) error {
user := c.Locals("nex_user").(*sec.UserInfo) 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 { if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error()) return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else if boost.AccountID != user.ID { } else if boost.AccountID != user.ID {

View File

@ -28,6 +28,8 @@ func MapAPIs(app *fiber.App, baseURL string) {
attachments := api.Get("/attachments").Name("Attachments API") attachments := api.Get("/attachments").Name("Attachments API")
{ {
attachments.Get("/:attachmentId/boosts", listBoost)
attachments.Get("/", listAttachment) attachments.Get("/", listAttachment)
attachments.Get("/:id/meta", getAttachmentMeta) attachments.Get("/:id/meta", getAttachmentMeta)
attachments.Get("/:id", openAttachment) attachments.Get("/:id", openAttachment)

View File

@ -13,6 +13,14 @@ import (
"github.com/spf13/viper" "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) { func GetBoostByID(id uint) (models.AttachmentBoost, error) {
var boost models.AttachmentBoost var boost models.AttachmentBoost
if err := database.C. if err := database.C.