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
|
||||
}
|
||||
|
||||
@@ -30,3 +30,11 @@ type UserPostFilter struct {
|
||||
CreatedAt *time.Time `query:"created_at"`
|
||||
Keyword *string `json:"title"`
|
||||
}
|
||||
|
||||
type PostBody struct {
|
||||
Title string
|
||||
Description string
|
||||
Content string
|
||||
Price int64
|
||||
Discount int16
|
||||
}
|
||||
|
||||
@@ -13,14 +13,16 @@ import (
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/samber/lo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/speps/go-hashids/v2"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
|
||||
)
|
||||
|
||||
// @provider:except
|
||||
type Service struct {
|
||||
db *sql.DB
|
||||
log *log.Entry `inject:"false"`
|
||||
db *sql.DB
|
||||
hashIds *hashids.HashID
|
||||
log *log.Entry `inject:"false"`
|
||||
}
|
||||
|
||||
func (svc *Service) Prepare() error {
|
||||
@@ -163,22 +165,14 @@ func (svc *Service) GetPosts(ctx context.Context, pagination *requests.Paginatio
|
||||
return posts, count.Cnt, nil
|
||||
}
|
||||
|
||||
// GetPostByHash
|
||||
func (svc *Service) GetPostByHash(ctx context.Context, tenantID int64, hash string) (*model.Posts, error) {
|
||||
_, span := otel.Start(ctx, "users.service.GetPostByHash")
|
||||
// GetPostByID
|
||||
func (svc *Service) GetPostByID(ctx context.Context, id int64) (*model.Posts, error) {
|
||||
_, span := otel.Start(ctx, "users.service.GetPostByID")
|
||||
defer span.End()
|
||||
span.SetAttributes(
|
||||
attribute.String("hash", hash),
|
||||
)
|
||||
span.SetAttributes(attribute.Int64("post.id", id))
|
||||
tbl := table.Posts
|
||||
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(
|
||||
tbl.Hash.EQ(String(hash)).AND(
|
||||
tbl.TenantID.EQ(Int64(tenantID)),
|
||||
),
|
||||
)
|
||||
stmt := tbl.SELECT(tbl.AllColumns).WHERE(tbl.ID.EQ(Int64(id)))
|
||||
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
|
||||
|
||||
var post model.Posts
|
||||
@@ -220,3 +214,40 @@ func (svc *Service) GetUserBoughtIDs(ctx context.Context, tenantID, userID int64
|
||||
return item.PostID
|
||||
}), nil
|
||||
}
|
||||
|
||||
// Create
|
||||
func (svc *Service) Create(ctx context.Context, tenant *model.Tenants, user *model.Users, post *model.Posts) error {
|
||||
_, span := otel.Start(ctx, "users.service.Create")
|
||||
defer span.End()
|
||||
|
||||
tbl := table.Posts
|
||||
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(post)
|
||||
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
|
||||
|
||||
if _, err := stmt.ExecContext(ctx, svc.db); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetPostByHash
|
||||
func (svc *Service) GetPostByHash(ctx context.Context, tenantID int64, hash string) (*model.Posts, error) {
|
||||
_, span := otel.Start(ctx, "users.service.GetPostByHash")
|
||||
defer span.End()
|
||||
span.SetAttributes(
|
||||
attribute.Int64("tenant.id", tenantID),
|
||||
attribute.String("hash", hash),
|
||||
)
|
||||
|
||||
postIDs, err := svc.hashIds.DecodeInt64WithError(hash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if tenantID != postIDs[0] {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return svc.GetPostByID(ctx, postIDs[1])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user