150 lines
3.2 KiB
Go
150 lines
3.2 KiB
Go
package models
|
|
|
|
import (
|
|
"context"
|
|
|
|
"quyun/app/requests"
|
|
"quyun/database/fields"
|
|
"quyun/database/schemas/public/model"
|
|
"quyun/database/schemas/public/table"
|
|
|
|
. "github.com/go-jet/jet/v2/postgres"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// @provider
|
|
type postsModel struct {
|
|
log *logrus.Entry `inject:"false"`
|
|
}
|
|
|
|
func (m *postsModel) Prepare() error {
|
|
m.log = logrus.WithField("model", "postsModel")
|
|
return nil
|
|
}
|
|
|
|
// BuildConditionWithKey
|
|
func (m *postsModel) BuildConditionWithKey(key *string) BoolExpression {
|
|
tbl := table.Posts
|
|
|
|
cond := tbl.DeletedAt.IS_NULL().AND(
|
|
tbl.Status.EQ(Int32(int32(fields.PostStatusPublished))),
|
|
)
|
|
|
|
if key == nil || *key == "" {
|
|
return cond
|
|
}
|
|
|
|
cond = 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) {
|
|
tbl := table.Posts
|
|
stmt := tbl.
|
|
SELECT(tbl.AllColumns).
|
|
WHERE(
|
|
tbl.ID.EQ(Int64(id)).AND(
|
|
tbl.DeletedAt.IS_NULL(),
|
|
).AND(
|
|
tbl.Status.EQ(Int32(int32(fields.PostStatusPublished))),
|
|
),
|
|
)
|
|
m.log.Infof("sql: %s", stmt.DebugSql())
|
|
|
|
var post model.Posts
|
|
err := stmt.QueryContext(ctx, db, &post)
|
|
if err != nil {
|
|
m.log.Errorf("error getting post: %v", err)
|
|
return nil, err
|
|
}
|
|
return &post, nil
|
|
}
|
|
|
|
// Create
|
|
func (m *postsModel) Create(ctx context.Context, model *model.Posts) error {
|
|
tbl := table.Posts
|
|
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(model)
|
|
m.log.Infof("sql: %s", stmt.DebugSql())
|
|
|
|
_, err := stmt.ExecContext(ctx, db)
|
|
if err != nil {
|
|
m.log.Errorf("error creating post: %v", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Update
|
|
func (m *postsModel) Update(ctx context.Context, id int64, model *model.Posts) error {
|
|
tbl := table.Posts
|
|
stmt := tbl.UPDATE(tbl.MutableColumns).SET(model).WHERE(tbl.ID.EQ(Int64(id)))
|
|
m.log.Infof("sql: %s", stmt.DebugSql())
|
|
|
|
_, err := stmt.ExecContext(ctx, db)
|
|
if err != nil {
|
|
m.log.Errorf("error updating post: %v", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// countByCond
|
|
func (m *postsModel) countByCondition(ctx context.Context, expr BoolExpression) (int64, error) {
|
|
var cnt struct {
|
|
Cnt int64
|
|
}
|
|
|
|
tbl := table.Posts
|
|
stmt := SELECT(COUNT(tbl.ID).AS("cnt")).FROM(tbl).WHERE(expr)
|
|
m.log.Infof("sql: %s", stmt.DebugSql())
|
|
|
|
err := stmt.QueryContext(ctx, db, &cnt)
|
|
if err != nil {
|
|
m.log.Errorf("error counting post items: %v", err)
|
|
return 0, err
|
|
}
|
|
|
|
return cnt.Cnt, nil
|
|
}
|
|
|
|
func (m *postsModel) List(ctx context.Context, pagination *requests.Pagination, cond BoolExpression) (*requests.Pager, error) {
|
|
limit := pagination.Limit
|
|
offset := pagination.Offset()
|
|
|
|
tbl := table.Posts
|
|
stmt := tbl.
|
|
SELECT(tbl.AllColumns).
|
|
WHERE(cond).
|
|
ORDER_BY(tbl.ID.DESC()).
|
|
LIMIT(limit).
|
|
OFFSET(offset)
|
|
m.log.Infof("sql: %s", stmt.DebugSql())
|
|
|
|
var posts []model.Posts
|
|
err := stmt.QueryContext(ctx, db, &posts)
|
|
if err != nil {
|
|
m.log.Errorf("error querying post items: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
count, err := m.countByCondition(ctx, cond)
|
|
if err != nil {
|
|
m.log.Errorf("error getting post count: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
return &requests.Pager{
|
|
Items: posts,
|
|
Total: count,
|
|
Pagination: *pagination,
|
|
}, nil
|
|
}
|