feat: update posts
This commit is contained in:
@@ -99,7 +99,7 @@ func (ctl *posts) Create(ctx fiber.Ctx, form *PostForm) error {
|
||||
post.Assets = fields.ToJson(assets)
|
||||
}
|
||||
|
||||
if err := model.PostsModel.Create(ctx.Context(), &post); err != nil {
|
||||
if err := post.Create(ctx.Context()); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -111,27 +111,18 @@ func (ctl *posts) Create(ctx fiber.Ctx, form *PostForm) error {
|
||||
// @Bind id path
|
||||
// @Bind form body
|
||||
func (ctl *posts) Update(ctx fiber.Ctx, id int64, form *PostForm) error {
|
||||
oldPost, err := model.PostsModel.GetByID(ctx.Context(), id)
|
||||
post, err := model.PostsModel.GetByID(ctx.Context(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
post := &model.Posts{
|
||||
Title: form.Title,
|
||||
HeadImages: fields.ToJson(form.HeadImages),
|
||||
Price: form.Price,
|
||||
Discount: form.Discount,
|
||||
Description: form.Introduction,
|
||||
Status: form.Status,
|
||||
Content: form.Content,
|
||||
Tags: fields.Json[[]string]{},
|
||||
Assets: fields.Json[[]fields.MediaAsset]{},
|
||||
CreatedAt: oldPost.CreatedAt,
|
||||
UpdatedAt: oldPost.UpdatedAt,
|
||||
DeletedAt: oldPost.DeletedAt,
|
||||
Views: oldPost.Views,
|
||||
Likes: oldPost.Likes,
|
||||
}
|
||||
post.Title = form.Title
|
||||
post.HeadImages = fields.ToJson(form.HeadImages)
|
||||
post.Price = form.Price
|
||||
post.Discount = form.Discount
|
||||
post.Description = form.Introduction
|
||||
post.Status = form.Status
|
||||
post.Content = form.Content
|
||||
post.Tags = fields.Json[[]string]{}
|
||||
|
||||
if form.Medias != nil {
|
||||
medias, err := model.MediasModel.GetByIds(ctx.Context(), form.Medias)
|
||||
@@ -148,7 +139,7 @@ func (ctl *posts) Update(ctx fiber.Ctx, id int64, form *PostForm) error {
|
||||
post.Assets = fields.ToJson(assets)
|
||||
}
|
||||
|
||||
if err := model.PostsModel.Update(ctx.Context(), id, post); err != nil {
|
||||
if err := post.Update(ctx.Context()); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -167,7 +158,7 @@ func (ctl *posts) Delete(ctx fiber.Ctx, id int64) error {
|
||||
return fiber.ErrNotFound
|
||||
}
|
||||
|
||||
if err := model.PostsModel.DeleteByID(ctx.Context(), id); err != nil {
|
||||
if err := post.Delete(ctx.Context()); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@@ -207,15 +198,17 @@ func (ctl *posts) Show(ctx fiber.Ctx, id int64) (*PostItem, error) {
|
||||
// @Bind id path
|
||||
// @Bind userId path
|
||||
func (ctl *posts) SendTo(ctx fiber.Ctx, id, userId int64) error {
|
||||
if _, err := model.PostsModel.GetByID(ctx.Context(), id); err != nil {
|
||||
post, err := model.PostsModel.GetByID(ctx.Context(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := model.UsersModel.GetByID(ctx.Context(), userId); err != nil {
|
||||
user, err := model.UsersModel.GetByID(ctx.Context(), userId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := model.PostsModel.SendTo(ctx.Context(), id, userId); err != nil {
|
||||
if err := post.SendTo(ctx.Context(), user.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -184,7 +184,7 @@ func (ctl *posts) Play(ctx fiber.Ctx, id int64, user *model.Users) (*PlayUrl, er
|
||||
log.WithError(err).Errorf("GetByID err: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
go model.PostsModel.IncrViewCount(ctx.Context(), post.ID)
|
||||
go post.IncrViewCount(ctx.Context())
|
||||
|
||||
for _, asset := range post.Assets.Data {
|
||||
if asset.Type == "video/mp4" && asset.Metas != nil && asset.Metas.Short == preview {
|
||||
|
||||
@@ -97,7 +97,7 @@ func (w *PublishDraftPostsWorker) Work(ctx context.Context, job *Job[PublishDraf
|
||||
return media.ID, media.MimeType == "image/jpeg"
|
||||
})),
|
||||
}
|
||||
if err := model.PostsModel.Create(ctx, post); err != nil {
|
||||
if err := post.Create(ctx); err != nil {
|
||||
log.Errorf("Error creating post: %v", err)
|
||||
return errors.Wrap(err, "create post")
|
||||
}
|
||||
|
||||
@@ -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