feat: update order s
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/samber/lo"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -100,20 +101,56 @@ func (m *ordersModel) List(ctx context.Context, pagination *requests.Pagination,
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
var orders []model.Orders = make([]model.Orders, 0)
|
||||
err := stmt.QueryContext(ctx, db, &orders)
|
||||
if err != nil {
|
||||
if err := stmt.QueryContext(ctx, db, &orders); err != nil {
|
||||
m.log.Errorf("error querying orders: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
postsMap, err := Posts.GetPostsMapByIDs(ctx, lo.Map(orders, func(order model.Orders, _ int) int64 {
|
||||
return order.PostID
|
||||
}))
|
||||
if err != nil {
|
||||
m.log.Errorf("error getting posts map: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
userMap, err := Users.GetUsersMapByIDs(ctx, lo.Map(orders, func(order model.Orders, _ int) int64 {
|
||||
return order.UserID
|
||||
}))
|
||||
if err != nil {
|
||||
m.log.Errorf("error getting users map: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
count, err := m.countByCondition(ctx, cond)
|
||||
if err != nil {
|
||||
m.log.Errorf("error getting order count: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
type orderItem struct {
|
||||
model.Orders
|
||||
|
||||
PostTitle string `json:"post_title"`
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
return &requests.Pager{
|
||||
Items: orders,
|
||||
Items: lo.Map(orders, func(order model.Orders, _ int) *orderItem {
|
||||
item := &orderItem{
|
||||
Orders: order,
|
||||
}
|
||||
|
||||
if post, ok := postsMap[order.PostID]; ok {
|
||||
item.PostTitle = post.Title
|
||||
}
|
||||
|
||||
if user, ok := userMap[order.UserID]; ok {
|
||||
item.Username = user.Username
|
||||
}
|
||||
|
||||
return item
|
||||
}),
|
||||
Total: count,
|
||||
Pagination: *pagination,
|
||||
}, nil
|
||||
|
||||
Reference in New Issue
Block a user