mengstack-api/internal/modules/auth/infrastructure/user_repo.go
MengStack Dev fbf9af32f0 Initial commit: MengStack Go API framework
Clean public release with:
- Go/Gin/PostgreSQL/Redis architecture
- Multi-tenant support
- JWT authentication
- Docker Compose deployment
- Environment-based configuration
2026-10-03 00:14:40 +08:00

53 lines
1.3 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
}