ref: project structure
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using mws.backend.dotnet.application.Common;
|
||||
using mws.backend.dotnet.application.Common.Repositories;
|
||||
using mws.backend.dotnet.domain.Users;
|
||||
|
||||
namespace mws.backend.dotnet.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.UserRoles).ThenInclude(ur => ur.Role).FirstOrDefaultAsync(u => u.Id == id, ct);
|
||||
|
||||
public Task<User?> GetByUsernameWithRoleAsync(string username, CancellationToken ct = default) =>
|
||||
Set.Include(u => u.UserRoles).ThenInclude(ur => ur.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) =>
|
||||
Db.UserRoles.AnyAsync(ur => ur.RoleId == roleId, ct);
|
||||
|
||||
public Task<List<Guid>> GetRoleIdsAsync(Guid userId, CancellationToken ct = default) =>
|
||||
Db.UserRoles.Where(ur => ur.UserId == userId).Select(ur => ur.RoleId).ToListAsync(ct);
|
||||
|
||||
public Task<List<User>> SearchWithRoleAsync(string? term, int? take, CancellationToken ct = default)
|
||||
{
|
||||
var query = Set.Include(u => u.UserRoles).ThenInclude(ur => ur.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);
|
||||
}
|
||||
|
||||
public Task<PagedResult<User>> SearchWithRolePagedAsync(string? term, int page, int pageSize, CancellationToken ct = default)
|
||||
{
|
||||
var query = Set.Include(u => u.UserRoles).ThenInclude(ur => ur.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));
|
||||
}
|
||||
|
||||
return query.OrderBy(u => u.DisplayName).ToPagedResultAsync(page, pageSize, ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user