feat:add conds
This commit is contained in:
@@ -3,6 +3,7 @@ package admin
|
||||
import (
|
||||
"quyun/app/models"
|
||||
"quyun/app/requests"
|
||||
"quyun/database/conds"
|
||||
"quyun/database/fields"
|
||||
"quyun/database/schemas/public/model"
|
||||
|
||||
@@ -23,8 +24,12 @@ type posts struct{}
|
||||
// @Bind pagination query
|
||||
// @Bind query query
|
||||
func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery) (*requests.Pager, error) {
|
||||
cond := models.Posts.BuildConditionWithKey(query.Keyword)
|
||||
pager, err := models.Posts.List(ctx.Context(), pagination, cond)
|
||||
conds := []conds.Cond{
|
||||
// conds.Post_NotDeleted(),
|
||||
// conds.Post_Status(fields.PostStatusPublished),
|
||||
conds.Post_Like(query.Keyword),
|
||||
}
|
||||
pager, err := models.Posts.List(ctx.Context(), pagination, conds...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"quyun/app/models"
|
||||
"quyun/app/requests"
|
||||
"quyun/database/conds"
|
||||
"quyun/database/fields"
|
||||
"quyun/database/schemas/public/model"
|
||||
"quyun/providers/ali"
|
||||
@@ -35,14 +36,13 @@ type posts struct {
|
||||
// @Bind query query
|
||||
// @Bind user local
|
||||
func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery, user *model.Users) (*requests.Pager, error) {
|
||||
log.Infof("ok", pagination, query.Keyword, user.ID)
|
||||
conds := []conds.Cond{
|
||||
conds.Post_NotDeleted(),
|
||||
conds.Post_Status(fields.PostStatusPublished),
|
||||
conds.Post_Like(query.Keyword),
|
||||
}
|
||||
|
||||
cond := models.Posts.BuildConditionWithKey(query.Keyword)
|
||||
pager, err := models.Posts.List(ctx.Context(), pagination, cond, func(item model.Posts) model.Posts {
|
||||
item.Assets = fields.ToJson([]fields.MediaAsset{})
|
||||
item.Content = ""
|
||||
return item
|
||||
})
|
||||
pager, err := models.Posts.List(ctx.Context(), pagination, conds...)
|
||||
if err != nil {
|
||||
log.WithError(err).Errorf("post list err: %v", err)
|
||||
return nil, err
|
||||
@@ -106,7 +106,8 @@ type PostItem struct {
|
||||
// @Bind user local
|
||||
func (ctl *posts) Show(ctx fiber.Ctx, id int64, user *model.Users) (*PostItem, error) {
|
||||
log.Infof("Fetching post with ID: %d", id)
|
||||
post, err := models.Posts.GetByID(ctx.Context(), id)
|
||||
|
||||
post, err := models.Posts.GetByID(ctx.Context(), id, conds.Post_NotDeleted(), conds.Post_Status(fields.PostStatusPublished))
|
||||
if err != nil {
|
||||
log.WithError(err).Errorf("GetByID err: %v", err)
|
||||
return nil, err
|
||||
@@ -192,12 +193,12 @@ func (ctl *posts) Play(ctx fiber.Ctx, id int64, user *model.Users) (*PlayUrl, er
|
||||
func (ctl *posts) Mine(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery, user *model.Users) (*requests.Pager, error) {
|
||||
log.Infof("Fetching posts for user with pagination: %+v and keyword: %v", pagination, query.Keyword)
|
||||
|
||||
cond := models.Posts.BuildConditionWithKey(query.Keyword)
|
||||
pager, err := models.Users.PostList(ctx.Context(), user.ID, pagination, cond, func(item model.Posts) model.Posts {
|
||||
item.Assets = fields.ToJson([]fields.MediaAsset{})
|
||||
item.Content = ""
|
||||
return item
|
||||
})
|
||||
conds := []conds.Cond{
|
||||
conds.Post_NotDeleted(),
|
||||
conds.Post_Status(fields.PostStatusPublished),
|
||||
conds.Post_Like(query.Keyword),
|
||||
}
|
||||
pager, err := models.Users.PostList(ctx.Context(), user.ID, pagination, conds...)
|
||||
if err != nil {
|
||||
log.WithError(err).Errorf("post list err: %v", err)
|
||||
return nil, err
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"quyun/app/requests"
|
||||
"quyun/database/conds"
|
||||
"quyun/database/schemas/public/model"
|
||||
"quyun/database/schemas/public/table"
|
||||
|
||||
@@ -25,39 +26,15 @@ func (m *postsModel) Prepare() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// BuildConditionWithKey
|
||||
func (m *postsModel) BuildConditionWithKey(key *string) BoolExpression {
|
||||
tbl := table.Posts
|
||||
|
||||
cond := tbl.DeletedAt.IS_NULL()
|
||||
|
||||
if key == nil || *key == "" {
|
||||
return cond
|
||||
}
|
||||
|
||||
cond = cond.AND(
|
||||
tbl.Title.LIKE(String("%" + *key + "%")).
|
||||
OR(
|
||||
tbl.Content.LIKE(String("%" + *key + "%")),
|
||||
).
|
||||
OR(
|
||||
tbl.Description.LIKE(String("%" + *key + "%")),
|
||||
),
|
||||
)
|
||||
|
||||
return cond
|
||||
}
|
||||
|
||||
// GetByID
|
||||
func (m *postsModel) GetByID(ctx context.Context, id int64) (*model.Posts, error) {
|
||||
func (m *postsModel) GetByID(ctx context.Context, id int64, cond ...conds.Cond) (*model.Posts, error) {
|
||||
tbl := table.Posts
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(
|
||||
tbl.ID.EQ(Int64(id)).AND(
|
||||
tbl.DeletedAt.IS_NULL(),
|
||||
),
|
||||
)
|
||||
|
||||
var combinedCond BoolExpression = tbl.ID.EQ(Int64(id))
|
||||
for _, c := range cond {
|
||||
combinedCond = c(combinedCond)
|
||||
}
|
||||
stmt := tbl.SELECT(tbl.AllColumns).WHERE(combinedCond)
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
var post model.Posts
|
||||
@@ -121,13 +98,18 @@ func (m *postsModel) countByCondition(ctx context.Context, expr BoolExpression)
|
||||
return cnt.Cnt, nil
|
||||
}
|
||||
|
||||
func (m *postsModel) List(ctx context.Context, pagination *requests.Pagination, cond BoolExpression, callbacks ...func(model.Posts) model.Posts) (*requests.Pager, error) {
|
||||
func (m *postsModel) List(ctx context.Context, pagination *requests.Pagination, cond ...conds.Cond) (*requests.Pager, error) {
|
||||
pagination.Format()
|
||||
|
||||
combinedCond := table.Posts.DeletedAt.IS_NULL()
|
||||
for _, c := range cond {
|
||||
combinedCond = c(combinedCond)
|
||||
}
|
||||
|
||||
tbl := table.Posts
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(cond).
|
||||
WHERE(combinedCond).
|
||||
ORDER_BY(tbl.ID.DESC()).
|
||||
LIMIT(pagination.Limit).
|
||||
OFFSET(pagination.Offset)
|
||||
@@ -140,20 +122,12 @@ func (m *postsModel) List(ctx context.Context, pagination *requests.Pagination,
|
||||
return nil, err
|
||||
}
|
||||
|
||||
count, err := m.countByCondition(ctx, cond)
|
||||
count, err := m.countByCondition(ctx, combinedCond)
|
||||
if err != nil {
|
||||
m.log.Errorf("error getting post count: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(callbacks) > 0 {
|
||||
for _, f := range callbacks {
|
||||
posts = lo.Map(posts, func(item model.Posts, _ int) model.Posts {
|
||||
return f(item)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return &requests.Pager{
|
||||
Items: posts,
|
||||
Total: count,
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
"quyun/app/requests"
|
||||
"quyun/database/conds"
|
||||
"quyun/database/fields"
|
||||
"quyun/database/schemas/public/model"
|
||||
"quyun/database/schemas/public/table"
|
||||
@@ -194,14 +195,15 @@ func (m *usersModel) DeleteByID(ctx context.Context, id int64) error {
|
||||
}
|
||||
|
||||
// PostList returns a paginated list of posts for a user
|
||||
func (m *usersModel) PostList(ctx context.Context, userId int64, pagination *requests.Pagination, cond BoolExpression, callbacks ...func(model.Posts) model.Posts) (*requests.Pager, error) {
|
||||
func (m *usersModel) PostList(ctx context.Context, userId int64, pagination *requests.Pagination, conds ...conds.Cond) (*requests.Pager, error) {
|
||||
pagination.Format()
|
||||
|
||||
tblUserPosts := table.UserPosts
|
||||
|
||||
cond = cond.AND(
|
||||
tblUserPosts.UserID.EQ(Int64(userId)),
|
||||
)
|
||||
combineConds := tblUserPosts.UserID.EQ(Int64(userId))
|
||||
for _, c := range conds {
|
||||
combineConds = c(combineConds)
|
||||
}
|
||||
|
||||
tbl := table.Posts
|
||||
stmt := SELECT(tbl.AllColumns).
|
||||
@@ -211,7 +213,7 @@ func (m *usersModel) PostList(ctx context.Context, userId int64, pagination *req
|
||||
tblUserPosts.PostID.EQ(tbl.ID),
|
||||
),
|
||||
).
|
||||
WHERE(cond).
|
||||
WHERE(combineConds).
|
||||
ORDER_BY(tblUserPosts.ID.DESC()).
|
||||
LIMIT(pagination.Limit).
|
||||
OFFSET(pagination.Offset)
|
||||
@@ -244,14 +246,6 @@ func (m *usersModel) PostList(ctx context.Context, userId int64, pagination *req
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if len(callbacks) > 0 {
|
||||
for _, f := range callbacks {
|
||||
posts = lo.Map(posts, func(item model.Posts, _ int) model.Posts {
|
||||
return f(item)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return &requests.Pager{
|
||||
Items: posts,
|
||||
Total: cnt.Cnt,
|
||||
|
||||
Reference in New Issue
Block a user