feat: update
This commit is contained in:
@@ -5,16 +5,45 @@ import (
|
||||
"time"
|
||||
|
||||
"quyun/app/requests"
|
||||
"quyun/database/conds"
|
||||
"quyun/database/fields"
|
||||
"quyun/database/table"
|
||||
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/samber/lo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (m *Posts) log() *log.Entry {
|
||||
return log.WithField("model", "PostsModel")
|
||||
var postsUpdateExcludeColumns = []Column{
|
||||
table.Posts.CreatedAt,
|
||||
table.Posts.DeletedAt,
|
||||
table.Posts.Views,
|
||||
table.Posts.Likes,
|
||||
}
|
||||
|
||||
func (m *Posts) CondStatus(s fields.PostStatus) Cond {
|
||||
return func(cond BoolExpression) BoolExpression {
|
||||
return cond.AND(table.Posts.Status.EQ(Int(int64(s))))
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Posts) CondLike(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
|
||||
}
|
||||
}
|
||||
|
||||
func (m *Posts) IncrViewCount(ctx context.Context) error {
|
||||
@@ -32,67 +61,6 @@ func (m *Posts) IncrViewCount(ctx context.Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetByID
|
||||
func (m *Posts) GetByID(ctx context.Context, id int64, cond ...conds.Cond) (*Posts, error) {
|
||||
tbl := table.Posts
|
||||
|
||||
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 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 *Posts) Create(ctx context.Context) error {
|
||||
m.CreatedAt = time.Now()
|
||||
m.UpdatedAt = time.Now()
|
||||
|
||||
tbl := table.Posts
|
||||
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(m).RETURNING(tbl.AllColumns)
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||
m.log().Errorf("error creating post: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Update
|
||||
func (m *Posts) Update(ctx context.Context) error {
|
||||
m.UpdatedAt = time.Now()
|
||||
|
||||
tbl := table.Posts
|
||||
stmt := tbl.
|
||||
UPDATE(
|
||||
tbl.MutableColumns.Except(
|
||||
tbl.CreatedAt,
|
||||
tbl.DeletedAt,
|
||||
tbl.Views,
|
||||
tbl.Likes,
|
||||
),
|
||||
).
|
||||
MODEL(m).
|
||||
WHERE(tbl.ID.EQ(Int64(m.ID)))
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||
m.log().Errorf("error updating post: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// countByCond
|
||||
func (m *Posts) countByCondition(ctx context.Context, expr BoolExpression) (int64, error) {
|
||||
var cnt struct {
|
||||
@@ -112,18 +80,15 @@ func (m *Posts) countByCondition(ctx context.Context, expr BoolExpression) (int6
|
||||
return cnt.Cnt, nil
|
||||
}
|
||||
|
||||
func (m *Posts) List(ctx context.Context, pagination *requests.Pagination, cond ...conds.Cond) (*requests.Pager, error) {
|
||||
func (m *Posts) List(ctx context.Context, pagination *requests.Pagination, conds ...Cond) (*requests.Pager, error) {
|
||||
pagination.Format()
|
||||
|
||||
combinedCond := table.Posts.DeletedAt.IS_NULL()
|
||||
for _, c := range cond {
|
||||
combinedCond = c(combinedCond)
|
||||
}
|
||||
cond := CondJoin(m.CondNotDeleted(), conds...)
|
||||
|
||||
tbl := table.Posts
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(combinedCond).
|
||||
WHERE(CondTrue(cond...)).
|
||||
ORDER_BY(tbl.ID.DESC()).
|
||||
LIMIT(pagination.Limit).
|
||||
OFFSET(pagination.Offset)
|
||||
@@ -136,7 +101,7 @@ func (m *Posts) List(ctx context.Context, pagination *requests.Pagination, cond
|
||||
return nil, err
|
||||
}
|
||||
|
||||
count, err := m.countByCondition(ctx, combinedCond)
|
||||
count, err := m.Count(ctx, CondJoin(m.CondNotDeleted(), conds...)...)
|
||||
if err != nil {
|
||||
m.log().Errorf("error getting post count: %v", err)
|
||||
return nil, err
|
||||
@@ -149,24 +114,6 @@ func (m *Posts) List(ctx context.Context, pagination *requests.Pagination, cond
|
||||
}, nil
|
||||
}
|
||||
|
||||
// DeleteByID soft delete item
|
||||
func (m *Posts) Delete(ctx context.Context) error {
|
||||
tbl := table.Posts
|
||||
stmt := tbl.
|
||||
UPDATE(tbl.DeletedAt).
|
||||
SET(TimestampT(time.Now())).
|
||||
WHERE(
|
||||
tbl.ID.EQ(Int64(m.ID)),
|
||||
)
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||
m.log().Errorf("error deleting post: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SendTo
|
||||
func (m *Posts) SendTo(ctx context.Context, userId int64) error {
|
||||
// add record to user_posts
|
||||
@@ -332,20 +279,6 @@ func (m *Posts) GetMediaByIds(ctx context.Context, ids []int64) ([]Medias, error
|
||||
return medias, nil
|
||||
}
|
||||
|
||||
// Count
|
||||
func (m *Posts) Count(ctx context.Context, cond BoolExpression) (int64, error) {
|
||||
tbl := table.Posts
|
||||
stmt := tbl.
|
||||
SELECT(COUNT(tbl.ID).AS("count")).
|
||||
WHERE(cond)
|
||||
|
||||
var count struct {
|
||||
Count int64
|
||||
}
|
||||
if err := stmt.QueryContext(ctx, db, &count); err != nil {
|
||||
m.log().Errorf("error counting posts: %v", err)
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return count.Count, nil
|
||||
func (m *Posts) PayPrice() int64 {
|
||||
return m.Price * int64(m.Discount) / 100
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user