feat: add users page

This commit is contained in:
yanghao05
2025-04-10 21:04:35 +08:00
parent ed27cb3534
commit 5a63eee1ce
22 changed files with 797 additions and 60 deletions

View File

@@ -2,7 +2,9 @@ package models
import (
"context"
"time"
"quyun/app/requests"
"quyun/database/schemas/public/model"
"quyun/database/schemas/public/table"
@@ -75,3 +77,123 @@ func (m *usersModel) Posts(ctx context.Context, userID int64) ([]*model.Posts, e
return posts, nil
}
// BuildConditionWithKey builds the WHERE clause for user queries
func (m *usersModel) BuildConditionWithKey(key *string) BoolExpression {
tbl := table.Users
cond := tbl.DeletedAt.IS_NULL()
if key == nil || *key == "" {
return cond
}
cond = cond.AND(
tbl.Username.LIKE(String("%" + *key + "%")),
)
return cond
}
// countByCondition counts users matching the given condition
func (m *usersModel) countByCondition(ctx context.Context, expr BoolExpression) (int64, error) {
var cnt struct {
Cnt int64
}
tbl := table.Users
stmt := SELECT(COUNT(tbl.ID).AS("cnt")).FROM(tbl).WHERE(expr)
m.log.Infof("sql: %s", stmt.DebugSql())
err := stmt.QueryContext(ctx, db, &cnt)
if err != nil {
m.log.Errorf("error counting users: %v", err)
return 0, err
}
return cnt.Cnt, nil
}
// List returns a paginated list of users
func (m *usersModel) List(ctx context.Context, pagination *requests.Pagination, cond BoolExpression) (*requests.Pager, error) {
pagination.Format()
tbl := table.Users
stmt := tbl.
SELECT(tbl.AllColumns).
WHERE(cond).
ORDER_BY(tbl.ID.DESC()).
LIMIT(pagination.Limit).
OFFSET(pagination.Offset)
m.log.Infof("sql: %s", stmt.DebugSql())
var users []model.Users = make([]model.Users, 0)
err := stmt.QueryContext(ctx, db, &users)
if err != nil {
m.log.Errorf("error querying users: %v", err)
return nil, err
}
count, err := m.countByCondition(ctx, cond)
if err != nil {
m.log.Errorf("error getting user count: %v", err)
return nil, err
}
return &requests.Pager{
Items: users,
Total: count,
Pagination: *pagination,
}, nil
}
// Create creates a new user
func (m *usersModel) Create(ctx context.Context, model *model.Users) error {
model.CreatedAt = time.Now()
model.UpdatedAt = time.Now()
tbl := table.Users
stmt := tbl.INSERT(tbl.MutableColumns).MODEL(model)
m.log.Infof("sql: %s", stmt.DebugSql())
_, err := stmt.ExecContext(ctx, db)
if err != nil {
m.log.Errorf("error creating user: %v", err)
return err
}
return nil
}
// Update updates an existing user
func (m *usersModel) Update(ctx context.Context, id int64, model *model.Users) error {
model.UpdatedAt = time.Now()
tbl := table.Users
stmt := tbl.UPDATE(tbl.MutableColumns.Except(tbl.CreatedAt, tbl.DeletedAt)).MODEL(model).WHERE(tbl.ID.EQ(Int64(id)))
m.log.Infof("sql: %s", stmt.DebugSql())
_, err := stmt.ExecContext(ctx, db)
if err != nil {
m.log.Errorf("error updating user: %v", err)
return err
}
return nil
}
// DeleteByID soft deletes a user by ID
func (m *usersModel) 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
}