Перепиливание под чистый DDD
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Knot.Modules.Auth.Domain;
|
||||
|
||||
namespace Knot.Modules.Auth.Infrastructure.Persistence;
|
||||
|
||||
/// <summary>
|
||||
/// Реализация репозитория пользователей с использованием EF Core.
|
||||
/// </summary>
|
||||
public sealed class UserRepository : IUserRepository
|
||||
{
|
||||
private readonly AuthDbContext _context;
|
||||
|
||||
public UserRepository(AuthDbContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public async Task<User?> GetByIdAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _context.Users.FirstOrDefaultAsync(u => u.Id == id, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<List<User>> GetByIdsAsync(IEnumerable<Guid> ids, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _context.Users.Where(u => ids.Contains(u.Id)).ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<User?> GetByUsernameAsync(string username, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _context.Users.FirstOrDefaultAsync(u => u.Username == username, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<bool> IsUsernameUniqueAsync(string username, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return !await _context.Users.AnyAsync(u => u.Username == username, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<List<User>> SearchUsersAsync(string query, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return await _context.Users
|
||||
.Where(u => u.Username.ToLower().Contains(query.ToLower()) ||
|
||||
(u.DisplayName != null && u.DisplayName.ToLower().Contains(query.ToLower())))
|
||||
.Take(20)
|
||||
.ToListAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public void Add(User user)
|
||||
{
|
||||
_context.Users.Add(user);
|
||||
}
|
||||
|
||||
public void Update(User user)
|
||||
{
|
||||
_context.Users.Update(user);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user