Skip to content

Commit 0019624

Browse files
authored
Merge pull request #12 from suxrobGM/repository-refactorings
Repository refactorings
2 parents 12fa81c + b19f581 commit 0019624

File tree

127 files changed

+1150
-1442
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

127 files changed

+1150
-1442
lines changed

src/Core/Logistics.Application.Admin/Commands/CreateTenant/CreateTenantHandler.cs

+12-10
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,30 @@ namespace Logistics.Application.Admin.Commands;
99
internal sealed class CreateTenantHandler : RequestHandler<CreateTenantCommand, ResponseResult>
1010
{
1111
private readonly ITenantDatabaseService _tenantDatabase;
12-
private readonly IMasterRepository _repository;
12+
private readonly IMasterUnityOfWork _masterUow;
1313

1414
public CreateTenantHandler(
1515
ITenantDatabaseService tenantDatabase,
16-
IMasterRepository repository)
16+
IMasterUnityOfWork masterUow)
1717
{
1818
_tenantDatabase = tenantDatabase;
19-
_repository = repository;
19+
_masterUow = masterUow;
2020
}
2121

2222
protected override async Task<ResponseResult> HandleValidated(CreateTenantCommand req, CancellationToken cancellationToken)
2323
{
24+
var tenantName = req.Name.Trim().ToLower();
2425
var tenant = new Tenant
2526
{
26-
Name = req.Name.Trim().ToLower(),
27+
Name = tenantName,
2728
CompanyName = req.CompanyName,
28-
CompanyAddress = req.CompanyAddress
29+
CompanyAddress = req.CompanyAddress,
30+
ConnectionString = _tenantDatabase.GenerateConnectionString(tenantName)
2931
};
30-
tenant.ConnectionString = _tenantDatabase.GenerateConnectionString(tenant.Name);
3132

32-
var existingTenant = await _repository.GetAsync<Tenant>(i => i.Name == tenant.Name);
33-
if (existingTenant != null)
33+
var existingTenant = await _masterUow.Repository<Tenant>().GetAsync(i => i.Name == tenant.Name);
34+
35+
if (existingTenant is not null)
3436
{
3537
return ResponseResult.CreateError($"Tenant name '{tenant.Name}' is already taken, please chose another name");
3638
}
@@ -41,8 +43,8 @@ protected override async Task<ResponseResult> HandleValidated(CreateTenantComman
4143
return ResponseResult.CreateError("Could not create the tenant's database");
4244
}
4345

44-
await _repository.AddAsync(tenant);
45-
await _repository.UnitOfWork.CommitAsync();
46+
await _masterUow.Repository<Tenant>().AddAsync(tenant);
47+
await _masterUow.SaveChangesAsync();
4648
return ResponseResult.CreateSuccess();
4749
}
4850
}

src/Core/Logistics.Application.Admin/Commands/DeleteTenant/DeleteTenantCommand.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ namespace Logistics.Application.Admin.Commands;
55

66
public class DeleteTenantCommand : IRequest<ResponseResult>
77
{
8-
public string? Id { get; set; }
8+
public string Id { get; set; } = default!;
99
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Logistics.Application.Core;
2+
using Logistics.Domain.Entities;
23
using Logistics.Domain.Persistence;
34
using Logistics.Domain.Services;
45
using Logistics.Shared;
@@ -8,30 +9,34 @@ namespace Logistics.Application.Admin.Commands;
89
internal sealed class DeleteTenantHandler : RequestHandler<DeleteTenantCommand, ResponseResult>
910
{
1011
private readonly ITenantDatabaseService _tenantDatabase;
11-
private readonly IMasterRepository _masterRepository;
12+
private readonly IMasterUnityOfWork _masterRepository;
1213

1314
public DeleteTenantHandler(
1415
ITenantDatabaseService tenantDatabase,
15-
IMasterRepository masterRepository)
16+
IMasterUnityOfWork masterRepository)
1617
{
1718
_tenantDatabase = tenantDatabase;
1819
_masterRepository = masterRepository;
1920
}
2021

2122
protected override async Task<ResponseResult> HandleValidated(DeleteTenantCommand req, CancellationToken cancellationToken)
2223
{
23-
var tenant = await _masterRepository.GetAsync<Domain.Entities.Tenant>(req.Id!);
24+
var tenant = await _masterRepository.Repository<Tenant>().GetByIdAsync(req.Id);
2425

25-
if (tenant == null)
26-
return ResponseResult.CreateError("Could not find the tenant");
26+
if (tenant is null)
27+
{
28+
return ResponseResult.CreateError($"Could not find a tenant with ID '{req.Id}'");
29+
}
2730

2831
var isDeleted = await _tenantDatabase.DeleteDatabaseAsync(tenant.ConnectionString!);
2932

3033
if (!isDeleted)
34+
{
3135
return ResponseResult.CreateError("Could not delete the tenant's database");
36+
}
3237

33-
_masterRepository.Delete(tenant);
34-
await _masterRepository.UnitOfWork.CommitAsync();
38+
_masterRepository.Repository<Tenant>().Delete(tenant);
39+
await _masterRepository.SaveChangesAsync();
3540
return ResponseResult.CreateSuccess();
3641
}
3742
}

src/Core/Logistics.Application.Admin/Commands/DeleteTenant/DeleteTenantValidator.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ internal sealed class DeleteTenantValidator : AbstractValidator<DeleteTenantComm
66
{
77
public DeleteTenantValidator()
88
{
9-
RuleFor(i => i.Id)
10-
.NotEmpty();
9+
RuleFor(i => i.Id).NotEmpty();
1110
}
12-
}
11+
}

src/Core/Logistics.Application.Admin/Commands/UpdateTenant/UpdateTenantHandler.cs

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
11
using Logistics.Application.Core;
2+
using Logistics.Domain.Entities;
23
using Logistics.Domain.Persistence;
34
using Logistics.Shared;
45

56
namespace Logistics.Application.Admin.Commands;
67

78
internal sealed class UpdateTenantHandler : RequestHandler<UpdateTenantCommand, ResponseResult>
89
{
9-
private readonly IMasterRepository _repository;
10+
private readonly IMasterUnityOfWork _masterUow;
1011

11-
public UpdateTenantHandler(IMasterRepository repository)
12+
public UpdateTenantHandler(IMasterUnityOfWork masterUow)
1213
{
13-
_repository = repository;
14+
_masterUow = masterUow;
1415
}
1516

1617
protected override async Task<ResponseResult> HandleValidated(UpdateTenantCommand req, CancellationToken cancellationToken)
1718
{
18-
var tenant = await _repository.GetAsync<Domain.Entities.Tenant>(req.Id);
19+
var tenant = await _masterUow.Repository<Tenant>().GetByIdAsync(req.Id);
1920

2021
if (tenant is null)
22+
{
2123
return ResponseResult.CreateError($"Could not find a tenant with ID '{req.Id}'");
24+
}
2225

2326
if (!string.IsNullOrEmpty(req.Name) && tenant.Name != req.Name)
2427
{
@@ -37,8 +40,8 @@ protected override async Task<ResponseResult> HandleValidated(UpdateTenantComman
3740
tenant.ConnectionString = req.ConnectionString;
3841
}
3942

40-
_repository.Update(tenant);
41-
await _repository.UnitOfWork.CommitAsync();
43+
_masterUow.Repository<Tenant>().Update(tenant);
44+
await _masterUow.SaveChangesAsync();
4245
return ResponseResult.CreateSuccess();
4346
}
4447
}

src/Core/Logistics.Application.Admin/Commands/UpdateUser/UpdateUserCommand.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Logistics.Application.Admin.Commands;
66

77
public class UpdateUserCommand : IRequest<ResponseResult>
88
{
9-
public string? Id { get; set; }
9+
public string Id { get; set; } = default!;
1010
public string? FirstName { get; set; }
1111
public string? LastName { get; set; }
1212
public string? PhoneNumber { get; set; }

src/Core/Logistics.Application.Admin/Commands/UpdateUser/UpdateUserHandler.cs

+24-14
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,41 @@ namespace Logistics.Application.Admin.Commands;
77

88
internal sealed class UpdateUserHandler : RequestHandler<UpdateUserCommand, ResponseResult>
99
{
10-
private readonly IMasterRepository _masterRepository;
11-
private readonly ITenantRepository _tenantRepository;
10+
private readonly IMasterUnityOfWork _masterUow;
11+
private readonly ITenantUnityOfWork _tenantUow;
1212

1313
public UpdateUserHandler(
14-
IMasterRepository masterRepository,
15-
ITenantRepository tenantRepository)
14+
IMasterUnityOfWork masterUow,
15+
ITenantUnityOfWork tenantUow)
1616
{
17-
_masterRepository = masterRepository;
18-
_tenantRepository = tenantRepository;
17+
_masterUow = masterUow;
18+
_tenantUow = tenantUow;
1919
}
2020

2121
protected override async Task<ResponseResult> HandleValidated(
2222
UpdateUserCommand req, CancellationToken cancellationToken)
2323
{
24-
var user = await _masterRepository.GetAsync<User>(req.Id);
24+
var user = await _masterUow.Repository<User>().GetByIdAsync(req.Id);
2525

26-
if (user == null)
26+
if (user is null)
27+
{
2728
return ResponseResult.CreateError("Could not find the specified user");
29+
}
2830

2931
if (!string.IsNullOrEmpty(req.FirstName))
32+
{
3033
user.FirstName = req.FirstName;
34+
}
3135

3236
if (!string.IsNullOrEmpty(req.LastName))
37+
{
3338
user.LastName = req.LastName;
39+
}
3440

3541
if (!string.IsNullOrEmpty(req.PhoneNumber))
42+
{
3643
user.PhoneNumber = req.PhoneNumber;
44+
}
3745

3846
var tenantIds = user.GetJoinedTenantIds();
3947

@@ -42,24 +50,26 @@ protected override async Task<ResponseResult> HandleValidated(
4250
await UpdateTenantEmployeeDataAsync(tenantId, user);
4351
}
4452

45-
_masterRepository.Update(user);
46-
await _masterRepository.UnitOfWork.CommitAsync();
53+
_masterUow.Repository<User>().Update(user);
54+
await _masterUow.SaveChangesAsync();
55+
await _tenantUow.SaveChangesAsync();
4756
return ResponseResult.CreateSuccess();
4857
}
4958

5059
private async Task UpdateTenantEmployeeDataAsync(string tenantId, User user)
5160
{
52-
_tenantRepository.SetCurrentTenantById(tenantId);
53-
var employee = await _tenantRepository.GetAsync<Employee>(user.Id);
61+
_tenantUow.SetCurrentTenantById(tenantId);
62+
var employee = await _tenantUow.Repository<Employee>().GetByIdAsync(user.Id);
5463

5564
if (employee is null)
65+
{
5666
return;
67+
}
5768

5869
employee.FirstName = user.FirstName;
5970
employee.LastName = user.LastName;
6071
employee.Email = user.Email;
6172
employee.PhoneNumber = user.PhoneNumber;
62-
_tenantRepository.Update(employee);
63-
await _tenantRepository.UnitOfWork.CommitAsync();
73+
_tenantUow.Repository<Employee>().Update(employee);
6474
}
6575
}

src/Core/Logistics.Application.Admin/Queries/GetAppRoles/GetAppRolesHandler.cs

+8-10
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,20 @@ namespace Logistics.Application.Admin.Queries;
99

1010
internal sealed class GetAppRolesHandler : RequestHandler<GetAppRolesQuery, PagedResponseResult<AppRoleDto>>
1111
{
12-
private readonly IMasterRepository _repository;
12+
private readonly IMasterUnityOfWork _masterUow;
1313

14-
public GetAppRolesHandler(IMasterRepository repository)
14+
public GetAppRolesHandler(IMasterUnityOfWork masterUow)
1515
{
16-
_repository = repository;
16+
_masterUow = masterUow;
1717
}
1818

19-
protected override Task<PagedResponseResult<AppRoleDto>> HandleValidated(
19+
protected override async Task<PagedResponseResult<AppRoleDto>> HandleValidated(
2020
GetAppRolesQuery req, CancellationToken cancellationToken)
2121
{
22-
var totalItems = _repository.Query<AppRole>().Count();
22+
var totalItems = await _masterUow.Repository<AppRole>().CountAsync();
2323

24-
var rolesDto = _repository
25-
.ApplySpecification(new SearchAppRoles(req.Search))
26-
.Skip((req.Page - 1) * req.PageSize)
27-
.Take(req.PageSize)
24+
var rolesDto = _masterUow.Repository<AppRole>()
25+
.ApplySpecification(new SearchAppRoles(req.Search, req.Page, req.PageSize))
2826
.Select(i => new AppRoleDto()
2927
{
3028
Name = i.Name,
@@ -33,6 +31,6 @@ protected override Task<PagedResponseResult<AppRoleDto>> HandleValidated(
3331
.ToArray();
3432

3533
var totalPages = (int)Math.Ceiling(totalItems / (double)req.PageSize);
36-
return Task.FromResult(new PagedResponseResult<AppRoleDto>(rolesDto, totalItems, totalPages));
34+
return PagedResponseResult<AppRoleDto>.Create(rolesDto, totalItems, totalPages);
3735
}
3836
}

src/Core/Logistics.Application.Admin/Queries/GetTenant/GetTenantHandler.cs

+6-4
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,22 @@ namespace Logistics.Application.Admin.Queries;
88

99
internal sealed class GetTenantHandler : RequestHandler<GetTenantQuery, ResponseResult<TenantDto>>
1010
{
11-
private readonly IMasterRepository _repository;
11+
private readonly IMasterUnityOfWork _masterUow;
1212

13-
public GetTenantHandler(IMasterRepository repository)
13+
public GetTenantHandler(IMasterUnityOfWork masterUow)
1414
{
15-
_repository = repository;
15+
_masterUow = masterUow;
1616
}
1717

1818
protected override async Task<ResponseResult<TenantDto>> HandleValidated(
1919
GetTenantQuery req, CancellationToken cancellationToken)
2020
{
21-
var tenantEntity = await _repository.GetAsync<Tenant>(i => i.Id == req.Id || i.Name == req.Name);
21+
var tenantEntity = await _masterUow.Repository<Tenant>().GetAsync(i => i.Id == req.Id || i.Name == req.Name);
2222

2323
if (tenantEntity is null)
24+
{
2425
return ResponseResult<TenantDto>.CreateError("Could not find the specified tenant");
26+
}
2527

2628
var tenantDto = tenantEntity.ToDto(req.IncludeConnectionString);
2729
return ResponseResult<TenantDto>.CreateSuccess(tenantDto);

src/Core/Logistics.Application.Admin/Queries/GetTenants/GetTenantsHandler.cs

+8-10
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,24 @@ namespace Logistics.Application.Admin.Queries;
99

1010
internal sealed class GetTenantsHandler : RequestHandler<GetTenantsQuery, PagedResponseResult<TenantDto>>
1111
{
12-
private readonly IMasterRepository _repository;
12+
private readonly IMasterUnityOfWork _masterUow;
1313

14-
public GetTenantsHandler(IMasterRepository repository)
14+
public GetTenantsHandler(IMasterUnityOfWork masterUow)
1515
{
16-
_repository = repository;
16+
_masterUow = masterUow;
1717
}
1818

19-
protected override Task<PagedResponseResult<TenantDto>> HandleValidated(GetTenantsQuery req, CancellationToken cancellationToken)
19+
protected override async Task<PagedResponseResult<TenantDto>> HandleValidated(GetTenantsQuery req, CancellationToken cancellationToken)
2020
{
21-
var totalItems = _repository.Query<Tenant>().Count();
22-
var spec = new SearchTenants(req.Search, req.OrderBy, req.Descending);
21+
var totalItems = await _masterUow.Repository<Tenant>().CountAsync();
22+
var spec = new SearchTenants(req.Search, req.OrderBy, req.Page, req.PageSize, req.Descending);
2323

24-
var items = _repository
24+
var items = _masterUow.Repository<Tenant>()
2525
.ApplySpecification(spec)
26-
.Skip((req.Page - 1) * req.PageSize)
27-
.Take(req.PageSize)
2826
.Select(i => i.ToDto(req.IncludeConnectionStrings))
2927
.ToArray();
3028

3129
var totalPages = (int)Math.Ceiling(totalItems / (double)req.PageSize);
32-
return Task.FromResult(new PagedResponseResult<TenantDto>(items, totalItems, totalPages));
30+
return PagedResponseResult<TenantDto>.Create(items, totalItems, totalPages);
3331
}
3432
}

src/Core/Logistics.Application.Admin/Queries/GetUserJoinedOrganizations/GetUserJoinedOrganizationsHandler.cs

+14-9
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,39 @@
66

77
namespace Logistics.Application.Admin.Queries;
88

9-
internal sealed class GetUserJoinedOrganizationsHandler :
9+
internal sealed class GetUserJoinedOrganizationsHandler :
1010
RequestHandler<GetUserJoinedOrganizationsQuery, ResponseResult<OrganizationDto[]>>
1111
{
12-
private readonly IMasterRepository _repository;
12+
private readonly IMasterUnityOfWork _masterUow;
1313

14-
public GetUserJoinedOrganizationsHandler(IMasterRepository repository)
14+
public GetUserJoinedOrganizationsHandler(IMasterUnityOfWork masterUow)
1515
{
16-
_repository = repository;
16+
_masterUow = masterUow;
1717
}
1818

1919
protected override async Task<ResponseResult<OrganizationDto[]>> HandleValidated(
2020
GetUserJoinedOrganizationsQuery req,
2121
CancellationToken cancellationToken)
2222
{
23-
var user = await _repository.GetAsync<User>(req.UserId);
23+
var user = await _masterUow.Repository<User>().GetByIdAsync(req.UserId);
2424

2525
if (user is null)
26+
{
2627
return ResponseResult<OrganizationDto[]>.CreateError($"Could not find an user with ID '{req.UserId}'");
28+
}
2729

2830
var tenantsIds = user.GetJoinedTenantIds();
29-
var organizations = _repository.Query<Tenant>()
30-
.Where(i => tenantsIds.Contains(i.Id))
31+
var organizations = await _masterUow.Repository<Tenant>().GetListAsync(i => tenantsIds.Contains(i.Id));
32+
33+
var organizationsDto = organizations
3134
.Select(i => new OrganizationDto
3235
{
3336
TenantId = i.Id,
3437
Name = i.Name!,
3538
DisplayName = i.CompanyName!
36-
}).ToArray();
37-
return ResponseResult<OrganizationDto[]>.CreateSuccess(organizations);
39+
})
40+
.ToArray();
41+
42+
return ResponseResult<OrganizationDto[]>.CreateSuccess(organizationsDto);
3843
}
3944
}

0 commit comments

Comments
 (0)