Skip to content

Commit 3e6730c

Browse files
committed
Add GetCurrentTenantAsync method to TenantRepository
1 parent a128f00 commit 3e6730c

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

application/account-management/Core/Features/Tenants/Domain/TenantRepository.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
using Microsoft.EntityFrameworkCore;
22
using PlatformPlatform.AccountManagement.Database;
33
using PlatformPlatform.SharedKernel.Domain;
4+
using PlatformPlatform.SharedKernel.ExecutionContext;
45
using PlatformPlatform.SharedKernel.Persistence;
56

67
namespace PlatformPlatform.AccountManagement.Features.Tenants.Domain;
78

89
public interface ITenantRepository : ICrudRepository<Tenant, TenantId>
910
{
11+
Task<Tenant> GetCurrentTenantAsync(CancellationToken cancellationToken);
12+
1013
Task<bool> ExistsAsync(TenantId id, CancellationToken cancellationToken);
1114

1215
Task<bool> IsSubdomainFreeAsync(string subdomain, CancellationToken cancellationToken);
1316
}
1417

15-
internal sealed class TenantRepository(AccountManagementDbContext accountManagementDbContext)
18+
internal sealed class TenantRepository(AccountManagementDbContext accountManagementDbContext, IExecutionContext executionContext)
1619
: RepositoryBase<Tenant, TenantId>(accountManagementDbContext), ITenantRepository
1720
{
21+
public async Task<Tenant> GetCurrentTenantAsync(CancellationToken cancellationToken)
22+
{
23+
ArgumentNullException.ThrowIfNull(executionContext.TenantId!);
24+
return await GetByIdAsync(executionContext.TenantId, cancellationToken) ??
25+
throw new InvalidOperationException("Active tenant not found.");
26+
}
27+
1828
public Task<bool> IsSubdomainFreeAsync(string subdomain, CancellationToken cancellationToken)
1929
{
2030
return DbSet.AllAsync(tenant => tenant.Id != subdomain, cancellationToken);

0 commit comments

Comments
 (0)