192 lines
4.6 KiB
Go
192 lines
4.6 KiB
Go
package posts
|
|
|
|
import (
|
|
"time"
|
|
|
|
"backend/app/errorx"
|
|
"backend/app/http/medias"
|
|
"backend/app/http/tenants"
|
|
"backend/app/http/users"
|
|
"backend/app/requests"
|
|
"backend/database/fields"
|
|
"backend/database/models/qvyun_v2/public/model"
|
|
"backend/providers/jwt"
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
"github.com/jinzhu/copier"
|
|
"github.com/samber/lo"
|
|
log "github.com/sirupsen/logrus"
|
|
"github.com/speps/go-hashids/v2"
|
|
)
|
|
|
|
// @provider
|
|
type Controller struct {
|
|
svc *Service
|
|
hashIds *hashids.HashID
|
|
userSvc *users.Service
|
|
tenantSvc *tenants.Service
|
|
mediaSvc *medias.Service
|
|
log *log.Entry `inject:"false"`
|
|
}
|
|
|
|
func (c *Controller) Prepare() error {
|
|
c.log = log.WithField("module", "posts.Controller")
|
|
return nil
|
|
}
|
|
|
|
// List show posts list
|
|
// @Router /api/v1/posts [get]
|
|
// @Bind tenantSlug cookie key(tenant)
|
|
// @Bind claim local
|
|
// @Bind pagination query
|
|
// @Bind filter query
|
|
func (c *Controller) List(ctx fiber.Ctx, tenantSlug string, claim *jwt.Claims, pagination *requests.Pagination, filter *UserPostFilter) (*requests.Pager, error) {
|
|
tenant, err := c.tenantSvc.GetTenantBySlug(ctx.Context(), tenantSlug)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pagination.Format()
|
|
pager := &requests.Pager{
|
|
Pagination: *pagination,
|
|
}
|
|
|
|
filter.TenantID = tenant.ID
|
|
filter.UserID = claim.UserID
|
|
orders, total, err := c.svc.GetPosts(ctx.Context(), pagination, filter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
pager.Total = total
|
|
|
|
pager.Items = lo.FilterMap(orders, func(item model.Posts, _ int) (UserPost, bool) {
|
|
var o UserPost
|
|
if err := copier.Copy(&o, item); err != nil {
|
|
return o, false
|
|
}
|
|
return o, true
|
|
})
|
|
|
|
return pager, nil
|
|
}
|
|
|
|
// ListBought show user bought posts list
|
|
// @Router /api/v1/bought-posts [get]
|
|
// @Bind tenantSlug cookie key(tenant)
|
|
// @Bind claim local
|
|
// @Bind pagination query
|
|
// @Bind filter query
|
|
func (c *Controller) ListBought(ctx fiber.Ctx, tenantSlug string, claim *jwt.Claims, pagination *requests.Pagination, filter *UserPostFilter) (*requests.Pager, error) {
|
|
tenant, err := c.tenantSvc.GetTenantBySlug(ctx.Context(), tenantSlug)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
pagination.Format()
|
|
pager := &requests.Pager{
|
|
Pagination: *pagination,
|
|
}
|
|
|
|
filter.TenantID = tenant.ID
|
|
filter.UserID = claim.UserID
|
|
orders, total, err := c.svc.GetBoughtPosts(ctx.Context(), pagination, filter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
pager.Total = total
|
|
|
|
pager.Items = lo.FilterMap(orders, func(item model.Posts, _ int) (UserPost, bool) {
|
|
var o UserPost
|
|
if err := copier.Copy(&o, item); err != nil {
|
|
return o, false
|
|
}
|
|
return o, true
|
|
})
|
|
|
|
return pager, nil
|
|
}
|
|
|
|
// Show show posts detail
|
|
// @Router /api/v1/show/:hash [get]
|
|
// @Bind claim local
|
|
// @Bind tenantSlug cookie key(tenant)
|
|
// @Bind hash path
|
|
func (c *Controller) Show(ctx fiber.Ctx, claim *jwt.Claims, tenantSlug, hash string) (*UserPost, error) {
|
|
userPost := &UserPost{}
|
|
|
|
tenant, err := c.tenantSvc.GetTenantBySlug(ctx.Context(), tenantSlug)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
postIds, err := c.hashIds.DecodeInt64WithError(hash)
|
|
if err != nil {
|
|
return nil, errorx.RecordNotExists
|
|
}
|
|
|
|
if tenant.ID != postIds[0] {
|
|
return nil, errorx.RecordNotExists
|
|
}
|
|
|
|
post, err := c.svc.GetPostByID(ctx.Context(), postIds[1])
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := copier.Copy(userPost, post); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return userPost, nil
|
|
}
|
|
|
|
// @Router /api/v1/posts [post]
|
|
// @Bind claim local
|
|
// @Bind tenantSlug cookie key(tenant)
|
|
// @Bind body body
|
|
func (ctl *Controller) Create(ctx fiber.Ctx, claim *jwt.Claims, tenantSlug string, body *PostBody) error {
|
|
user, err := ctl.userSvc.GetUserByID(ctx.Context(), claim.UserID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
tenant, err := ctl.tenantSvc.GetTenantBySlug(ctx.Context(), tenantSlug)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// check media assets exists
|
|
hashes := lo.Map(body.Assets.Data, func(item fields.MediaAsset, _ int) string { return item.Hash })
|
|
medias, err := ctl.mediaSvc.GetMediasByHash(ctx.Context(), tenant.ID, user.ID, hashes)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if len(medias) != len(lo.Uniq(hashes)) {
|
|
return errorx.BadRequest
|
|
}
|
|
|
|
post := &model.Posts{
|
|
CreatedAt: time.Now(),
|
|
UpdatedAt: time.Now(),
|
|
TenantID: tenant.ID,
|
|
UserID: user.ID,
|
|
Title: body.Title,
|
|
Description: body.Description,
|
|
Content: body.Content,
|
|
Stage: fields.PostStagePending,
|
|
Status: fields.PostStatusPending,
|
|
Price: body.Price,
|
|
Discount: body.Discount,
|
|
Assets: body.Assets,
|
|
Tags: body.Tags,
|
|
}
|
|
|
|
if err := ctl.svc.Create(ctx.Context(), tenant, user, post); err != nil {
|
|
return err
|
|
}
|
|
// TODO: trigger event && jobs
|
|
|
|
return nil
|
|
}
|