feat: update user detail
This commit is contained in:
@@ -286,3 +286,60 @@ func (m *postsModel) BoughtStatistics(ctx context.Context, postIds []int64) (map
|
||||
|
||||
return resultMap, nil
|
||||
}
|
||||
|
||||
// Bought
|
||||
func (m *postsModel) Bought(ctx context.Context, userId int64, pagination *requests.Pagination) (*requests.Pager, error) {
|
||||
pagination.Format()
|
||||
|
||||
// select up.price,up.created_at,p.* from user_posts up left join posts p on up.post_id = p.id where up.user_id =1
|
||||
tbl := table.UserPosts
|
||||
stmt := tbl.
|
||||
SELECT(
|
||||
tbl.Price.AS("price"),
|
||||
tbl.CreatedAt.AS("bought_at"),
|
||||
table.Posts.Title.AS("title"),
|
||||
).
|
||||
FROM(
|
||||
tbl.INNER_JOIN(table.Posts, table.Posts.ID.EQ(tbl.PostID)),
|
||||
).
|
||||
WHERE(
|
||||
tbl.UserID.EQ(Int64(1)),
|
||||
).
|
||||
ORDER_BY(tbl.ID.DESC()).
|
||||
LIMIT(pagination.Limit).
|
||||
OFFSET(pagination.Offset)
|
||||
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
var items []struct {
|
||||
Title string `json:"title"`
|
||||
Price int64 `json:"price"`
|
||||
BoughtAt time.Time `json:"bought_at"`
|
||||
}
|
||||
|
||||
if err := stmt.QueryContext(ctx, db, &items); err != nil {
|
||||
m.log.Errorf("error getting bought posts: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// convert to model.Posts
|
||||
var cnt struct {
|
||||
Cnt int64
|
||||
}
|
||||
stmtCnt := tbl.
|
||||
SELECT(COUNT(tbl.ID).AS("cnt")).
|
||||
WHERE(
|
||||
tbl.UserID.EQ(Int64(userId)),
|
||||
)
|
||||
|
||||
if err := stmtCnt.QueryContext(ctx, db, &cnt); err != nil {
|
||||
m.log.Errorf("error getting bought posts count: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &requests.Pager{
|
||||
Items: items,
|
||||
Total: cnt.Cnt,
|
||||
Pagination: *pagination,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user