64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
package medias
|
|
|
|
import (
|
|
"backend/common/consts"
|
|
"backend/common/errorx"
|
|
"backend/providers/jwt"
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// @provider
|
|
type Controller struct {
|
|
svc *Service
|
|
}
|
|
|
|
// List
|
|
func (c *Controller) List(ctx fiber.Ctx) error {
|
|
filter := ListFilter{}
|
|
if err := ctx.Bind().Body(&filter); err != nil {
|
|
log.WithError(err).Error("parse body failed")
|
|
return ctx.Status(fiber.StatusBadRequest).JSON(errorx.RequestParseError)
|
|
}
|
|
claim := ctx.Locals(consts.CtxKeyClaim).(*jwt.Claims)
|
|
|
|
items, err := c.svc.List(ctx.Context(), claim.TenantID, claim.UserID, &filter)
|
|
if err != nil {
|
|
return ctx.Status(fiber.StatusInternalServerError).JSON(errorx.InternalError)
|
|
}
|
|
|
|
return ctx.JSON(items)
|
|
}
|
|
|
|
// Show
|
|
func (c *Controller) Show(ctx fiber.Ctx) error {
|
|
hash := ctx.Params("hash")
|
|
claim := fiber.Locals[*jwt.Claims](ctx, consts.CtxKeyClaim)
|
|
log.Debug(claim)
|
|
|
|
model, err := c.svc.GetMediaByHash(ctx.Context(), claim.TenantID, hash)
|
|
if err != nil {
|
|
log.WithField("action", "medias.Show").WithError(err).Error("GetMediaByHash")
|
|
return ctx.Status(fiber.StatusInternalServerError).JSON(errorx.InternalError)
|
|
}
|
|
|
|
resource := c.svc.ModelToListItem(ctx.Context(), model)
|
|
resource.Bought, err = c.svc.HasUserBought(ctx.Context(), claim.TenantID, claim.UserID, model.ID)
|
|
if err != nil {
|
|
log.WithField("action", "medias.Show").WithError(err).Error("HasUserBought")
|
|
return ctx.Status(fiber.StatusInternalServerError).JSON(errorx.InternalError)
|
|
}
|
|
|
|
return ctx.JSON(resource)
|
|
}
|
|
|
|
// Audio
|
|
func (c *Controller) Video(ctx fiber.Ctx) error {
|
|
// mediaId := ToInt64(ctx.Params("media"))
|
|
// tenantId := ToInt64(ctx.Locals("tenantId"))
|
|
// userId := ToInt64(ctx.Locals("userId"))
|
|
|
|
return ctx.JSON(nil)
|
|
}
|