38 lines
1.4 KiB
C#
38 lines
1.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using TakeoutSaaS.Domain.Identity.Entities;
|
|
using TakeoutSaaS.Domain.Identity.Repositories;
|
|
|
|
namespace TakeoutSaaS.Infrastructure.Identity.Persistence;
|
|
|
|
/// <summary>
|
|
/// EF Core 后台用户仓储实现。
|
|
/// </summary>
|
|
public sealed class EfIdentityUserRepository(IdentityDbContext dbContext) : IIdentityUserRepository
|
|
{
|
|
|
|
public Task<IdentityUser?> FindByAccountAsync(string account, CancellationToken cancellationToken = default)
|
|
=> dbContext.IdentityUsers.AsNoTracking().FirstOrDefaultAsync(x => x.Account == account, cancellationToken);
|
|
|
|
public Task<IdentityUser?> FindByIdAsync(long userId, CancellationToken cancellationToken = default)
|
|
=> dbContext.IdentityUsers.AsNoTracking().FirstOrDefaultAsync(x => x.Id == userId, cancellationToken);
|
|
|
|
public async Task<IReadOnlyList<IdentityUser>> SearchAsync(long tenantId, string? keyword, CancellationToken cancellationToken = default)
|
|
{
|
|
var query = dbContext.IdentityUsers
|
|
.AsNoTracking()
|
|
.Where(x => x.TenantId == tenantId);
|
|
|
|
if (!string.IsNullOrWhiteSpace(keyword))
|
|
{
|
|
var normalized = keyword.Trim();
|
|
query = query.Where(x => x.Account.Contains(normalized) || x.DisplayName.Contains(normalized));
|
|
}
|
|
|
|
return await query.ToListAsync(cancellationToken);
|
|
}
|
|
}
|