remove: tenantslug
This commit is contained in:
@@ -4,25 +4,29 @@ import (
|
||||
"time"
|
||||
|
||||
"backend/app/errorx"
|
||||
"backend/app/events/publishers"
|
||||
"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/event"
|
||||
"backend/providers/hashids"
|
||||
"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 {
|
||||
event *event.PubSub
|
||||
|
||||
svc *Service
|
||||
hashIds *hashids.HashID
|
||||
hashId *hashids.Hasher
|
||||
userSvc *users.Service
|
||||
tenantSvc *tenants.Service
|
||||
mediaSvc *medias.Service
|
||||
@@ -36,22 +40,16 @@ func (c *Controller) Prepare() error {
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
func (c *Controller) List(ctx fiber.Ctx, claim *jwt.Claims, pagination *requests.Pagination, filter *UserPostFilter) (*requests.Pager, error) {
|
||||
pagination.Format()
|
||||
pager := &requests.Pager{
|
||||
Pagination: *pagination,
|
||||
}
|
||||
|
||||
filter.TenantID = tenant.ID
|
||||
filter.TenantID = *claim.TenantID
|
||||
filter.UserID = claim.UserID
|
||||
orders, total, err := c.svc.GetPosts(ctx.Context(), pagination, filter)
|
||||
if err != nil {
|
||||
@@ -72,22 +70,16 @@ func (c *Controller) List(ctx fiber.Ctx, tenantSlug string, claim *jwt.Claims, p
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
func (c *Controller) ListBought(ctx fiber.Ctx, claim *jwt.Claims, pagination *requests.Pagination, filter *UserPostFilter) (*requests.Pager, error) {
|
||||
pagination.Format()
|
||||
pager := &requests.Pager{
|
||||
Pagination: *pagination,
|
||||
}
|
||||
|
||||
filter.TenantID = tenant.ID
|
||||
filter.TenantID = *claim.TenantID
|
||||
filter.UserID = claim.UserID
|
||||
orders, total, err := c.svc.GetBoughtPosts(ctx.Context(), pagination, filter)
|
||||
if err != nil {
|
||||
@@ -109,26 +101,16 @@ func (c *Controller) ListBought(ctx fiber.Ctx, tenantSlug string, claim *jwt.Cla
|
||||
// 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) {
|
||||
func (c *Controller) Show(ctx fiber.Ctx, claim *jwt.Claims, 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)
|
||||
postId, err := c.hashId.DecodeOnlyInt64(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])
|
||||
post, err := c.svc.GetPostByID(ctx.Context(), postId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -186,7 +168,84 @@ func (ctl *Controller) Create(ctx fiber.Ctx, claim *jwt.Claims, tenantSlug strin
|
||||
if err := ctl.svc.Create(ctx.Context(), tenant, user, post); err != nil {
|
||||
return err
|
||||
}
|
||||
// TODO: trigger event && jobs
|
||||
|
||||
_ = ctl.event.Publish(&publishers.PostCreated{ID: post.ID})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Delete
|
||||
// @Router /api/v1/posts/:hash [delete]
|
||||
// @Bind claim local
|
||||
// @Bind tenantSlug cookie key(tenant)
|
||||
// @Bind hash path
|
||||
func (c *Controller) Delete(ctx fiber.Ctx, claim *jwt.Claims, tenantSlug, hash string) error {
|
||||
tenant, err := c.tenantSvc.GetTenantBySlug(ctx.Context(), tenantSlug)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
postId, err := c.hashId.DecodeOnlyInt64(hash)
|
||||
if err != nil {
|
||||
return errorx.RecordNotExists
|
||||
}
|
||||
|
||||
if err := c.svc.Delete(ctx.Context(), tenant.ID, *&claim.UserID, postId); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// trigger event
|
||||
_ = c.event.Publish(&publishers.PostDeleted{ID: postId})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Update
|
||||
// @Router /api/v1/posts/:hash [put]
|
||||
// @Bind claim local
|
||||
// @Bind tenantSlug cookie key(tenant)
|
||||
// @Bind hash path
|
||||
// @Bind body body
|
||||
func (ctl *Controller) Update(ctx fiber.Ctx, claim *jwt.Claims, hash string, body *PostBody) error {
|
||||
postId, err := ctl.hashId.DecodeOnlyInt64(hash)
|
||||
if err != nil {
|
||||
return errorx.RecordNotExists
|
||||
}
|
||||
|
||||
post, err := ctl.svc.GetPostByID(ctx.Context(), postId)
|
||||
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(), *claim.TenantID, post.UserID, hashes)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(medias) != len(lo.Uniq(hashes)) {
|
||||
return errorx.BadRequest
|
||||
}
|
||||
|
||||
_ = ctl.event.Publish(&publishers.PostUpdatedEvent{ID: postId})
|
||||
|
||||
m := &model.Posts{
|
||||
UpdatedAt: time.Now(),
|
||||
Title: body.Title,
|
||||
Description: body.Description,
|
||||
Content: body.Content,
|
||||
Price: body.Price,
|
||||
Discount: body.Discount,
|
||||
Assets: body.Assets,
|
||||
Tags: body.Tags,
|
||||
|
||||
Stage: fields.PostStagePending,
|
||||
Status: fields.PostStatusPending,
|
||||
}
|
||||
|
||||
if err := ctl.svc.Update(ctx.Context(), post.TenantID, post.UserID, post.ID, m); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -3,25 +3,31 @@ package posts
|
||||
import (
|
||||
"database/sql"
|
||||
|
||||
"backend/app/http/medias"
|
||||
"backend/app/http/tenants"
|
||||
"backend/app/http/users"
|
||||
"backend/providers/event"
|
||||
"backend/providers/hashids"
|
||||
|
||||
"git.ipao.vip/rogeecn/atom"
|
||||
"git.ipao.vip/rogeecn/atom/container"
|
||||
"git.ipao.vip/rogeecn/atom/contracts"
|
||||
"git.ipao.vip/rogeecn/atom/utils/opt"
|
||||
"github.com/speps/go-hashids/v2"
|
||||
)
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
if err := container.Container.Provide(func(
|
||||
hashIds *hashids.HashID,
|
||||
event *event.PubSub,
|
||||
hashId *hashids.Hasher,
|
||||
mediaSvc *medias.Service,
|
||||
svc *Service,
|
||||
tenantSvc *tenants.Service,
|
||||
userSvc *users.Service,
|
||||
) (*Controller, error) {
|
||||
obj := &Controller{
|
||||
hashIds: hashIds,
|
||||
event: event,
|
||||
hashId: hashId,
|
||||
mediaSvc: mediaSvc,
|
||||
svc: svc,
|
||||
tenantSvc: tenantSvc,
|
||||
userSvc: userSvc,
|
||||
@@ -50,7 +56,7 @@ func Provide(opts ...opt.Option) error {
|
||||
}
|
||||
if err := container.Container.Provide(func(
|
||||
db *sql.DB,
|
||||
hashIds *hashids.HashID,
|
||||
hashIds *hashids.Hasher,
|
||||
) (*Service, error) {
|
||||
obj := &Service{
|
||||
db: db,
|
||||
|
||||
@@ -30,26 +30,23 @@ func (r *Routes) Name() string {
|
||||
|
||||
func (r *Routes) Register(router fiber.Router) {
|
||||
// 注册路由组: Controller
|
||||
router.Get("/api/v1/posts", DataFunc4(
|
||||
router.Get("/api/v1/posts", DataFunc3(
|
||||
r.controller.List,
|
||||
CookieParam("tenant"),
|
||||
Local[*jwt.Claims]("claim"),
|
||||
Query[requests.Pagination]("pagination"),
|
||||
Query[UserPostFilter]("filter"),
|
||||
))
|
||||
|
||||
router.Get("/api/v1/bought-posts", DataFunc4(
|
||||
router.Get("/api/v1/bought-posts", DataFunc3(
|
||||
r.controller.ListBought,
|
||||
CookieParam("tenant"),
|
||||
Local[*jwt.Claims]("claim"),
|
||||
Query[requests.Pagination]("pagination"),
|
||||
Query[UserPostFilter]("filter"),
|
||||
))
|
||||
|
||||
router.Get("/api/v1/show/:hash", DataFunc3(
|
||||
router.Get("/api/v1/show/:hash", DataFunc2(
|
||||
r.controller.Show,
|
||||
Local[*jwt.Claims]("claim"),
|
||||
CookieParam("tenant"),
|
||||
PathParam[string]("hash"),
|
||||
))
|
||||
|
||||
@@ -60,4 +57,18 @@ func (r *Routes) Register(router fiber.Router) {
|
||||
Body[PostBody]("body"),
|
||||
))
|
||||
|
||||
router.Delete("/api/v1/posts/:hash", Func3(
|
||||
r.controller.Delete,
|
||||
Local[*jwt.Claims]("claim"),
|
||||
CookieParam("tenant"),
|
||||
PathParam[string]("hash"),
|
||||
))
|
||||
|
||||
router.Put("/api/v1/posts/:hash", Func3(
|
||||
r.controller.Update,
|
||||
Local[*jwt.Claims]("claim"),
|
||||
PathParam[string]("hash"),
|
||||
Body[PostBody]("body"),
|
||||
))
|
||||
|
||||
}
|
||||
|
||||
@@ -3,17 +3,18 @@ package posts
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"backend/app/requests"
|
||||
"backend/database"
|
||||
"backend/database/models/qvyun_v2/public/model"
|
||||
"backend/database/models/qvyun_v2/public/table"
|
||||
"backend/providers/hashids"
|
||||
"backend/providers/otel"
|
||||
|
||||
. "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"
|
||||
)
|
||||
@@ -21,7 +22,7 @@ import (
|
||||
// @provider:except
|
||||
type Service struct {
|
||||
db *sql.DB
|
||||
hashIds *hashids.HashID
|
||||
hashIds *hashids.Hasher
|
||||
log *log.Entry `inject:"false"`
|
||||
}
|
||||
|
||||
@@ -114,7 +115,7 @@ func (svc *Service) GetPosts(ctx context.Context, pagination *requests.Paginatio
|
||||
)
|
||||
tbl := table.Posts
|
||||
|
||||
cond := Bool(true)
|
||||
cond := tbl.DeletedAt.IS_NULL()
|
||||
if filter.ID != nil {
|
||||
cond = cond.AND(tbl.ID.EQ(Int64(*filter.ID)))
|
||||
}
|
||||
@@ -165,6 +166,24 @@ func (svc *Service) GetPosts(ctx context.Context, pagination *requests.Paginatio
|
||||
return posts, count.Cnt, nil
|
||||
}
|
||||
|
||||
// ForceGetPostByID
|
||||
func (svc *Service) ForceGetPostByID(ctx context.Context, id int64) (*model.Posts, error) {
|
||||
_, span := otel.Start(ctx, "users.service.ForceGetPostByID")
|
||||
defer span.End()
|
||||
span.SetAttributes(attribute.Int64("post.id", id))
|
||||
tbl := table.Posts
|
||||
|
||||
stmt := tbl.SELECT(tbl.AllColumns).WHERE(tbl.ID.EQ(Int64(id)))
|
||||
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
|
||||
|
||||
var post model.Posts
|
||||
if err := stmt.QueryContext(ctx, svc.db, &post); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &post, nil
|
||||
}
|
||||
|
||||
// GetPostByID
|
||||
func (svc *Service) GetPostByID(ctx context.Context, id int64) (*model.Posts, error) {
|
||||
_, span := otel.Start(ctx, "users.service.GetPostByID")
|
||||
@@ -172,7 +191,7 @@ func (svc *Service) GetPostByID(ctx context.Context, id int64) (*model.Posts, er
|
||||
span.SetAttributes(attribute.Int64("post.id", id))
|
||||
tbl := table.Posts
|
||||
|
||||
stmt := tbl.SELECT(tbl.AllColumns).WHERE(tbl.ID.EQ(Int64(id)))
|
||||
stmt := tbl.SELECT(tbl.AllColumns).WHERE(tbl.ID.EQ(Int64(id)).AND(tbl.DeletedAt.IS_NULL()))
|
||||
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
|
||||
|
||||
var post model.Posts
|
||||
@@ -240,14 +259,76 @@ func (svc *Service) GetPostByHash(ctx context.Context, tenantID int64, hash stri
|
||||
attribute.String("hash", hash),
|
||||
)
|
||||
|
||||
postIDs, err := svc.hashIds.DecodeInt64WithError(hash)
|
||||
postId, err := svc.hashIds.DecodeOnlyInt64(hash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if tenantID != postIDs[0] {
|
||||
return nil, nil
|
||||
return svc.GetPostByID(ctx, postId)
|
||||
}
|
||||
|
||||
// Delete
|
||||
func (svc *Service) Delete(ctx context.Context, tenantID, userID, postID int64) error {
|
||||
_, span := otel.Start(ctx, "users.service.Delete")
|
||||
defer span.End()
|
||||
span.SetAttributes(
|
||||
attribute.Int64("tenant.id", tenantID),
|
||||
attribute.Int64("user.id", userID),
|
||||
attribute.Int64("post.id", postID),
|
||||
)
|
||||
tbl := table.Posts
|
||||
|
||||
stmt := tbl.
|
||||
UPDATE().
|
||||
SET(
|
||||
tbl.DeletedAt.SET(TimestampT(time.Now())),
|
||||
).
|
||||
WHERE(
|
||||
tbl.ID.EQ(Int64(postID)).AND(
|
||||
tbl.TenantID.EQ(Int64(tenantID)).AND(
|
||||
tbl.UserID.EQ(Int64(userID)),
|
||||
),
|
||||
),
|
||||
)
|
||||
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
|
||||
|
||||
if _, err := stmt.ExecContext(ctx, svc.db); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return svc.GetPostByID(ctx, postIDs[1])
|
||||
return nil
|
||||
}
|
||||
|
||||
// Update
|
||||
func (svc *Service) Update(ctx context.Context, tenantID, userID, postID int64, post *model.Posts) error {
|
||||
_, span := otel.Start(ctx, "users.service.Update")
|
||||
defer span.End()
|
||||
span.SetAttributes(
|
||||
attribute.Int64("tenant.id", tenantID),
|
||||
attribute.Int64("user.id", userID),
|
||||
attribute.Int64("post.id", postID),
|
||||
)
|
||||
tbl := table.Posts
|
||||
|
||||
stmt := tbl.
|
||||
UPDATE(
|
||||
tbl.MutableColumns.Except(
|
||||
tbl.TenantID, tbl.UserID, tbl.CreatedAt, tbl.DeletedAt,
|
||||
),
|
||||
).
|
||||
MODEL(post).
|
||||
WHERE(
|
||||
tbl.ID.EQ(Int64(postID)).AND(
|
||||
tbl.TenantID.EQ(Int64(tenantID)).AND(
|
||||
tbl.UserID.EQ(Int64(userID)),
|
||||
),
|
||||
),
|
||||
)
|
||||
span.SetAttributes(semconv.DBStatementKey.String(stmt.DebugSql()))
|
||||
|
||||
if _, err := stmt.ExecContext(ctx, svc.db); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user