feat: add posts
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"time"
|
||||
|
||||
"backend/app/errorx"
|
||||
"backend/app/http/medias"
|
||||
"backend/app/http/tenants"
|
||||
"backend/app/http/users"
|
||||
"backend/app/requests"
|
||||
@@ -24,6 +25,7 @@ type Controller struct {
|
||||
hashIds *hashids.HashID
|
||||
userSvc *users.Service
|
||||
tenantSvc *tenants.Service
|
||||
mediaSvc *medias.Service
|
||||
log *log.Entry `inject:"false"`
|
||||
}
|
||||
|
||||
@@ -153,24 +155,37 @@ func (ctl *Controller) Create(ctx fiber.Ctx, claim *jwt.Claims, tenantSlug strin
|
||||
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,
|
||||
PosterAssetID: 0,
|
||||
Stage: fields.PostStagePending,
|
||||
Status: fields.PostStatusPending,
|
||||
Price: body.Price,
|
||||
Discount: body.Discount,
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user