Some checks failed
CI / Build & Test (push) Failing after 1m31s
- Add user management endpoints (list/create/get/update/delete) with pagination and search - Add dashboard stats endpoint with tenant/user/online counts and growth metrics - Add tenant resolver middleware for multi-tenant request scoping - Add i18n kernel with zh/en message files and AcceptLanguage middleware - Add WebSocket hub/handler for real-time communication - Add job scheduler kernel with cron support - Add plugin sandbox for isolated execution - Add storage kernel (local filesystem) - Add event bus kernel for pub/sub - Add cache kernel abstraction - Add database migration runner and version upgrade checker - Add rate limiting middleware with Redis backend - Add SQL migrations for rbac, audit_logs, settings, notifications, examples - Extend user repository with list/delete/count operations - Register all module routes with tenant resolver
111 lines
3.1 KiB
Go
111 lines
3.1 KiB
Go
package infrastructure
|
|
|
|
import (
|
|
"context"
|
|
|
|
"mengstack/internal/modules/auth/domain"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type userRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewUserRepository(db *gorm.DB) domain.UserRepository {
|
|
return &userRepo{db: db}
|
|
}
|
|
|
|
func (r *userRepo) Create(ctx context.Context, user *domain.User) error {
|
|
return r.db.WithContext(ctx).Create(user).Error
|
|
}
|
|
|
|
func (r *userRepo) FindByID(ctx context.Context, tenantID string, id uint) (*domain.User, error) {
|
|
var user domain.User
|
|
err := r.db.WithContext(ctx).Where("id = ? AND tenant_id = ?", id, tenantID).First(&user).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (r *userRepo) FindByEmail(ctx context.Context, tenantID string, email string) (*domain.User, error) {
|
|
var user domain.User
|
|
err := r.db.WithContext(ctx).Where("email = ? AND tenant_id = ?", email, tenantID).First(&user).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (r *userRepo) FindByUsername(ctx context.Context, tenantID string, username string) (*domain.User, error) {
|
|
var user domain.User
|
|
err := r.db.WithContext(ctx).Where("username = ? AND tenant_id = ?", username, tenantID).First(&user).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &user, nil
|
|
}
|
|
|
|
func (r *userRepo) Update(ctx context.Context, user *domain.User) error {
|
|
return r.db.WithContext(ctx).Save(user).Error
|
|
}
|
|
|
|
func (r *userRepo) List(ctx context.Context, tenantID string, page, pageSize int, search string, status *int) ([]domain.User, int64, error) {
|
|
var users []domain.User
|
|
var total int64
|
|
|
|
q := r.db.WithContext(ctx).Model(&domain.User{}).Where("tenant_id = ?", tenantID)
|
|
|
|
if search != "" {
|
|
like := "%" + search + "%"
|
|
q = q.Where("username LIKE ? OR email LIKE ? OR nickname LIKE ?", like, like, like)
|
|
}
|
|
if status != nil {
|
|
q = q.Where("status = ?", *status)
|
|
}
|
|
|
|
if err := q.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
offset := (page - 1) * pageSize
|
|
if err := q.Order("id DESC").Offset(offset).Limit(pageSize).Find(&users).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return users, total, nil
|
|
}
|
|
|
|
func (r *userRepo) Delete(ctx context.Context, tenantID string, id uint) error {
|
|
return r.db.WithContext(ctx).Where("id = ? AND tenant_id = ?", id, tenantID).Delete(&domain.User{}).Error
|
|
}
|
|
|
|
func (r *userRepo) Count(ctx context.Context, tenantID string) (int64, error) {
|
|
var count int64
|
|
err := r.db.WithContext(ctx).Model(&domain.User{}).Where("tenant_id = ?", tenantID).Count(&count).Error
|
|
return count, err
|
|
}
|
|
|
|
func (r *userRepo) CountAll(ctx context.Context) (int64, error) {
|
|
var count int64
|
|
err := r.db.WithContext(ctx).Model(&domain.User{}).Count(&count).Error
|
|
return count, err
|
|
}
|
|
|
|
func (r *userRepo) ListAll(ctx context.Context, page, pageSize int) ([]domain.User, int64, error) {
|
|
var users []domain.User
|
|
var total int64
|
|
|
|
if err := r.db.WithContext(ctx).Model(&domain.User{}).Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
offset := (page - 1) * pageSize
|
|
if err := r.db.WithContext(ctx).Order("id DESC").Offset(offset).Limit(pageSize).Find(&users).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return users, total, nil
|
|
}
|