Skip to content

Commit

Permalink
Merge pull request #268 from DFE-Digital/consistency-refactor
Browse files Browse the repository at this point in the history
Consistency refactor
  • Loading branch information
paullocknimble authored Jun 13, 2023
2 parents 65fff6c + 4130663 commit 01e7263
Show file tree
Hide file tree
Showing 38 changed files with 155 additions and 158 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Commands\Application\School\**" />
<EmbeddedResource Remove="Commands\Application\School\**" />
<None Remove="Commands\Application\School\**" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="MediatR" Version="12.0.1" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public async Task ApplicationReturnedFromFactory___ApplicationPassedToDataLayer_
.Setup(x => x.Create(It.IsAny<ApplicationType>(), It.IsAny<ContributorDetails>()))
.Returns(new CreateSuccessResult<Application>(app));

ApplicationCreateCommand subject = new(_applicationFactoryMock.Object, _repo.Object, _mockMapper.Object);
ApplicationCreateCommandHandler subject = new(_applicationFactoryMock.Object, _repo.Object, _mockMapper.Object);

// act
var result = await subject.Execute(applicationCreateRequestModel);
var result = await subject.Handle(applicationCreateRequestModel, default);

// assert
_repo.Verify(x => x.Insert(It.Is<Application>(y => y == app)), Times.Once());
Expand All @@ -68,10 +68,10 @@ public async Task ValidationErrorReturnedFromFactory___ApplicationNotPassedToDat
.Setup(x => x.Create(It.IsAny<ApplicationType>(), It.IsAny<ContributorDetails>()))
.Returns(new CreateValidationErrorResult(new List<ValidationError>()));

ApplicationCreateCommand subject = new(_applicationFactoryMock.Object, _repo.Object, _mockMapper.Object);
ApplicationCreateCommandHandler subject = new(_applicationFactoryMock.Object, _repo.Object, _mockMapper.Object);

// act
var result = await subject.Execute(applicationCreateRequestModel);
var result = await subject.Handle(applicationCreateRequestModel, default);

// assert
_repo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public async Task NotFound___NotPassedToDataLayer_NotFoundReturned()
_repo.Setup(x => x.GetByIdAsync(_applicationId)).ReturnsAsync((Application?)null);

// act
var result = await _subject.Handle(new SubmitApplicationCommand(_applicationId), default(CancellationToken));
var result = await _subject.Handle(new ApplicationSubmitCommand(_applicationId), default(CancellationToken));

// assert
Assert.IsType<NotFoundCommandResult>(result);
Expand All @@ -64,7 +64,7 @@ public async Task SubmitApplicationValidationError___NotPassedToUpdateDataComman
_applicationSubmissionServiceMock.Setup(x => x.SubmitApplication(_applicationMock.Object)).Returns(commandValidationErrorResult);

// act
var result = await _subject.Handle(new SubmitApplicationCommand(_applicationId), default(CancellationToken));
var result = await _subject.Handle(new ApplicationSubmitCommand(_applicationId), default(CancellationToken));

// assert
Assert.IsType<CommandValidationErrorResult>(result);
Expand All @@ -84,7 +84,7 @@ public async Task ProjectNotCreated___PassedToDataLayer_CommandSuccessReturned()
.Returns(new CommandSuccessResult());

// act
var result = await _subject.Handle(new SubmitApplicationCommand(_applicationId), default(CancellationToken));
var result = await _subject.Handle(new ApplicationSubmitCommand(_applicationId), default(CancellationToken));

// assert
Assert.IsType<CommandSuccessResult>(result);
Expand All @@ -105,7 +105,7 @@ public async Task ProjectCreated___PassedToDataLayer_CreateSuccessReturned()
.Returns(new CreateSuccessResult<IProject>(_projectMock.Object));

// act
var result = await _subject.Handle(new SubmitApplicationCommand(_applicationId), default(CancellationToken));
var result = await _subject.Handle(new ApplicationSubmitCommand(_applicationId), default(CancellationToken));

// assert
Assert.IsType<CreateSuccessResult<LegacyProjectServiceModel>>(result);
Expand All @@ -126,7 +126,7 @@ public async Task ProjectCreateValidationError___NotPassedToUpdateDataCommand_Va
.Returns(createValidationErrorResult);

// act
var result = await _subject.Handle(new SubmitApplicationCommand(_applicationId), default(CancellationToken));
var result = await _subject.Handle(new ApplicationSubmitCommand(_applicationId), default(CancellationToken));

// assert
Assert.IsType<CreateValidationErrorResult>(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ internal class ApplicationCreateRequestModelBuilder
private static readonly Faker _faker = new();


public ApplicationCreateRequestModel Build()
public ApplicationCreateCommand Build()
{
return new ApplicationCreateRequestModel(_applicationType, _contributorRequestModel);
return new ApplicationCreateCommand(_applicationType, _contributorRequestModel);
}

public ApplicationCreateRequestModelBuilder WithApplicationType(ApplicationType applicationType)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,45 +1,17 @@
using AutoMapper;
using Dfe.Academies.Academisation.Core;
using Dfe.Academies.Academisation.Domain.ApplicationAggregate;
using Dfe.Academies.Academisation.Core;
using Dfe.Academies.Academisation.Domain.Core.ApplicationAggregate;
using Dfe.Academies.Academisation.IDomain.ApplicationAggregate;
using Dfe.Academies.Academisation.IService.Commands.AdvisoryBoardDecision;
using Dfe.Academies.Academisation.IService.RequestModels;
using Dfe.Academies.Academisation.Service.Mappers.Application;
using MediatR;

namespace Dfe.Academies.Academisation.Service.Commands.Application;
namespace Dfe.Academies.Academisation.IService.RequestModels;

public class ApplicationCreateCommand : IApplicationCreateCommand
public class ApplicationCreateCommand : IRequest<CreateResult>
{
private readonly IApplicationFactory _domainFactory;
private readonly IApplicationRepository _applicationRepository;
private readonly IMapper _mapper;

public ApplicationCreateCommand(IApplicationFactory domainFactory, IApplicationRepository applicationRepository, IMapper mapper)
public ApplicationCreateCommand(ApplicationType applicationType, ContributorRequestModel contributor)
{
_domainFactory = domainFactory;
_applicationRepository = applicationRepository;
_mapper = mapper;
ApplicationType = applicationType;
Contributor = contributor;
}

public async Task<CreateResult> Execute(ApplicationCreateRequestModel applicationCreateRequestModel)
{
(ApplicationType applicationType, ContributorDetails contributorDetails) = applicationCreateRequestModel.AsDomain();
var result = _domainFactory.Create(applicationType, contributorDetails);

if (result is CreateValidationErrorResult domainValidationErrorResult)
{
return domainValidationErrorResult.MapToPayloadType();
}

if (result is not CreateSuccessResult<Domain.ApplicationAggregate.Application> domainSuccessResult)
{
throw new NotImplementedException("Other CreateResult types not expected");
}

await _applicationRepository.Insert(domainSuccessResult.Payload);
await _applicationRepository.UnitOfWork.SaveChangesAsync();

return domainSuccessResult.MapToPayloadType(ApplicationServiceModelMapper.MapFromDomain, _mapper);
}
public ApplicationType ApplicationType { get; }
public ContributorRequestModel Contributor { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using AutoMapper;
using Dfe.Academies.Academisation.Core;
using Dfe.Academies.Academisation.Domain.ApplicationAggregate;
using Dfe.Academies.Academisation.Domain.Core.ApplicationAggregate;
using Dfe.Academies.Academisation.IDomain.ApplicationAggregate;
using Dfe.Academies.Academisation.IService.Commands.AdvisoryBoardDecision;
using Dfe.Academies.Academisation.IService.RequestModels;
using Dfe.Academies.Academisation.Service.Mappers.Application;
using MediatR;

namespace Dfe.Academies.Academisation.Service.Commands.Application;

public class ApplicationCreateCommandHandler : IRequestHandler<ApplicationCreateCommand, CreateResult>
{
private readonly IApplicationFactory _domainFactory;
private readonly IApplicationRepository _applicationRepository;
private readonly IMapper _mapper;

public ApplicationCreateCommandHandler(IApplicationFactory domainFactory, IApplicationRepository applicationRepository, IMapper mapper)
{
_domainFactory = domainFactory;
_applicationRepository = applicationRepository;
_mapper = mapper;
}

public async Task<CreateResult> Handle(ApplicationCreateCommand applicationCreateRequestModel, CancellationToken cancellationToken)
{
(ApplicationType applicationType, ContributorDetails contributorDetails) = applicationCreateRequestModel.AsDomain();
var result = _domainFactory.Create(applicationType, contributorDetails);

if (result is CreateValidationErrorResult domainValidationErrorResult)
{
return domainValidationErrorResult.MapToPayloadType();
}

if (result is not CreateSuccessResult<Domain.ApplicationAggregate.Application> domainSuccessResult)
{
throw new NotImplementedException("Other CreateResult types not expected");
}

await _applicationRepository.Insert(domainSuccessResult.Payload);
await _applicationRepository.UnitOfWork.SaveChangesAsync();

return domainSuccessResult.MapToPayloadType(ApplicationServiceModelMapper.MapFromDomain, _mapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

namespace Dfe.Academies.Academisation.IService.Commands.Application
{
public record SubmitApplicationCommand(
public record ApplicationSubmitCommand(
int applicationId) : IRequest<CommandOrCreateResult>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Dfe.Academies.Academisation.Service.Commands.Application
{
public class ApplicationSubmitCommandHandler : IRequestHandler<SubmitApplicationCommand, CommandOrCreateResult>
public class ApplicationSubmitCommandHandler : IRequestHandler<ApplicationSubmitCommand, CommandOrCreateResult>
{
private readonly IApplicationRepository _applicationRepository;
private readonly IProjectCreateDataCommand _projectCreateDataCommand;
Expand All @@ -27,7 +27,7 @@ public ApplicationSubmitCommandHandler(
_applicationSubmissionService = applicationSubmissionService;
}

public async Task<CommandOrCreateResult> Handle(SubmitApplicationCommand command, CancellationToken cancellationToken)
public async Task<CommandOrCreateResult> Handle(ApplicationSubmitCommand command, CancellationToken cancellationToken)
{
var application = await _applicationRepository.GetByIdAsync(command.applicationId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Dfe.Academies.Academisation.IService.Commands.Application;
using MediatR;

namespace Dfe.Academies.Academisation.Service.Commands.Application
namespace Dfe.Academies.Academisation.Service.Commands.Application.School
{
public class DeleteSchoolCommandHandler : IRequestHandler<DeleteSchoolCommand, CommandResult>
{
Expand All @@ -29,7 +29,7 @@ public async Task<CommandResult> Handle(DeleteSchoolCommand command, Cancellatio
var schoolToDelete = existingApplication.Schools.SingleOrDefault(x => x.Details.Urn == command.Urn);

var result = existingApplication.DeleteSchool(command.Urn);

if (result is CommandValidationErrorResult)
{
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Dfe.Academies.Academisation.IService.ServiceModels.Application;
using MediatR;

namespace Dfe.Academies.Academisation.IService.Commands.Application
namespace Dfe.Academies.Academisation.Service.Commands.Application.Trust
{
public record CreateTrustKeyPersonCommand(
int ApplicationId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
using Dfe.Academies.Academisation.Core;
using Dfe.Academies.Academisation.Domain.ApplicationAggregate;
using Dfe.Academies.Academisation.Domain.ApplicationAggregate.Trusts;
using Dfe.Academies.Academisation.IService.Commands.Application;
using MediatR;

namespace Dfe.Academies.Academisation.Service.Commands.Application
namespace Dfe.Academies.Academisation.Service.Commands.Application.Trust
{
public class CreateTrustKeyPersonCommandHandler : IRequestHandler<CreateTrustKeyPersonCommand, CommandResult>
{
Expand All @@ -30,7 +29,7 @@ public async Task<CommandResult> Handle(CreateTrustKeyPersonCommand command, Can
}

var result = existingApplication.AddTrustKeyPerson(command.Name, command.DateOfBirth, command.Biography, command.Roles.Select(x => TrustKeyPersonRole.Create(x.Role, x.TimeInRole)));

if (result is CommandValidationErrorResult)
{
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Dfe.Academies.Academisation.Domain.Core.ApplicationAggregate;
using MediatR;

namespace Dfe.Academies.Academisation.IService.Commands.Application
namespace Dfe.Academies.Academisation.Service.Commands.Application.Trust
{
public record DeleteTrustKeyPersonCommand(
int ApplicationId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using Dfe.Academies.Academisation.Core;
using Dfe.Academies.Academisation.Domain.ApplicationAggregate;
using Dfe.Academies.Academisation.IService.Commands.Application;
using MediatR;
using TrustKeyPerson = Dfe.Academies.Academisation.Domain.ApplicationAggregate.Trusts.TrustKeyPerson;

namespace Dfe.Academies.Academisation.Service.Commands.Application
namespace Dfe.Academies.Academisation.Service.Commands.Application.Trust
{
public class DeleteTrustKeyPersonCommandHandler : IRequestHandler<DeleteTrustKeyPersonCommand, CommandResult>
{
Expand All @@ -28,7 +27,7 @@ public async Task<CommandResult> Handle(DeleteTrustKeyPersonCommand command, Can
}

var result = existingApplication.DeleteTrustKeyPerson(command.KeyPersonId);

if (result is CommandValidationErrorResult)
{
return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using Dfe.Academies.Academisation.Core;
using Dfe.Academies.Academisation.Domain.ApplicationAggregate;
using Dfe.Academies.Academisation.Domain.Core.ApplicationAggregate;
using Dfe.Academies.Academisation.IService.Commands.Application;
using MediatR;

namespace Dfe.Academies.Academisation.Service.Commands.Application
namespace Dfe.Academies.Academisation.Service.Commands.Application.Trust
{
public class FormTrustCommandHandler : IRequestHandler<SetFormTrustDetailsCommand, CommandResult>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using Dfe.Academies.Academisation.Core;
using Dfe.Academies.Academisation.Domain.ApplicationAggregate;
using Dfe.Academies.Academisation.IService.Commands.Application;
using MediatR;

namespace Dfe.Academies.Academisation.Service.Commands.Application
namespace Dfe.Academies.Academisation.Service.Commands.Application.Trust
{
public class JoinTrustCommandHandler : IRequestHandler<SetJoinTrustDetailsCommand, CommandResult>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Dfe.Academies.Academisation.Core;
using MediatR;

namespace Dfe.Academies.Academisation.IService.Commands.Application
namespace Dfe.Academies.Academisation.Service.Commands.Application.Trust
{
public record SetFormTrustDetailsCommand(
int applicationId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Dfe.Academies.Academisation.Domain.Core.ApplicationAggregate;
using MediatR;

namespace Dfe.Academies.Academisation.IService.Commands.Application
namespace Dfe.Academies.Academisation.Service.Commands.Application.Trust
{
public record SetJoinTrustDetailsCommand(
int applicationId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using Dfe.Academies.Academisation.IService.ServiceModels.Application;
using MediatR;

namespace Dfe.Academies.Academisation.IService.Commands.Application
namespace Dfe.Academies.Academisation.Service.Commands.Application.Trust
{
public record UpdateTrustKeyPersonCommand(
int ApplicationId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using Dfe.Academies.Academisation.Core;
using Dfe.Academies.Academisation.Domain.ApplicationAggregate;
using Dfe.Academies.Academisation.Domain.ApplicationAggregate.Trusts;
using Dfe.Academies.Academisation.IService.Commands.Application;
using MediatR;

namespace Dfe.Academies.Academisation.Service.Commands.Application
namespace Dfe.Academies.Academisation.Service.Commands.Application.Trust
{
public class UpdateTrustKeyPersonCommandHandler : IRequestHandler<UpdateTrustKeyPersonCommand, CommandResult>
{
Expand All @@ -27,7 +26,7 @@ public async Task<CommandResult> Handle(UpdateTrustKeyPersonCommand command, Can
}

var result = existingApplication.UpdateTrustKeyPerson(command.KeyPersonId, command.Name, command.DateOfBirth, command.Biography, command.Roles.Select(x => TrustKeyPersonRole.Create(x.Id, x.Role, x.TimeInRole)));

if (result is CommandValidationErrorResult)
{
return result;
Expand Down
Loading

0 comments on commit 01e7263

Please sign in to comment.