feat: update toolchain
This commit is contained in:
@@ -6,7 +6,6 @@ import (
|
||||
|
||||
"quyun/app/requests"
|
||||
"quyun/database/fields"
|
||||
"quyun/database/table"
|
||||
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/go-jet/jet/v2/qrm"
|
||||
@@ -14,31 +13,16 @@ import (
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
var usersUpdateExcludeColumns = []Column{
|
||||
table.Users.OpenID,
|
||||
table.Users.Balance,
|
||||
table.Users.CreatedAt,
|
||||
table.Users.DeletedAt,
|
||||
}
|
||||
|
||||
func (m *Users) Update(ctx context.Context) error {
|
||||
m.UpdatedAt = time.Now()
|
||||
|
||||
stmt := table.Users.UPDATE(table.Users.MutableColumns.Except(table.Users.OpenID, table.Users.Balance, table.Users.CreatedAt, table.Users.DeletedAt)).SET(m).WHERE(table.Users.ID.EQ(Int(m.ID))).RETURNING(table.Users.AllColumns)
|
||||
m.log().WithField("func", "Update").Info(stmt.DebugSql())
|
||||
|
||||
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||
m.log().WithField("func", "Update").Errorf("error updating Users item: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
m.log().WithField("func", "Update").Infof("Users item updated successfully")
|
||||
return nil
|
||||
}
|
||||
var tblUsersUpdateMutableColumns = tblOrders.MutableColumns.Except(
|
||||
tblUsers.OpenID,
|
||||
tblUsers.Balance,
|
||||
tblUsers.CreatedAt,
|
||||
tblUsers.DeletedAt,
|
||||
)
|
||||
|
||||
// BuildConditionWithKey builds the WHERE clause for user queries
|
||||
func (m *Users) BuildConditionWithKey(key *string) BoolExpression {
|
||||
tbl := table.Users
|
||||
tbl := tblUsers
|
||||
|
||||
cond := tbl.DeletedAt.IS_NULL()
|
||||
|
||||
@@ -59,7 +43,7 @@ func (m *Users) countByCondition(ctx context.Context, expr BoolExpression) (int6
|
||||
Cnt int64
|
||||
}
|
||||
|
||||
tbl := table.Users
|
||||
tbl := tblUsers
|
||||
stmt := SELECT(COUNT(tbl.ID).AS("cnt")).FROM(tbl).WHERE(expr)
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
@@ -73,10 +57,14 @@ func (m *Users) countByCondition(ctx context.Context, expr BoolExpression) (int6
|
||||
}
|
||||
|
||||
// List returns a paginated list of users
|
||||
func (m *Users) List(ctx context.Context, pagination *requests.Pagination, cond BoolExpression) (*requests.Pager, error) {
|
||||
func (m *Users) List(
|
||||
ctx context.Context,
|
||||
pagination *requests.Pagination,
|
||||
cond BoolExpression,
|
||||
) (*requests.Pager, error) {
|
||||
pagination.Format()
|
||||
|
||||
tbl := table.Users
|
||||
tbl := tblUsers
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(cond).
|
||||
@@ -106,14 +94,19 @@ func (m *Users) List(ctx context.Context, pagination *requests.Pagination, cond
|
||||
}
|
||||
|
||||
// PostList returns a paginated list of posts for a user
|
||||
func (m *Users) PostList(ctx context.Context, userId int64, pagination *requests.Pagination, conds ...Cond) (*requests.Pager, error) {
|
||||
func (m *Users) PostList(
|
||||
ctx context.Context,
|
||||
userId int64,
|
||||
pagination *requests.Pagination,
|
||||
conds ...Cond,
|
||||
) (*requests.Pager, error) {
|
||||
pagination.Format()
|
||||
|
||||
tblUserPosts := table.UserPosts
|
||||
tblUserPosts := tblUserPosts
|
||||
|
||||
cond := CondJoin(ExprCond(tblUserPosts.UserID.EQ(Int64(userId))), conds...)
|
||||
|
||||
tbl := table.Posts
|
||||
tbl := tblPosts
|
||||
stmt := SELECT(tbl.AllColumns).
|
||||
FROM(tbl.
|
||||
RIGHT_JOIN(
|
||||
@@ -163,7 +156,7 @@ func (m *Users) PostList(ctx context.Context, userId int64, pagination *requests
|
||||
|
||||
// GetUserIDByOpenID
|
||||
func (m *Users) GetUserByOpenID(ctx context.Context, openID string) (*Users, error) {
|
||||
tbl := table.Users
|
||||
tbl := tblUsers
|
||||
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
@@ -213,7 +206,7 @@ func (m *Users) GetUsersMapByIDs(ctx context.Context, ids []int64) (map[int64]Us
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
tbl := table.Users
|
||||
tbl := tblUsers
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(
|
||||
@@ -235,7 +228,7 @@ func (m *Users) GetUsersMapByIDs(ctx context.Context, ids []int64) (map[int64]Us
|
||||
}
|
||||
|
||||
func (m *Users) BatchCheckHasBought(ctx context.Context, postIDs []int64) (map[int64]bool, error) {
|
||||
tbl := table.UserPosts
|
||||
tbl := tblUserPosts
|
||||
stmt := tbl.SELECT(tbl.PostID.AS("post_id")).WHERE(
|
||||
tbl.UserID.EQ(Int64(m.ID)).AND(
|
||||
tbl.PostID.IN(lo.Map(postIDs, func(id int64, _ int) Expression { return Int64(id) })...),
|
||||
@@ -259,7 +252,7 @@ func (m *Users) BatchCheckHasBought(ctx context.Context, postIDs []int64) (map[i
|
||||
|
||||
// HasBought
|
||||
func (m *Users) HasBought(ctx context.Context, postID int64) (bool, error) {
|
||||
tbl := table.UserPosts
|
||||
tbl := tblUserPosts
|
||||
stmt := tbl.
|
||||
SELECT(tbl.ID).
|
||||
WHERE(
|
||||
@@ -284,7 +277,7 @@ func (m *Users) HasBought(ctx context.Context, postID int64) (bool, error) {
|
||||
|
||||
// SetUsername
|
||||
func (m *Users) SetUsername(ctx context.Context, username string) error {
|
||||
tbl := table.Users
|
||||
tbl := tblUsers
|
||||
stmt := tbl.
|
||||
UPDATE(tbl.Username).
|
||||
SET(String(username)).
|
||||
@@ -302,7 +295,7 @@ func (m *Users) SetUsername(ctx context.Context, username string) error {
|
||||
|
||||
// UpdateUserToken
|
||||
func (m *Users) UpdateUserToken(ctx context.Context, token fields.UserAuthToken) error {
|
||||
tbl := table.Users
|
||||
tbl := tblUsers
|
||||
stmt := tbl.
|
||||
UPDATE(tbl.AuthToken).
|
||||
SET(fields.ToJson(token)).
|
||||
@@ -320,7 +313,7 @@ func (m *Users) UpdateUserToken(ctx context.Context, token fields.UserAuthToken)
|
||||
|
||||
// BuyPosts
|
||||
func (m *Users) BuyPosts(ctx context.Context, postID, price int64) error {
|
||||
tbl := table.UserPosts
|
||||
tbl := tblUserPosts
|
||||
stmt := tbl.
|
||||
INSERT(tbl.MutableColumns).
|
||||
MODEL(&UserPosts{
|
||||
@@ -341,7 +334,7 @@ func (m *Users) BuyPosts(ctx context.Context, postID, price int64) error {
|
||||
}
|
||||
|
||||
func (m *Users) RevokePosts(ctx context.Context, postID int64) error {
|
||||
tbl := table.UserPosts
|
||||
tbl := tblUserPosts
|
||||
stmt := tbl.
|
||||
DELETE().
|
||||
WHERE(
|
||||
@@ -360,7 +353,7 @@ func (m *Users) RevokePosts(ctx context.Context, postID int64) error {
|
||||
|
||||
// SetBalance
|
||||
func (m *Users) SetBalance(ctx context.Context, balance int64) error {
|
||||
tbl := table.Users
|
||||
tbl := tblUsers
|
||||
stmt := tbl.
|
||||
UPDATE(tbl.Balance).
|
||||
SET(Int64(balance)).
|
||||
@@ -378,7 +371,7 @@ func (m *Users) SetBalance(ctx context.Context, balance int64) error {
|
||||
|
||||
// AddBalance adds the given amount to the user's balance
|
||||
func (m *Users) AddBalance(ctx context.Context, amount int64) error {
|
||||
tbl := table.Users
|
||||
tbl := tblUsers
|
||||
stmt := tbl.
|
||||
UPDATE(tbl.Balance).
|
||||
SET(tbl.Balance.ADD(Int64(amount))).
|
||||
|
||||
Reference in New Issue
Block a user