Files
quyun/backend/app/models/users.go
2025-04-10 21:04:35 +08:00

200 lines
4.5 KiB
Go

package models
import (
"context"
"time"
"quyun/app/requests"
"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"
)
// @provider
type usersModel struct {
log *logrus.Entry `inject:"false"`
}
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
}
// 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
}