feat: update models
This commit is contained in:
@@ -27,13 +27,12 @@ func (m *Users) GetByID(ctx context.Context, id int64) (*Users, error) {
|
||||
stmt := tbl.
|
||||
SELECT(tbl.AllColumns).
|
||||
WHERE(
|
||||
tbl.ID.EQ(Int64(id)),
|
||||
tbl.ID.EQ(Int64(m.ID)),
|
||||
)
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
var user Users
|
||||
err := stmt.QueryContext(ctx, db, &user)
|
||||
if err != nil {
|
||||
if err := stmt.QueryContext(ctx, db, &user); err != nil {
|
||||
m.log().Errorf("error querying user by ID: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
@@ -41,25 +40,6 @@ func (m *Users) GetByID(ctx context.Context, id int64) (*Users, error) {
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (m *Users) Own(ctx context.Context, userID, postID int64) error {
|
||||
tbl := table.UserPosts
|
||||
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(&UserPosts{
|
||||
UserID: userID,
|
||||
PostID: postID,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
})
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||
m.log().Errorf("error inserting user post: %v", err)
|
||||
return err
|
||||
}
|
||||
m.log().Infof("user post inserted successfully: userID=%d, postID=%d", userID, postID)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// BuildConditionWithKey builds the WHERE clause for user queries
|
||||
func (m *Users) BuildConditionWithKey(key *string) BoolExpression {
|
||||
tbl := table.Users
|
||||
@@ -148,29 +128,30 @@ func (m *Users) Create(ctx context.Context, userModel *Users) (*Users, error) {
|
||||
}
|
||||
|
||||
// Update updates an existing user
|
||||
func (m *Users) Update(ctx context.Context, id int64, userModel *Users) (*Users, error) {
|
||||
userModel.UpdatedAt = time.Now()
|
||||
func (m *Users) Update(ctx context.Context) (*Users, error) {
|
||||
m.UpdatedAt = time.Now()
|
||||
|
||||
tbl := table.Users
|
||||
stmt := tbl.
|
||||
UPDATE(
|
||||
tbl.MutableColumns.Except(
|
||||
tbl.OpenID,
|
||||
tbl.Balance,
|
||||
tbl.CreatedAt,
|
||||
tbl.DeletedAt,
|
||||
),
|
||||
).
|
||||
MODEL(userModel).
|
||||
WHERE(tbl.ID.EQ(Int64(id))).
|
||||
MODEL(m).
|
||||
WHERE(tbl.ID.EQ(Int64(m.ID))).
|
||||
RETURNING(tbl.AllColumns)
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
var updatedUser Users
|
||||
if err := stmt.QueryContext(ctx, db, &updatedUser); err != nil {
|
||||
var user Users
|
||||
if err := stmt.QueryContext(ctx, db, &user); err != nil {
|
||||
m.log().Errorf("error updating user: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
return &updatedUser, nil
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
// DeleteByID soft deletes a user by ID
|
||||
@@ -283,12 +264,14 @@ func (m *Users) GetUserByOpenIDOrCreate(ctx context.Context, openID string, user
|
||||
return nil, errors.Wrap(err, "failed to get user")
|
||||
}
|
||||
} else {
|
||||
userModel.OpenID = user.OpenID
|
||||
if !(user.Username == "" || user.Username == "-" || user.Username == "暂未设置昵称") {
|
||||
userModel.Username = user.Username
|
||||
userModel.Avatar = user.Avatar
|
||||
if user.Username == "" || user.Username == "-" || user.Username == "暂未设置昵称" {
|
||||
user.Username = userModel.Username
|
||||
user.Avatar = userModel.Avatar
|
||||
}
|
||||
user, err = m.Update(ctx, user.ID, userModel)
|
||||
user.Metas = userModel.Metas
|
||||
user.AuthToken = userModel.AuthToken
|
||||
|
||||
user, err = user.Update(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to update user")
|
||||
}
|
||||
@@ -323,10 +306,10 @@ func (m *Users) GetUsersMapByIDs(ctx context.Context, ids []int64) (map[int64]Us
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (m *Users) BatchCheckHasBought(ctx context.Context, userID int64, postIDs []int64) (map[int64]bool, error) {
|
||||
func (m *Users) BatchCheckHasBought(ctx context.Context, postIDs []int64) (map[int64]bool, error) {
|
||||
tbl := table.UserPosts
|
||||
stmt := tbl.SELECT(tbl.PostID.AS("post_id")).WHERE(
|
||||
tbl.UserID.EQ(Int64(userID)).AND(
|
||||
tbl.UserID.EQ(Int64(m.ID)).AND(
|
||||
tbl.PostID.IN(lo.Map(postIDs, func(id int64, _ int) Expression { return Int64(id) })...),
|
||||
),
|
||||
)
|
||||
@@ -347,12 +330,12 @@ func (m *Users) BatchCheckHasBought(ctx context.Context, userID int64, postIDs [
|
||||
}
|
||||
|
||||
// HasBought
|
||||
func (m *Users) HasBought(ctx context.Context, userID, postID int64) (bool, error) {
|
||||
func (m *Users) HasBought(ctx context.Context, postID int64) (bool, error) {
|
||||
tbl := table.UserPosts
|
||||
stmt := tbl.
|
||||
SELECT(tbl.ID).
|
||||
WHERE(
|
||||
tbl.UserID.EQ(Int64(userID)).AND(
|
||||
tbl.UserID.EQ(Int64(m.ID)).AND(
|
||||
tbl.PostID.EQ(Int64(postID)),
|
||||
),
|
||||
)
|
||||
@@ -392,14 +375,14 @@ func (m *Users) Count(ctx context.Context, cond BoolExpression) (int64, error) {
|
||||
return cnt.Cnt, nil
|
||||
}
|
||||
|
||||
// UpdateUsername
|
||||
func (m *Users) UpdateUsername(ctx context.Context, id int64, username string) error {
|
||||
// SetUsername
|
||||
func (m *Users) SetUsername(ctx context.Context, username string) error {
|
||||
tbl := table.Users
|
||||
stmt := tbl.
|
||||
UPDATE(tbl.Username).
|
||||
SET(String(username)).
|
||||
WHERE(
|
||||
tbl.ID.EQ(Int64(id)),
|
||||
tbl.ID.EQ(Int64(m.ID)),
|
||||
)
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
@@ -411,13 +394,13 @@ func (m *Users) UpdateUsername(ctx context.Context, id int64, username string) e
|
||||
}
|
||||
|
||||
// UpdateUserToken
|
||||
func (m *Users) UpdateUserToken(ctx context.Context, id int64, token fields.UserAuthToken) error {
|
||||
func (m *Users) UpdateUserToken(ctx context.Context, token fields.UserAuthToken) error {
|
||||
tbl := table.Users
|
||||
stmt := tbl.
|
||||
UPDATE(tbl.AuthToken).
|
||||
SET(fields.ToJson(token)).
|
||||
WHERE(
|
||||
tbl.ID.EQ(Int64(id)),
|
||||
tbl.ID.EQ(Int64(m.ID)),
|
||||
)
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
@@ -429,12 +412,12 @@ func (m *Users) UpdateUserToken(ctx context.Context, id int64, token fields.User
|
||||
}
|
||||
|
||||
// BuyPosts
|
||||
func (m *Users) BuyPosts(ctx context.Context, userID, postID, price int64) error {
|
||||
func (m *Users) BuyPosts(ctx context.Context, postID, price int64) error {
|
||||
tbl := table.UserPosts
|
||||
stmt := tbl.
|
||||
INSERT(tbl.MutableColumns).
|
||||
MODEL(&UserPosts{
|
||||
UserID: userID,
|
||||
UserID: m.ID,
|
||||
PostID: postID,
|
||||
Price: price,
|
||||
CreatedAt: time.Now(),
|
||||
@@ -450,12 +433,12 @@ func (m *Users) BuyPosts(ctx context.Context, userID, postID, price int64) error
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Users) RevokePosts(ctx context.Context, userID, postID int64) error {
|
||||
func (m *Users) RevokePosts(ctx context.Context, postID int64) error {
|
||||
tbl := table.UserPosts
|
||||
stmt := tbl.
|
||||
DELETE().
|
||||
WHERE(
|
||||
tbl.UserID.EQ(Int64(userID)).AND(
|
||||
tbl.UserID.EQ(Int64(m.ID)).AND(
|
||||
tbl.PostID.EQ(Int64(postID)),
|
||||
),
|
||||
)
|
||||
@@ -469,13 +452,13 @@ func (m *Users) RevokePosts(ctx context.Context, userID, postID int64) error {
|
||||
}
|
||||
|
||||
// SetBalance
|
||||
func (m *Users) SetBalance(ctx context.Context, id, balance int64) error {
|
||||
func (m *Users) SetBalance(ctx context.Context, balance int64) error {
|
||||
tbl := table.Users
|
||||
stmt := tbl.
|
||||
UPDATE(tbl.Balance).
|
||||
SET(Int64(balance)).
|
||||
WHERE(
|
||||
tbl.ID.EQ(Int64(id)),
|
||||
tbl.ID.EQ(Int64(m.ID)),
|
||||
)
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
@@ -487,13 +470,13 @@ func (m *Users) SetBalance(ctx context.Context, id, balance int64) error {
|
||||
}
|
||||
|
||||
// AddBalance adds the given amount to the user's balance
|
||||
func (m *Users) AddBalance(ctx context.Context, id, amount int64) error {
|
||||
func (m *Users) AddBalance(ctx context.Context, amount int64) error {
|
||||
tbl := table.Users
|
||||
stmt := tbl.
|
||||
UPDATE(tbl.Balance).
|
||||
SET(tbl.Balance.ADD(Int64(amount))).
|
||||
WHERE(
|
||||
tbl.ID.EQ(Int64(id)),
|
||||
tbl.ID.EQ(Int64(m.ID)),
|
||||
)
|
||||
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user