123 lines
2.9 KiB
Go
123 lines
2.9 KiB
Go
package posts
|
|
|
|
import (
|
|
"backend/app/http/tenants"
|
|
"backend/app/requests"
|
|
"backend/database/models/qvyun_v2/public/model"
|
|
"backend/providers/jwt"
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
"github.com/jinzhu/copier"
|
|
"github.com/samber/lo"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// @provider
|
|
type Controller struct {
|
|
tenantSvc *tenants.Service
|
|
svc *Service
|
|
log *log.Entry `inject:"false"`
|
|
}
|
|
|
|
func (c *Controller) Prepare() error {
|
|
c.log = log.WithField("module", "posts.Controller")
|
|
return nil
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
pagination.Format()
|
|
pager := &requests.Pager{
|
|
Pagination: *pagination,
|
|
}
|
|
|
|
filter.TenantID = tenant.ID
|
|
filter.UserID = claim.UserID
|
|
orders, total, err := c.svc.GetPosts(ctx.Context(), pagination, filter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
pager.Total = total
|
|
|
|
pager.Items = lo.FilterMap(orders, func(item model.Posts, _ int) (UserPost, bool) {
|
|
var o UserPost
|
|
if err := copier.Copy(&o, item); err != nil {
|
|
return o, false
|
|
}
|
|
return o, true
|
|
})
|
|
|
|
return pager, nil
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
pagination.Format()
|
|
pager := &requests.Pager{
|
|
Pagination: *pagination,
|
|
}
|
|
|
|
filter.TenantID = tenant.ID
|
|
filter.UserID = claim.UserID
|
|
orders, total, err := c.svc.GetBoughtPosts(ctx.Context(), pagination, filter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
pager.Total = total
|
|
|
|
pager.Items = lo.FilterMap(orders, func(item model.Posts, _ int) (UserPost, bool) {
|
|
var o UserPost
|
|
if err := copier.Copy(&o, item); err != nil {
|
|
return o, false
|
|
}
|
|
return o, true
|
|
})
|
|
|
|
return pager, nil
|
|
}
|
|
|
|
// 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) {
|
|
userPost := &UserPost{}
|
|
|
|
tenant, err := c.tenantSvc.GetTenantBySlug(ctx.Context(), tenantSlug)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
post, err := c.svc.GetPostByHash(ctx.Context(), tenant.ID, hash)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := copier.Copy(userPost, post); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return userPost, nil
|
|
}
|