feat:add conds
This commit is contained in:
@@ -3,6 +3,7 @@ package admin
|
|||||||
import (
|
import (
|
||||||
"quyun/app/models"
|
"quyun/app/models"
|
||||||
"quyun/app/requests"
|
"quyun/app/requests"
|
||||||
|
"quyun/database/conds"
|
||||||
"quyun/database/fields"
|
"quyun/database/fields"
|
||||||
"quyun/database/schemas/public/model"
|
"quyun/database/schemas/public/model"
|
||||||
|
|
||||||
@@ -23,8 +24,12 @@ type posts struct{}
|
|||||||
// @Bind pagination query
|
// @Bind pagination query
|
||||||
// @Bind query query
|
// @Bind query query
|
||||||
func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery) (*requests.Pager, error) {
|
func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery) (*requests.Pager, error) {
|
||||||
cond := models.Posts.BuildConditionWithKey(query.Keyword)
|
conds := []conds.Cond{
|
||||||
pager, err := models.Posts.List(ctx.Context(), pagination, 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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
"quyun/app/models"
|
"quyun/app/models"
|
||||||
"quyun/app/requests"
|
"quyun/app/requests"
|
||||||
|
"quyun/database/conds"
|
||||||
"quyun/database/fields"
|
"quyun/database/fields"
|
||||||
"quyun/database/schemas/public/model"
|
"quyun/database/schemas/public/model"
|
||||||
"quyun/providers/ali"
|
"quyun/providers/ali"
|
||||||
@@ -35,14 +36,13 @@ type posts struct {
|
|||||||
// @Bind query query
|
// @Bind query query
|
||||||
// @Bind user local
|
// @Bind user local
|
||||||
func (ctl *posts) List(ctx fiber.Ctx, pagination *requests.Pagination, query *ListQuery, user *model.Users) (*requests.Pager, error) {
|
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, conds...)
|
||||||
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
|
|
||||||
})
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Errorf("post list err: %v", err)
|
log.WithError(err).Errorf("post list err: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -106,7 +106,8 @@ type PostItem struct {
|
|||||||
// @Bind user local
|
// @Bind user local
|
||||||
func (ctl *posts) Show(ctx fiber.Ctx, id int64, user *model.Users) (*PostItem, error) {
|
func (ctl *posts) Show(ctx fiber.Ctx, id int64, user *model.Users) (*PostItem, error) {
|
||||||
log.Infof("Fetching post with ID: %d", id)
|
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 {
|
if err != nil {
|
||||||
log.WithError(err).Errorf("GetByID err: %v", err)
|
log.WithError(err).Errorf("GetByID err: %v", err)
|
||||||
return nil, 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) {
|
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)
|
log.Infof("Fetching posts for user with pagination: %+v and keyword: %v", pagination, query.Keyword)
|
||||||
|
|
||||||
cond := models.Posts.BuildConditionWithKey(query.Keyword)
|
conds := []conds.Cond{
|
||||||
pager, err := models.Users.PostList(ctx.Context(), user.ID, pagination, cond, func(item model.Posts) model.Posts {
|
conds.Post_NotDeleted(),
|
||||||
item.Assets = fields.ToJson([]fields.MediaAsset{})
|
conds.Post_Status(fields.PostStatusPublished),
|
||||||
item.Content = ""
|
conds.Post_Like(query.Keyword),
|
||||||
return item
|
}
|
||||||
})
|
pager, err := models.Users.PostList(ctx.Context(), user.ID, pagination, conds...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Errorf("post list err: %v", err)
|
log.WithError(err).Errorf("post list err: %v", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"quyun/app/requests"
|
"quyun/app/requests"
|
||||||
|
"quyun/database/conds"
|
||||||
"quyun/database/schemas/public/model"
|
"quyun/database/schemas/public/model"
|
||||||
"quyun/database/schemas/public/table"
|
"quyun/database/schemas/public/table"
|
||||||
|
|
||||||
@@ -25,39 +26,15 @@ func (m *postsModel) Prepare() error {
|
|||||||
return nil
|
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
|
// 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
|
tbl := table.Posts
|
||||||
stmt := tbl.
|
|
||||||
SELECT(tbl.AllColumns).
|
var combinedCond BoolExpression = tbl.ID.EQ(Int64(id))
|
||||||
WHERE(
|
for _, c := range cond {
|
||||||
tbl.ID.EQ(Int64(id)).AND(
|
combinedCond = c(combinedCond)
|
||||||
tbl.DeletedAt.IS_NULL(),
|
}
|
||||||
),
|
stmt := tbl.SELECT(tbl.AllColumns).WHERE(combinedCond)
|
||||||
)
|
|
||||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||||
|
|
||||||
var post model.Posts
|
var post model.Posts
|
||||||
@@ -121,13 +98,18 @@ func (m *postsModel) countByCondition(ctx context.Context, expr BoolExpression)
|
|||||||
return cnt.Cnt, nil
|
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()
|
pagination.Format()
|
||||||
|
|
||||||
|
combinedCond := table.Posts.DeletedAt.IS_NULL()
|
||||||
|
for _, c := range cond {
|
||||||
|
combinedCond = c(combinedCond)
|
||||||
|
}
|
||||||
|
|
||||||
tbl := table.Posts
|
tbl := table.Posts
|
||||||
stmt := tbl.
|
stmt := tbl.
|
||||||
SELECT(tbl.AllColumns).
|
SELECT(tbl.AllColumns).
|
||||||
WHERE(cond).
|
WHERE(combinedCond).
|
||||||
ORDER_BY(tbl.ID.DESC()).
|
ORDER_BY(tbl.ID.DESC()).
|
||||||
LIMIT(pagination.Limit).
|
LIMIT(pagination.Limit).
|
||||||
OFFSET(pagination.Offset)
|
OFFSET(pagination.Offset)
|
||||||
@@ -140,20 +122,12 @@ func (m *postsModel) List(ctx context.Context, pagination *requests.Pagination,
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
count, err := m.countByCondition(ctx, cond)
|
count, err := m.countByCondition(ctx, combinedCond)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
m.log.Errorf("error getting post count: %v", err)
|
m.log.Errorf("error getting post count: %v", err)
|
||||||
return nil, 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{
|
return &requests.Pager{
|
||||||
Items: posts,
|
Items: posts,
|
||||||
Total: count,
|
Total: count,
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"quyun/app/requests"
|
"quyun/app/requests"
|
||||||
|
"quyun/database/conds"
|
||||||
"quyun/database/fields"
|
"quyun/database/fields"
|
||||||
"quyun/database/schemas/public/model"
|
"quyun/database/schemas/public/model"
|
||||||
"quyun/database/schemas/public/table"
|
"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
|
// 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()
|
pagination.Format()
|
||||||
|
|
||||||
tblUserPosts := table.UserPosts
|
tblUserPosts := table.UserPosts
|
||||||
|
|
||||||
cond = cond.AND(
|
combineConds := tblUserPosts.UserID.EQ(Int64(userId))
|
||||||
tblUserPosts.UserID.EQ(Int64(userId)),
|
for _, c := range conds {
|
||||||
)
|
combineConds = c(combineConds)
|
||||||
|
}
|
||||||
|
|
||||||
tbl := table.Posts
|
tbl := table.Posts
|
||||||
stmt := SELECT(tbl.AllColumns).
|
stmt := SELECT(tbl.AllColumns).
|
||||||
@@ -211,7 +213,7 @@ func (m *usersModel) PostList(ctx context.Context, userId int64, pagination *req
|
|||||||
tblUserPosts.PostID.EQ(tbl.ID),
|
tblUserPosts.PostID.EQ(tbl.ID),
|
||||||
),
|
),
|
||||||
).
|
).
|
||||||
WHERE(cond).
|
WHERE(combineConds).
|
||||||
ORDER_BY(tblUserPosts.ID.DESC()).
|
ORDER_BY(tblUserPosts.ID.DESC()).
|
||||||
LIMIT(pagination.Limit).
|
LIMIT(pagination.Limit).
|
||||||
OFFSET(pagination.Offset)
|
OFFSET(pagination.Offset)
|
||||||
@@ -244,14 +246,6 @@ func (m *usersModel) PostList(ctx context.Context, userId int64, pagination *req
|
|||||||
return nil, 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{
|
return &requests.Pager{
|
||||||
Items: posts,
|
Items: posts,
|
||||||
Total: cnt.Cnt,
|
Total: cnt.Cnt,
|
||||||
|
|||||||
11
backend/database/conds/conds.go
Normal file
11
backend/database/conds/conds.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package conds
|
||||||
|
|
||||||
|
import (
|
||||||
|
. "github.com/go-jet/jet/v2/postgres"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Cond func(BoolExpression) BoolExpression
|
||||||
|
|
||||||
|
func Default() BoolExpression {
|
||||||
|
return BoolExp(Bool(true))
|
||||||
|
}
|
||||||
41
backend/database/conds/posts.go
Normal file
41
backend/database/conds/posts.go
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
package conds
|
||||||
|
|
||||||
|
import (
|
||||||
|
"quyun/database/fields"
|
||||||
|
"quyun/database/schemas/public/table"
|
||||||
|
|
||||||
|
. "github.com/go-jet/jet/v2/postgres"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Post_NotDeleted() Cond {
|
||||||
|
return func(cond BoolExpression) BoolExpression {
|
||||||
|
return cond.AND(table.Posts.DeletedAt.IS_NULL())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Post_Status(s fields.PostStatus) Cond {
|
||||||
|
return func(cond BoolExpression) BoolExpression {
|
||||||
|
return cond.AND(table.Posts.Status.EQ(Int(int64(s))))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Post_Like(key *string) Cond {
|
||||||
|
return func(cond BoolExpression) BoolExpression {
|
||||||
|
tbl := table.Posts
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user