feat: fix issues

This commit is contained in:
Rogee
2025-05-26 12:25:55 +08:00
parent 51ed5685dc
commit d634e0175b
10 changed files with 43 additions and 17 deletions

View File

@@ -82,7 +82,7 @@ func (m *Medias) BatchDelete(ctx context.Context, ids []int64) error {
}
func (m *Medias) Update(ctx context.Context) error {
stmt := tblMedias.UPDATE(tblMediasUpdateMutableColumns).SET(m).WHERE(tblMedias.ID.EQ(Int(m.ID))).RETURNING(tblMedias.AllColumns)
stmt := tblMedias.UPDATE(tblMediasUpdateMutableColumns).MODEL(m).WHERE(tblMedias.ID.EQ(Int(m.ID))).RETURNING(tblMedias.AllColumns)
m.log().WithField("func", "Update").Info(stmt.DebugSql())
if err := stmt.QueryContext(ctx, db, m); err != nil {

View File

@@ -85,10 +85,7 @@ func (m *Orders) BatchDelete(ctx context.Context, ids []int64) error {
func (m *Orders) Update(ctx context.Context) error {
m.UpdatedAt = time.Now()
stmt := tblOrders.UPDATE(tblOrdersUpdateMutableColumns).
SET(m).
WHERE(tblOrders.ID.EQ(Int(m.ID))).
RETURNING(tblOrders.AllColumns)
stmt := tblOrders.UPDATE(tblOrdersUpdateMutableColumns).MODEL(m).WHERE(tblOrders.ID.EQ(Int(m.ID))).RETURNING(tblOrders.AllColumns)
m.log().WithField("func", "Update").Info(stmt.DebugSql())
if err := stmt.QueryContext(ctx, db, m); err != nil {

View File

@@ -126,7 +126,7 @@ func (m *Posts) BatchForceDelete(ctx context.Context, ids []int64) error {
func (m *Posts) Update(ctx context.Context) error {
m.UpdatedAt = time.Now()
stmt := tblPosts.UPDATE(tblPostsUpdateMutableColumns).SET(m).WHERE(tblPosts.ID.EQ(Int(m.ID))).RETURNING(tblPosts.AllColumns)
stmt := tblPosts.UPDATE(tblPostsUpdateMutableColumns).MODEL(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 {

View File

@@ -48,12 +48,13 @@ func (m *Posts) CondLike(key *string) Cond {
func (m *Posts) IncrViewCount(ctx context.Context) error {
tbl := tblPosts
stmt := tbl.UPDATE(tbl.Views).SET(tbl.Views.ADD(Int64(1))).WHERE(tbl.ID.EQ(Int64(m.ID)))
stmt := tbl.UPDATE(tbl.Views).
SET(tbl.Views.ADD(Int64(1))).
WHERE(tbl.ID.EQ(Int64(m.ID))).
RETURNING(tblPosts.AllColumns)
m.log().Infof("sql: %s", stmt.DebugSql())
var post Posts
err := stmt.QueryContext(ctx, db, &post)
if err != nil {
if err := stmt.QueryContext(ctx, db, m); err != nil {
m.log().Errorf("error updating post view count: %v", err)
return err
}

View File

@@ -64,3 +64,18 @@ func (s *PostsTestSuite) Test_Create() {
So(post.UpdatedAt, ShouldNotBeZeroValue)
})
}
func (s *PostsTestSuite) Test_IncrView() {
Convey("Test_IncrView", s.T(), func() {
// database.Truncate(context.Background(), db, tblPosts.TableName())
post, err := PostsModel().GetByID(context.Background(), 1)
So(err, ShouldBeNil)
oldViews := post.Views
So(post, ShouldNotBeNil)
err = post.IncrViewCount(context.Background())
So(err, ShouldBeNil)
So(post.Views, ShouldBeGreaterThan, oldViews)
})
}

View File

@@ -85,7 +85,7 @@ func (m *UserPosts) BatchDelete(ctx context.Context, ids []int64) error {
func (m *UserPosts) Update(ctx context.Context) error {
m.UpdatedAt = time.Now()
stmt := tblUserPosts.UPDATE(tblUserPostsUpdateMutableColumns).SET(m).WHERE(tblUserPosts.ID.EQ(Int(m.ID))).RETURNING(tblUserPosts.AllColumns)
stmt := tblUserPosts.UPDATE(tblUserPostsUpdateMutableColumns).MODEL(m).WHERE(tblUserPosts.ID.EQ(Int(m.ID))).RETURNING(tblUserPosts.AllColumns)
m.log().WithField("func", "Update").Info(stmt.DebugSql())
if err := stmt.QueryContext(ctx, db, m); err != nil {

View File

@@ -126,7 +126,7 @@ func (m *Users) BatchForceDelete(ctx context.Context, ids []int64) error {
func (m *Users) Update(ctx context.Context) error {
m.UpdatedAt = time.Now()
stmt := tblUsers.UPDATE(tblUsersUpdateMutableColumns).SET(m).WHERE(tblUsers.ID.EQ(Int(m.ID))).RETURNING(tblUsers.AllColumns)
stmt := tblUsers.UPDATE(tblUsersUpdateMutableColumns).MODEL(m).WHERE(tblUsers.ID.EQ(Int(m.ID))).RETURNING(tblUsers.AllColumns)
m.log().WithField("func", "Update").Info(stmt.DebugSql())
if err := stmt.QueryContext(ctx, db, m); err != nil {

View File

@@ -13,7 +13,7 @@ import (
"github.com/samber/lo"
)
var tblUsersUpdateMutableColumns = tblOrders.MutableColumns.Except(
var tblUsersUpdateMutableColumns = tblUsers.MutableColumns.Except(
tblUsers.OpenID,
tblUsers.Balance,
tblUsers.CreatedAt,

View File

@@ -57,3 +57,20 @@ func (s *UsersTestSuite) Test_Create() {
So(user.ID, ShouldNotBeZeroValue)
})
}
func (s *UsersTestSuite) Test_Update() {
Convey("Test_Update", s.T(), func() {
user, err := UsersModel().GetByID(context.Background(), 1001)
So(err, ShouldBeNil)
So(user, ShouldNotBeNil)
user.Username = "TestUpdate"
err = user.Update(context.TODO())
So(err, ShouldBeNil)
updatedUser, err := UsersModel().GetByID(context.Background(), user.ID)
So(err, ShouldBeNil)
So(updatedUser, ShouldNotBeNil)
So(updatedUser.Username, ShouldEqual, "TestUpdate")
})
}

View File

@@ -198,8 +198,6 @@ github.com/lithammer/shortuuid/v3 v3.0.7 h1:trX0KTHy4Pbwo/6ia8fscyHoGA+mf1jWbPJV
github.com/lithammer/shortuuid/v3 v3.0.7/go.mod h1:vMk8ke37EmiewwolSO1NLW8vP4ZaKlRuDIi8tWWmAts=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/matoous/go-nanoid/v2 v2.1.0 h1:P64+dmq21hhWdtvZfEAofnvJULaRR1Yib0+PnU669bE=
github.com/matoous/go-nanoid/v2 v2.1.0/go.mod h1:KlbGNQ+FhrUNIHUxZdL63t7tl4LaPkZNpUULS8H4uVM=
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
@@ -256,8 +254,6 @@ github.com/riverqueue/river/rivertype v0.15.0 h1:+TXRnvQv1ulV24uQnsuZmbb3yJdmbpi
github.com/riverqueue/river/rivertype v0.15.0/go.mod h1:4vpt5ZSdZ35mFbRAV4oXgeRdH3Mq5h1pUzQTvaGfCUA=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogeecn/fabfile v1.6.0 h1:i0+Koa8yp3lsgEM8opcnya85cEohpGeGpm21xEekBmQ=
github.com/rogeecn/fabfile v1.6.0/go.mod h1:EPwX7TtVcIWSLJkJAqxSzYjM/aV1Q0wymcaXqnMgzas=
github.com/rogeecn/fabfile v1.7.0 h1:qtwkqaBsJjWrggbvznbd0HGyJ0ebBTOBE893JvD5Tng=
github.com/rogeecn/fabfile v1.7.0/go.mod h1:EPwX7TtVcIWSLJkJAqxSzYjM/aV1Q0wymcaXqnMgzas=
github.com/rogeecn/swag v1.0.1 h1:s1yxLgopqO1m8sqGjVmt6ocMBRubMPIh2JtIPG4xjQE=