feat: fix issues
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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")
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user