The ASP.NET Core solution (mws.api/mws.application/mws.domain/ mws.infrastructure) existed only as untracked working files. Adding it to version control, plus a .gitignore for build output and local tooling directories, so the CQRS/mediator refactor plan has a real git history to branch and diff against.
45 lines
1.8 KiB
C#
45 lines
1.8 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using Mws.Application.Common.Repositories;
|
|
using Mws.Domain.Users;
|
|
|
|
namespace Mws.Infrastructure.Persistence.Repositories;
|
|
|
|
public class UserRepository(AppDbContext db) : RepositoryBase<User>(db), IUserRepository
|
|
{
|
|
public Task<User?> GetByIdAsync(Guid id, CancellationToken ct = default) =>
|
|
Set.FirstOrDefaultAsync(u => u.Id == id, ct);
|
|
|
|
public Task<User?> GetByIdWithRoleAsync(Guid id, CancellationToken ct = default) =>
|
|
Set.Include(u => u.Role).FirstOrDefaultAsync(u => u.Id == id, ct);
|
|
|
|
public Task<User?> GetByUsernameWithRoleAsync(string username, CancellationToken ct = default) =>
|
|
Set.Include(u => u.Role).SingleOrDefaultAsync(u => u.Username == username, ct);
|
|
|
|
public Task<bool> ExistsByUsernameAsync(string username, CancellationToken ct = default) =>
|
|
Set.AnyAsync(u => u.Username == username, ct);
|
|
|
|
public Task<bool> ExistsByRoleIdAsync(Guid roleId, CancellationToken ct = default) =>
|
|
Set.AnyAsync(u => u.RoleId == roleId, ct);
|
|
|
|
public Task<Guid> GetRoleIdAsync(Guid userId, CancellationToken ct = default) =>
|
|
Set.Where(u => u.Id == userId).Select(u => u.RoleId).SingleOrDefaultAsync(ct);
|
|
|
|
public Task<List<User>> SearchWithRoleAsync(string? term, int? take, CancellationToken ct = default)
|
|
{
|
|
var query = Set.Include(u => u.Role).AsQueryable();
|
|
if (!string.IsNullOrWhiteSpace(term))
|
|
{
|
|
var lower = term.Trim().ToLower();
|
|
query = query.Where(u => u.Username.ToLower().Contains(lower) || u.DisplayName.ToLower().Contains(lower));
|
|
}
|
|
|
|
query = query.OrderBy(u => u.DisplayName);
|
|
if (take is { } n)
|
|
{
|
|
query = query.Take(n);
|
|
}
|
|
|
|
return query.ToListAsync(ct);
|
|
}
|
|
}
|