fix: issues

This commit is contained in:
Rogee
2025-01-14 19:48:23 +08:00
parent b429f30916
commit 48a0a6c195
14 changed files with 1037 additions and 195 deletions

View File

@@ -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
}