feat: update

This commit is contained in:
Rogee
2025-05-23 23:42:27 +08:00
parent 409a2e8304
commit 1166a5c949
17 changed files with 751 additions and 514 deletions

View File

@@ -5,7 +5,6 @@ import (
"time"
"quyun/app/requests"
"quyun/database/conds"
"quyun/database/fields"
"quyun/database/table"
@@ -13,31 +12,13 @@ import (
"github.com/go-jet/jet/v2/qrm"
"github.com/pkg/errors"
"github.com/samber/lo"
log "github.com/sirupsen/logrus"
)
func (m *Users) log() *log.Entry {
return log.WithField("model", "UsersModel")
}
// GetByID
func (m *Users) GetByID(ctx context.Context, id int64) (*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 Users
if err := stmt.QueryContext(ctx, db, &user); err != nil {
m.log().Errorf("error querying user by ID(%d), err: %v", id, err)
return nil, err
}
return &user, nil
var usersUpdateExcludeColumns = []Column{
table.Users.OpenID,
table.Users.Balance,
table.Users.CreatedAt,
table.Users.DeletedAt,
}
// BuildConditionWithKey builds the WHERE clause for user queries
@@ -109,79 +90,13 @@ func (m *Users) List(ctx context.Context, pagination *requests.Pagination, cond
}, nil
}
// Create creates a new user
func (m *Users) Create(ctx context.Context) (*Users, error) {
m.CreatedAt = time.Now()
m.UpdatedAt = time.Now()
tbl := table.Users
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(m).RETURNING(tbl.AllColumns)
m.log().Infof("sql: %s", stmt.DebugSql())
var createdUser Users
err := stmt.QueryContext(ctx, db, &createdUser)
if err != nil {
m.log().Errorf("error creating user: %v", err)
return nil, err
}
return &createdUser, nil
}
// Update updates an existing user
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(m).
WHERE(tbl.ID.EQ(Int64(m.ID))).
RETURNING(tbl.AllColumns)
m.log().Infof("sql: %s", stmt.DebugSql())
var user Users
if err := stmt.QueryContext(ctx, db, &user); err != nil {
m.log().Errorf("error updating user: %v", err)
return nil, err
}
return &user, nil
}
// DeleteByID soft deletes a user by ID
func (m *Users) DeleteByID(ctx context.Context, id int64) error {
tbl := table.Users
stmt := tbl.
UPDATE(tbl.DeletedAt).
SET(TimestampT(time.Now())).
WHERE(
tbl.ID.EQ(Int64(id)),
)
m.log().Infof("sql: %s", stmt.DebugSql())
if _, err := stmt.ExecContext(ctx, db); err != nil {
m.log().Errorf("error deleting user: %v", err)
return err
}
return nil
}
// PostList returns a paginated list of posts for a user
func (m *Users) PostList(ctx context.Context, userId int64, pagination *requests.Pagination, conds ...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
combineConds := tblUserPosts.UserID.EQ(Int64(userId))
for _, c := range conds {
combineConds = c(combineConds)
}
cond := CondJoin(ExprCond(tblUserPosts.UserID.EQ(Int64(userId))), conds...)
tbl := table.Posts
stmt := SELECT(tbl.AllColumns).
@@ -191,7 +106,7 @@ func (m *Users) PostList(ctx context.Context, userId int64, pagination *requests
tblUserPosts.PostID.EQ(tbl.ID),
),
).
WHERE(combineConds).
WHERE(CondTrue(cond...)).
ORDER_BY(tblUserPosts.ID.DESC()).
LIMIT(pagination.Limit).
OFFSET(pagination.Offset)
@@ -256,8 +171,7 @@ func (m *Users) GetUserByOpenIDOrCreate(ctx context.Context, openID string, user
user, err := m.GetUserByOpenID(ctx, openID)
if err != nil {
if errors.Is(err, qrm.ErrNoRows) {
user, err = userModel.Create(ctx)
if err != nil {
if err = userModel.Create(ctx); err != nil {
return nil, errors.Wrap(err, "failed to create user")
}
} else {
@@ -271,8 +185,7 @@ func (m *Users) GetUserByOpenIDOrCreate(ctx context.Context, openID string, user
user.Metas = userModel.Metas
user.AuthToken = userModel.AuthToken
user, err = user.Update(ctx)
if err != nil {
if err := user.Update(ctx); err != nil {
return nil, errors.Wrap(err, "failed to update user")
}
}
@@ -354,27 +267,6 @@ func (m *Users) HasBought(ctx context.Context, postID int64) (bool, error) {
return userPost.ID > 0, nil
}
// Count
func (m *Users) Count(ctx context.Context, cond BoolExpression) (int64, error) {
tbl := table.Users
stmt := tbl.
SELECT(COUNT(tbl.ID).AS("cnt")).
WHERE(cond)
m.log().Infof("sql: %s", stmt.DebugSql())
var cnt struct {
Cnt int64
}
if err := stmt.QueryContext(ctx, db, &cnt); err != nil {
m.log().Errorf("error counting users: %v", err)
return 0, err
}
return cnt.Cnt, nil
}
// SetUsername
func (m *Users) SetUsername(ctx context.Context, username string) error {
tbl := table.Users