54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package posts
|
|
|
|
import (
|
|
"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 {
|
|
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 claim local
|
|
// @Bind pagination query
|
|
// @Bind filter query
|
|
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 = *claim.TenantID
|
|
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
|
|
}
|