feat: add user posts
This commit is contained in:
@@ -2,6 +2,7 @@ package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"quyun/app/requests"
|
||||
"quyun/database/fields"
|
||||
@@ -9,6 +10,7 @@ import (
|
||||
"quyun/database/schemas/public/table"
|
||||
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/go-jet/jet/v2/qrm"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -147,3 +149,58 @@ func (m *postsModel) List(ctx context.Context, pagination *requests.Pagination,
|
||||
Pagination: *pagination,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m *postsModel) IsUserBought(ctx context.Context, userId int64, 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 model.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 *postsModel) Buy(ctx context.Context, userId int64, 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 := Users.GetByID(ctx, userId)
|
||||
if err != nil {
|
||||
m.log.Errorf("error getting user by ID: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
record := model.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
|
||||
}
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"quyun/database/schemas/public/model"
|
||||
"quyun/database/schemas/public/table"
|
||||
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/samber/lo"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -13,3 +20,58 @@ func (m *usersModel) Prepare() error {
|
||||
m.log = logrus.WithField("model", "usersModel")
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetByID
|
||||
func (m *usersModel) GetByID(ctx context.Context, id int64) (*model.Users, error) {
|
||||
tbl := table.Users
|
||||
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(
|
||||
tbl.ID.EQ(Int64(id)),
|
||||
)
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
var user model.Users
|
||||
err := stmt.QueryContext(ctx, db, &user)
|
||||
if err != nil {
|
||||
m.log.Errorf("error querying user by ID: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (m *usersModel) Posts(ctx context.Context, userID int64) ([]*model.Posts, error) {
|
||||
tblUserPosts := table.UserPosts
|
||||
stmtUserPosts := tblUserPosts.
|
||||
SELECT(tblUserPosts.PostID).
|
||||
WHERE(tblUserPosts.UserID.EQ(Int64(userID)))
|
||||
m.log.Infof("sql: %s", stmtUserPosts.DebugSql())
|
||||
|
||||
var userPosts []model.UserPosts
|
||||
err := stmtUserPosts.QueryContext(ctx, db, &userPosts)
|
||||
if err != nil {
|
||||
m.log.Errorf("error querying user posts: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
postIds := lo.Map(userPosts, func(up model.UserPosts, _ int) Expression {
|
||||
return Int64(up.PostID)
|
||||
})
|
||||
|
||||
tbl := table.Posts
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(tbl.ID.IN(postIds...))
|
||||
m.log.Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
var posts []*model.Posts
|
||||
|
||||
if err := stmt.QueryContext(ctx, db, &posts); err != nil {
|
||||
m.log.Errorf("error querying posts by user ID: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return posts, nil
|
||||
}
|
||||
|
||||
@@ -45,7 +45,9 @@ func Command() atom.Option {
|
||||
defaultProviders().
|
||||
With(
|
||||
jobs.Provide,
|
||||
appHttp.Provide,
|
||||
).
|
||||
WithProviders(
|
||||
appHttp.Providers(),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user