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

@@ -4,8 +4,6 @@ import (
"context"
"time"
"quyun/database/table"
. "github.com/go-jet/jet/v2/postgres"
"github.com/samber/lo"
log "github.com/sirupsen/logrus"
@@ -14,13 +12,19 @@ import (
// conds
func (m *Posts) CondNotDeleted() Cond {
return func(cond BoolExpression) BoolExpression {
return cond.AND(table.Posts.DeletedAt.IS_NULL())
return cond.AND(tblPosts.DeletedAt.IS_NULL())
}
}
func (m *Posts) CondDeleted() Cond {
return func(cond BoolExpression) BoolExpression {
return cond.AND(tblPosts.DeletedAt.IS_NOT_NULL())
}
}
func (m *Posts) CondID(id int64) Cond {
return func(cond BoolExpression) BoolExpression {
return cond.AND(table.Posts.ID.EQ(Int(id)))
return cond.AND(tblPosts.ID.EQ(Int(id)))
}
}
@@ -33,7 +37,7 @@ func (m *Posts) Create(ctx context.Context) error {
m.CreatedAt = time.Now()
m.UpdatedAt = time.Now()
stmt := table.Medias.INSERT(table.Posts.MutableColumns).MODEL(m).RETURNING(table.Medias.AllColumns)
stmt := tblMedias.INSERT(tblPosts.MutableColumns).MODEL(m).RETURNING(tblMedias.AllColumns)
m.log().WithField("func", "Create").Info(stmt.DebugSql())
if err := stmt.QueryContext(ctx, db, m); err != nil {
@@ -46,7 +50,7 @@ func (m *Posts) Create(ctx context.Context) error {
}
func (m *Posts) BatchCreate(ctx context.Context, models []*Posts) error {
stmt := table.Posts.INSERT(table.Posts.MutableColumns).MODELS(models)
stmt := tblPosts.INSERT(tblPosts.MutableColumns).MODELS(models)
m.log().WithField("func", "BatchCreate").Info(stmt.DebugSql())
if _, err := stmt.ExecContext(ctx, db); err != nil {
@@ -59,7 +63,7 @@ func (m *Posts) BatchCreate(ctx context.Context, models []*Posts) error {
}
func (m *Posts) Delete(ctx context.Context) error {
stmt := table.Posts.UPDATE().SET(table.Posts.DeletedAt.SET(TimestampT(time.Now()))).WHERE(table.Posts.ID.EQ(Int(m.ID)))
stmt := tblPosts.UPDATE().SET(tblPosts.DeletedAt.SET(TimestampT(time.Now()))).WHERE(tblPosts.ID.EQ(Int(m.ID)))
m.log().WithField("func", "SoftDelete").Info(stmt.DebugSql())
if err := stmt.QueryContext(ctx, db, m); err != nil {
@@ -77,7 +81,7 @@ func (m *Posts) BatchDelete(ctx context.Context, ids []int64) error {
return Int64(id)
})
stmt := table.Posts.UPDATE().SET(table.Posts.DeletedAt.SET(TimestampT(time.Now()))).WHERE(table.Posts.ID.IN(condIds...))
stmt := tblPosts.UPDATE().SET(tblPosts.DeletedAt.SET(TimestampT(time.Now()))).WHERE(tblPosts.ID.IN(condIds...))
m.log().WithField("func", "BatchSoftDelete").Info(stmt.DebugSql())
if err := stmt.QueryContext(ctx, db, m); err != nil {
@@ -85,12 +89,12 @@ func (m *Posts) BatchDelete(ctx context.Context, ids []int64) error {
return err
}
m.log().WithField("func", "BatchSoftDelete").Infof("Posts items soft deleted successfully")
m.log().WithField("func", "BatchSoftDelete").WithField("ids", ids).Infof("Posts items soft deleted successfully")
return nil
}
func (m *Posts) ForceDelete(ctx context.Context) error {
stmt := table.Posts.DELETE().WHERE(table.Posts.ID.EQ(Int(m.ID)))
stmt := tblPosts.DELETE().WHERE(tblPosts.ID.EQ(Int(m.ID)))
m.log().WithField("func", "Delete").Info(stmt.DebugSql())
if _, err := stmt.ExecContext(ctx, db); err != nil {
@@ -107,40 +111,38 @@ func (m *Posts) BatchForceDelete(ctx context.Context, ids []int64) error {
return Int64(id)
})
stmt := table.Posts.DELETE().WHERE(table.Posts.ID.IN(condIds...))
stmt := tblPosts.DELETE().WHERE(tblPosts.ID.IN(condIds...))
m.log().WithField("func", "BatchDelete").Info(stmt.DebugSql())
if _, err := stmt.ExecContext(ctx, db); err != nil {
m.log().WithField("func", "BatchDelete").Errorf("error deleting Posts items: %v", err)
m.log().WithField("func", "BatchForceDelete").Errorf("error deleting Posts items: %v", err)
return err
}
m.log().WithField("func", "BatchDelete").Infof("Posts items deleted successfully")
m.log().WithField("func", "BatchForceDelete").WithField("ids", ids).Infof("Posts items deleted successfully")
return nil
}
// func (m *Posts) Update(ctx context.Context) error {
//
// m.UpdatedAt = time.Now()
//
func (m *Posts) Update(ctx context.Context) error {
m.UpdatedAt = time.Now()
// stmt := table.Posts.UPDATE(table.Posts.MutableColumns.Except(postsUpdateExcludeColumns...)).SET(m).WHERE(table.Posts.ID.EQ(Int(m.ID))).RETURNING(table.Posts.AllColumns)
// m.log().WithField("func", "Update").Info(stmt.DebugSql())
stmt := tblPosts.UPDATE(tblPostsUpdateMutableColumns).SET(m).WHERE(tblPosts.ID.EQ(Int(m.ID))).RETURNING(tblPosts.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
// }
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
// }
m.log().WithField("func", "Update").Infof("Posts item updated successfully")
return nil
}
// GetByCond
func (m *Posts) GetByCond(ctx context.Context, conds ...Cond) (*Posts, error) {
cond := CondTrue(conds...)
stmt := table.Posts.SELECT(table.Posts.AllColumns).WHERE(cond)
stmt := tblPosts.SELECT(tblPosts.AllColumns).WHERE(cond)
m.log().WithField("func", "GetByCond").Info(stmt.DebugSql())
if err := stmt.QueryContext(ctx, db, m); err != nil {
@@ -161,7 +163,7 @@ func (m *Posts) GetByID(ctx context.Context, id int64, conds ...Cond) (*Posts, e
func (m *Posts) Count(ctx context.Context, conds ...Cond) (int64, error) {
cond := CondTrue(conds...)
tbl := table.Posts
tbl := tblPosts
stmt := tbl.SELECT(COUNT(tbl.ID).AS("count")).WHERE(cond)
m.log().Infof("sql: %s", stmt.DebugSql())