remove: tenantslug

This commit is contained in:
Rogee
2025-01-15 20:31:21 +08:00
parent 13566cfa38
commit dbe1e19be2
24 changed files with 521 additions and 301 deletions

View File

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