feat: update posts
This commit is contained in:
@@ -2,7 +2,6 @@ package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"quyun/app/requests"
|
||||
@@ -10,7 +9,6 @@ import (
|
||||
"quyun/database/table"
|
||||
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/go-jet/jet/v2/qrm"
|
||||
"github.com/samber/lo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
@@ -19,10 +17,10 @@ func (m *Posts) log() *log.Entry {
|
||||
return log.WithField("model", "PostsModel")
|
||||
}
|
||||
|
||||
func (m *Posts) IncrViewCount(ctx context.Context, id int64) error {
|
||||
func (m *Posts) IncrViewCount(ctx context.Context) error {
|
||||
tbl := table.Posts
|
||||
|
||||
stmt := tbl.UPDATE(tbl.Views).SET(tbl.Views.ADD(Int64(1))).WHERE(tbl.ID.EQ(Int64(id)))
|
||||
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())
|
||||
|
||||
var post Posts
|
||||
@@ -55,16 +53,15 @@ func (m *Posts) GetByID(ctx context.Context, id int64, cond ...conds.Cond) (*Pos
|
||||
}
|
||||
|
||||
// Create
|
||||
func (m *Posts) Create(ctx context.Context, post *Posts) error {
|
||||
post.CreatedAt = time.Now()
|
||||
post.UpdatedAt = time.Now()
|
||||
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(post)
|
||||
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(m).RETURNING(tbl.AllColumns)
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
_, err := stmt.ExecContext(ctx, db)
|
||||
if err != nil {
|
||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||
m.log().Errorf("error creating post: %v", err)
|
||||
return err
|
||||
}
|
||||
@@ -72,15 +69,24 @@ func (m *Posts) Create(ctx context.Context, post *Posts) error {
|
||||
}
|
||||
|
||||
// Update
|
||||
func (m *Posts) Update(ctx context.Context, id int64, posts *Posts) error {
|
||||
posts.UpdatedAt = time.Now()
|
||||
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)).MODEL(posts).WHERE(tbl.ID.EQ(Int64(id)))
|
||||
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())
|
||||
|
||||
_, err := stmt.ExecContext(ctx, db)
|
||||
if err != nil {
|
||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||
m.log().Errorf("error updating post: %v", err)
|
||||
return err
|
||||
}
|
||||
@@ -143,69 +149,14 @@ func (m *Posts) List(ctx context.Context, pagination *requests.Pagination, cond
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m *Posts) IsUserBought(ctx context.Context, userId, postId int64) (bool, error) {
|
||||
tbl := table.UserPosts
|
||||
stmt := tbl.
|
||||
SELECT(tbl.ID).
|
||||
WHERE(
|
||||
tbl.UserID.EQ(Int64(userId)).AND(
|
||||
tbl.PostID.EQ(Int64(postId)),
|
||||
),
|
||||
)
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
var userPost UserPosts
|
||||
err := stmt.QueryContext(ctx, db, &userPost)
|
||||
if err != nil {
|
||||
if errors.Is(err, qrm.ErrNoRows) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
m.log().Errorf("error querying user post item: %v", err)
|
||||
return false, err
|
||||
}
|
||||
|
||||
return userPost.ID > 0, nil
|
||||
}
|
||||
|
||||
func (m *Posts) Buy(ctx context.Context, userId, postId int64) error {
|
||||
tbl := table.UserPosts
|
||||
|
||||
post, err := m.GetByID(ctx, postId)
|
||||
if err != nil {
|
||||
m.log().Errorf("error getting post by ID: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
user, err := UsersModel.GetByID(ctx, userId)
|
||||
if err != nil {
|
||||
m.log().Errorf("error getting user by ID: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
record := UserPosts{
|
||||
UserID: user.ID,
|
||||
PostID: post.ID,
|
||||
Price: post.Price * int64(post.Discount) / 100,
|
||||
}
|
||||
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(record)
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||
m.log().Errorf("error buying post: %v", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// DeleteByID soft delete item
|
||||
func (m *Posts) DeleteByID(ctx context.Context, id int64) error {
|
||||
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(id)),
|
||||
tbl.ID.EQ(Int64(m.ID)),
|
||||
)
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
@@ -217,14 +168,14 @@ func (m *Posts) DeleteByID(ctx context.Context, id int64) error {
|
||||
}
|
||||
|
||||
// SendTo
|
||||
func (m *Posts) SendTo(ctx context.Context, postId, userId int64) error {
|
||||
func (m *Posts) SendTo(ctx context.Context, userId int64) error {
|
||||
// add record to user_posts
|
||||
tbl := table.UserPosts
|
||||
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(UserPosts{
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
UserID: userId,
|
||||
PostID: postId,
|
||||
PostID: m.ID,
|
||||
Price: -1,
|
||||
})
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"quyun/app/service/testx"
|
||||
"quyun/database"
|
||||
"quyun/database/fields"
|
||||
"quyun/database/table"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
@@ -41,3 +42,26 @@ func (s *PostsTestSuite) Test_Demo() {
|
||||
database.Truncate(context.Background(), db, table.Posts.TableName())
|
||||
})
|
||||
}
|
||||
|
||||
// Test_Create
|
||||
func (s *PostsTestSuite) Test_Create() {
|
||||
Convey("Test_Create", s.T(), func() {
|
||||
// database.Truncate(context.Background(), db, table.Posts.TableName())
|
||||
|
||||
post := &Posts{
|
||||
Title: "Test Post",
|
||||
Description: "This is a test post",
|
||||
Content: "Post content goes here",
|
||||
Price: 100,
|
||||
Discount: 10,
|
||||
Status: fields.PostStatusDraft,
|
||||
}
|
||||
|
||||
err := post.Create(context.Background())
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
So(post.ID, ShouldNotBeZeroValue)
|
||||
So(post.CreatedAt, ShouldNotBeZeroValue)
|
||||
So(post.UpdatedAt, ShouldNotBeZeroValue)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user