fix: issues
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
package posts
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"backend/app/errorx"
|
||||
"backend/app/http/tenants"
|
||||
"backend/app/http/users"
|
||||
"backend/app/requests"
|
||||
"backend/database/fields"
|
||||
"backend/database/models/qvyun_v2/public/model"
|
||||
"backend/providers/jwt"
|
||||
|
||||
@@ -10,12 +15,15 @@ import (
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/samber/lo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/speps/go-hashids/v2"
|
||||
)
|
||||
|
||||
// @provider
|
||||
type Controller struct {
|
||||
tenantSvc *tenants.Service
|
||||
svc *Service
|
||||
hashIds *hashids.HashID
|
||||
userSvc *users.Service
|
||||
tenantSvc *tenants.Service
|
||||
log *log.Entry `inject:"false"`
|
||||
}
|
||||
|
||||
@@ -109,7 +117,16 @@ func (c *Controller) Show(ctx fiber.Ctx, claim *jwt.Claims, tenantSlug, hash str
|
||||
return nil, err
|
||||
}
|
||||
|
||||
post, err := c.svc.GetPostByHash(ctx.Context(), tenant.ID, hash)
|
||||
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
|
||||
}
|
||||
@@ -120,3 +137,40 @@ func (c *Controller) Show(ctx fiber.Ctx, claim *jwt.Claims, tenantSlug, hash str
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
if err := ctl.svc.Create(ctx.Context(), tenant, user, post); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user