feat: update toolchain

This commit is contained in:
Rogee
2025-05-26 10:53:26 +08:00
parent f28bc7226f
commit 51ed5685dc
17 changed files with 412 additions and 284 deletions

View File

@@ -6,43 +6,27 @@ import (
"quyun/app/requests"
"quyun/database/fields"
"quyun/database/table"
. "github.com/go-jet/jet/v2/postgres"
"github.com/samber/lo"
)
var postsUpdateExcludeColumns = []Column{
table.Posts.CreatedAt,
table.Posts.DeletedAt,
table.Posts.Views,
table.Posts.Likes,
}
func (m *Posts) Update(ctx context.Context) error {
m.UpdatedAt = time.Now()
stmt := table.Posts.UPDATE(table.Posts.MutableColumns.Except(table.Posts.CreatedAt, table.Posts.DeletedAt, table.Posts.Views, table.Posts.Likes)).SET(m).WHERE(table.Posts.ID.EQ(Int(m.ID))).RETURNING(table.Posts.AllColumns)
m.log().WithField("func", "Update").Info(stmt.DebugSql())
if err := stmt.QueryContext(ctx, db, m); err != nil {
m.log().WithField("func", "Update").Errorf("error updating Posts item: %v", err)
return err
}
m.log().WithField("func", "Update").Infof("Posts item updated successfully")
return nil
}
var tblPostsUpdateMutableColumns = tblPosts.MutableColumns.Except(
tblPosts.CreatedAt,
tblPosts.DeletedAt,
tblPosts.Views,
tblPosts.Likes,
)
func (m *Posts) CondStatus(s fields.PostStatus) Cond {
return func(cond BoolExpression) BoolExpression {
return cond.AND(table.Posts.Status.EQ(Int(int64(s))))
return cond.AND(tblPosts.Status.EQ(Int(int64(s))))
}
}
func (m *Posts) CondLike(key *string) Cond {
return func(cond BoolExpression) BoolExpression {
tbl := table.Posts
tbl := tblPosts
if key == nil || *key == "" {
return cond
}
@@ -62,7 +46,7 @@ func (m *Posts) CondLike(key *string) Cond {
}
func (m *Posts) IncrViewCount(ctx context.Context) error {
tbl := table.Posts
tbl := tblPosts
stmt := tbl.UPDATE(tbl.Views).SET(tbl.Views.ADD(Int64(1))).WHERE(tbl.ID.EQ(Int64(m.ID)))
m.log().Infof("sql: %s", stmt.DebugSql())
@@ -76,31 +60,12 @@ func (m *Posts) IncrViewCount(ctx context.Context) error {
return nil
}
// countByCond
func (m *Posts) 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 *Posts) List(ctx context.Context, pagination *requests.Pagination, conds ...Cond) (*requests.Pager, error) {
pagination.Format()
cond := CondJoin(m.CondNotDeleted(), conds...)
tbl := table.Posts
tbl := tblPosts
stmt := tbl.
SELECT(tbl.AllColumns).
WHERE(CondTrue(cond...)).
@@ -132,7 +97,7 @@ func (m *Posts) List(ctx context.Context, pagination *requests.Pagination, conds
// SendTo
func (m *Posts) SendTo(ctx context.Context, userId int64) error {
// add record to user_posts
tbl := table.UserPosts
tbl := tblUserPosts
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(UserPosts{
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
@@ -150,7 +115,7 @@ func (m *Posts) SendTo(ctx context.Context, userId int64) error {
// PostBoughtStatistics 获取指定文件 ID 的购买次数
func (m *Posts) BoughtStatistics(ctx context.Context, postIds []int64) (map[int64]int64, error) {
tbl := table.UserPosts
tbl := tblUserPosts
// select count(user_id), post_id from user_posts up where post_id in (1, 2,3,4,5,6,7,8,9,10) group by post_id
stmt := tbl.
@@ -190,15 +155,15 @@ func (m *Posts) Bought(ctx context.Context, userId int64, pagination *requests.P
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
tbl := tblUserPosts
stmt := tbl.
SELECT(
tbl.Price.AS("price"),
tbl.CreatedAt.AS("bought_at"),
table.Posts.Title.AS("title"),
tblPosts.Title.AS("title"),
).
FROM(
tbl.INNER_JOIN(table.Posts, table.Posts.ID.EQ(tbl.PostID)),
tbl.INNER_JOIN(tblPosts, tblPosts.ID.EQ(tbl.PostID)),
).
WHERE(
tbl.UserID.EQ(Int64(userId)),
@@ -248,7 +213,7 @@ func (m *Posts) GetPostsMapByIDs(ctx context.Context, ids []int64) (map[int64]Po
return nil, nil
}
tbl := table.Posts
tbl := tblPosts
stmt := tbl.
SELECT(tbl.AllColumns).
WHERE(
@@ -275,7 +240,7 @@ func (m *Posts) GetMediaByIds(ctx context.Context, ids []int64) ([]Medias, error
return nil, nil
}
tbl := table.Medias
tbl := tblMedias
stmt := tbl.
SELECT(tbl.AllColumns).
WHERE(