Skip to content

Commit

Permalink
Changed the endpoint name
Browse files Browse the repository at this point in the history
  • Loading branch information
mshakirdfe committed Aug 16, 2024
2 parents 937edf1 + e95d87e commit d39a5fd
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@ public TransferProjectRepository(AcademisationContext context) : base(context)
return await DefaultIncludes().SingleOrDefaultAsync(x => x.Urn == urn);
}

public async Task<IEnumerable<ITransferProject>> GetTransferProjectsByIncomingTrustUkprn(string ukprn, CancellationToken cancellationToken)
=> await dbSet.Include(x => x.TransferringAcademies)
.Include(x=> x.IntendedTransferBenefits)
.Where(x => x.TransferringAcademies.Any(a => a.IncomingTrustUkprn == ukprn) && x.Status == null && x.ProjectGroupId == null).ToListAsync(cancellationToken);

public async Task<IEnumerable<ITransferProject>> GetTransfersProjectsForGroup(string ukprn, CancellationToken cancellationToken)
=> await DefaultIncludes().Where(x => x.TransferringAcademies.Any(a => a.IncomingTrustUkprn == ukprn)
&& x.Status == null && x.ProjectGroupId == null).ToListAsync(cancellationToken);
public async Task<IEnumerable<ITransferProject>> GetTransferProjectsByIdsAsync(List<int> ids, CancellationToken cancellationToken)
=> await dbSet.Include(x => x.TransferringAcademies)
.Include(x => x.IntendedTransferBenefits)
=> await DefaultIncludes()
.Where(x => ids.Contains(x.Id) && x.ProjectGroupId == null).ToListAsync(cancellationToken);

public async Task<(IEnumerable<ITransferProject>, int totalcount)> SearchProjects(IEnumerable<string>? states, string? title, IEnumerable<string>? deliveryOfficers, int page, int count)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public interface ITransferProjectRepository : IRepository<TransferProject>, IGen
public Task<ITransferProject?> GetByUrn(int urn);
public Task<IEnumerable<ITransferProject?>> GetAllTransferProjects();
public Task<IEnumerable<ITransferProject?>> GetIncompleteProjects();
Task<IEnumerable<ITransferProject>> GetTransferProjectsByIncomingTrustUkprn(string ukprn, CancellationToken cancellationToken);
Task<IEnumerable<ITransferProject>> GetTransfersProjectsForGroup(string ukprn, CancellationToken cancellationToken);
Task<IEnumerable<ITransferProject>> GetTransferProjectsByIdsAsync(List<int> ids, CancellationToken cancellationToken);
Task<(IEnumerable<ITransferProject>, int totalcount)> SearchProjects(IEnumerable<string>? states, string? title, IEnumerable<string>? deliveryOfficers, int page, int count);
Task<IEnumerable<ITransferProject>> GetProjectsByProjectGroupIdAsync(int? projectGroupId, CancellationToken cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface ITransferProjectQueryService
{
Task<AcademyTransferProjectResponse?> GetByUrn(int Urn);
Task<AcademyTransferProjectResponse?> GetById(int id);
Task<IEnumerable<AcademyTransferProjectSummaryResponse>?> GetTransferProjectsByIncomingTrustUkprn(string ukprn, CancellationToken cancellationToken);
Task<IEnumerable<AcademyTransferProjectSummaryResponse>?> GetTransfersProjectsForGroup(string ukprn, CancellationToken cancellationToken);
Task<PagedResultResponse<AcademyTransferProjectSummaryResponse>> GetTransferProjects(int page, int count, int? urn,
string title);
Task<PagedDataResponse<AcademyTransferProjectSummaryResponse>?> GetProjects(IEnumerable<string>? states, string? title, IEnumerable<string>? deliveryOfficers, int page, int count);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using AutoFixture;
using Dfe.Academies.Academisation.Core;
using Dfe.Academies.Academisation.Domain.ProjectGroupsAggregate;
using Dfe.Academies.Academisation.Domain.SeedWork;
using Dfe.Academies.Academisation.Service.Commands.ProjectGroup;
using Microsoft.Extensions.Logging;
using Moq;
using Xunit;

namespace Dfe.Academies.Academisation.Service.UnitTest.Commands.ProjectGroup
{
public class DeleteProjectGroupCommandHandlerTests
{
private readonly MockRepository _mockRepository;
private readonly Mock<IProjectGroupRepository> _mockProjectGroupRepository;
private readonly Mock<ILogger<DeleteProjectGroupCommandHandler>> _mocklogger;
private readonly Fixture _fixture = new();
private readonly CancellationToken _cancellationToken;
private readonly DeleteProjectGroupCommandHandler _deleteProjectGroupCommandHandler;

public DeleteProjectGroupCommandHandlerTests()
{
_mockRepository = new MockRepository(MockBehavior.Strict);
_cancellationToken = CancellationToken.None;
_mockProjectGroupRepository = _mockRepository.Create<IProjectGroupRepository>();
_mocklogger = new Mock<ILogger<DeleteProjectGroupCommandHandler>>();

var mockContext = new Mock<IUnitOfWork>();
_mockProjectGroupRepository.Setup(x => x.UnitOfWork).Returns(mockContext.Object);

_deleteProjectGroupCommandHandler = new DeleteProjectGroupCommandHandler(
_mockProjectGroupRepository.Object,
_mocklogger.Object);
}

[Fact]
public async Task Handle_ProjectGroupDoesNotExists_ReturnsNotFoundCommandResult()
{
// Arrange
var request = new DeleteProjectGroupCommand("Reference");
_mockProjectGroupRepository.Setup(x => x.GetByReferenceNumberAsync(request.GroupReferenceNumber, _cancellationToken)).ReturnsAsync((Domain.ProjectGroupsAggregate.ProjectGroup?)null);

// Act
var result = await _deleteProjectGroupCommandHandler.Handle(
request,
_cancellationToken);

// Assert
var notFoundCommandResult = Assert.IsType<NotFoundCommandResult>(result);
_mockProjectGroupRepository.Verify(x => x.Update(It.IsAny<Domain.ProjectGroupsAggregate.ProjectGroup>()), Times.Never());
_mockProjectGroupRepository.Verify(x => x.GetByReferenceNumberAsync(request.GroupReferenceNumber, _cancellationToken), Times.Once());
_mockProjectGroupRepository.Verify(x => x.UnitOfWork.SaveChangesAsync(It.Is<CancellationToken>(x => x == _cancellationToken)), Times.Never());
}

[Fact]
public async Task Handle_ValidRequestWithNoRemovedConversions_ReturnsSuccess()
{
// Arrange
var expectedProjectGroup = _fixture.Create<Domain.ProjectGroupsAggregate.ProjectGroup>();
expectedProjectGroup.SetProjectReference(1);
var request = new DeleteProjectGroupCommand(expectedProjectGroup.ReferenceNumber!);
_mockProjectGroupRepository.Setup(x => x.Update(It.IsAny<Domain.ProjectGroupsAggregate.ProjectGroup>()));
_mockProjectGroupRepository.Setup(x => x.GetByReferenceNumberAsync(request.GroupReferenceNumber, _cancellationToken)).ReturnsAsync(expectedProjectGroup);
_mockProjectGroupRepository.Setup(x => x.Delete(expectedProjectGroup));

// Act
var result = await _deleteProjectGroupCommandHandler.Handle(
request,
_cancellationToken);

// Assert
var commandSuccessResult = Assert.IsType<CommandSuccessResult>(result);
_mockProjectGroupRepository.Verify(x => x.GetByReferenceNumberAsync(request.GroupReferenceNumber, _cancellationToken), Times.Once);
_mockProjectGroupRepository.Verify(x => x.Delete(expectedProjectGroup), Times.Once);
_mockProjectGroupRepository.Verify(x => x.UnitOfWork.SaveChangesAsync(_cancellationToken), Times.Once);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ public async Task GetProjects_ReturnsFilteredProjects()
}

[Fact]
public async Task GetTransferProjectsByIncomingTrustUkprn_ShouldReturnTranferProjects()
public async Task GetTransfersProjectsForGroup_ShouldReturnTranferProjects()
{
// Arrange
var trustUrn = "23456789";
Expand All @@ -302,28 +302,28 @@ public async Task GetTransferProjectsByIncomingTrustUkprn_ShouldReturnTranferPro
DateTime.Now
);
var cancelationToken = CancellationToken.None;
_mockTransferProjectRepository.Setup(repo => repo.GetTransferProjectsByIncomingTrustUkprn(trustUrn, cancelationToken))
_mockTransferProjectRepository.Setup(repo => repo.GetTransfersProjectsForGroup(trustUrn, cancelationToken))
.ReturnsAsync([(dummyTransferProject!)]);
var expectedResponse = _service.AcademyTransferProjectSummaryResponse([dummyTransferProject]);

// Action
var result = await _service.GetTransferProjectsByIncomingTrustUkprn(trustUrn, cancelationToken);
var result = await _service.GetTransfersProjectsForGroup(trustUrn, cancelationToken);

// Assert
result.Should().BeEquivalentTo(expectedResponse);
}
[Fact]
public async Task GetTransferProjectsByIncomingTrustUkprn_ShouldReturnNoTranferProject()
public async Task GetTransfersProjectsForGroup_ShouldReturnNoTranferProject()
{
// Arrange
var trustUrn = "123213";
var cancelationToken = CancellationToken.None;
_mockTransferProjectRepository.Setup(repo => repo.GetTransferProjectsByIncomingTrustUkprn(trustUrn, cancelationToken))
_mockTransferProjectRepository.Setup(repo => repo.GetTransfersProjectsForGroup(trustUrn, cancelationToken))
.ReturnsAsync([]);
var service = new TransferProjectQueryService(_mockTransferProjectRepository.Object, _establishmentRepo, _advisoryBoardDecisionRepository);

// Action
var result = await service.GetTransferProjectsByIncomingTrustUkprn(trustUrn, cancelationToken);
var result = await service.GetTransfersProjectsForGroup(trustUrn, cancelationToken);

// Assert
result.Should().BeEmpty();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Dfe.Academies.Academisation.Core;
using MediatR;

namespace Dfe.Academies.Academisation.Service.Commands.ProjectGroup
{
public class DeleteProjectGroupCommand(string referenceNumber) : IRequest<CommandResult>
{
public string GroupReferenceNumber { get; set; } = referenceNumber;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Dfe.Academies.Academisation.Core;
using Dfe.Academies.Academisation.Domain.ProjectGroupsAggregate;
using MediatR;
using Microsoft.Extensions.Logging;

namespace Dfe.Academies.Academisation.Service.Commands.ProjectGroup
{
public class DeleteProjectGroupCommandHandler(IProjectGroupRepository projectGroupRepository, ILogger<DeleteProjectGroupCommandHandler> logger) : IRequestHandler<DeleteProjectGroupCommand, CommandResult>
{
public async Task<CommandResult> Handle(DeleteProjectGroupCommand message, CancellationToken cancellationToken)
{
logger.LogInformation("Setting project group with reference number:{value}", message.GroupReferenceNumber);

var projectGroup = await projectGroupRepository.GetByReferenceNumberAsync(message.GroupReferenceNumber, cancellationToken);
if (projectGroup == null)
{
logger.LogError("Project group is not found with reference number:{value}", message.GroupReferenceNumber);
return new NotFoundCommandResult();
}

projectGroupRepository.Delete(projectGroup);
await projectGroupRepository.UnitOfWork.SaveChangesAsync(cancellationToken);

return new CommandSuccessResult();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,9 @@ public IEnumerable<AcademyTransferProjectSummaryResponse> AcademyTransferProject
});
}

public async Task<IEnumerable<AcademyTransferProjectSummaryResponse>?> GetTransferProjectsByIncomingTrustUkprn(string ukprn, CancellationToken cancellationToken)
public async Task<IEnumerable<AcademyTransferProjectSummaryResponse>?> GetTransfersProjectsForGroup(string ukprn, CancellationToken cancellationToken)
{
var transferProjects = await _transferProjectRepository.GetTransferProjectsByIncomingTrustUkprn(ukprn, cancellationToken);
var transferProjects = await _transferProjectRepository.GetTransfersProjectsForGroup(ukprn, cancellationToken);

return AcademyTransferProjectSummaryResponse(transferProjects);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using AutoFixture;
using System.Web.Http.Results;
using AutoFixture;
using Dfe.Academies.Academisation.Core;
using Dfe.Academies.Academisation.Data;
using Dfe.Academies.Academisation.Domain.ProjectAggregate;
using Dfe.Academies.Academisation.Domain.TransferProjectAggregate;
using Dfe.Academies.Academisation.IDomain.TransferProjectAggregate;
using Dfe.Academies.Academisation.IService.ServiceModels.ProjectGroup;
using Dfe.Academies.Academisation.Service.Commands.ProjectGroup;
using Dfe.Academies.Academisation.SubcutaneousTest.Utils;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace Dfe.Academies.Academisation.SubcutaneousTest.ProjectGroup
Expand Down Expand Up @@ -211,5 +214,33 @@ private async Task VerifyProjectGroupAsync(HttpResponseMessage httpResponseMessa
Assert.NotNull(dbTransferProject);
Assert.Equal(dbTransferProject.ProjectGroupId, projectGroup.Id);
}

[Fact]
public async Task DeleteProjectGroup_ShouldDeleteSuccessfully()
{
// Arrange
var projectGroup = await CreateProjectGroup();

// Action
var httpResponseMessage = await _client.DeleteAsync($"project-group/{projectGroup.ReferenceNumber}", CancellationToken);

Assert.True(httpResponseMessage.IsSuccessStatusCode);
var dbProjectGroup = await _context.ProjectGroups.AsNoTracking().FirstOrDefaultAsync(x => x.ReferenceNumber == projectGroup.ReferenceNumber);
Assert.Null(dbProjectGroup);
}

[Fact]
public async Task DeleteProjectGroup_ShouldReturnNotFoundOnNotFindingGroup()
{
// Arrange
var referenceNumber = Fixture.Create<string>()[..10];

// Action
var httpResponseMessage = await _client.DeleteAsync($"project-group/{referenceNumber}", CancellationToken);

// Assert
Assert.False(httpResponseMessage.IsSuccessStatusCode);
Assert.Equal(httpResponseMessage!.StatusCode.GetHashCode(), System.Net.HttpStatusCode.NotFound.GetHashCode());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,20 @@ public async Task<ActionResult<ProjectGroupResponseModel>> GetProjectGrouptById(

return Ok(project);
}

[HttpDelete("{referenceNumber}", Name = "DeleteProjectGroupByReference")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<ActionResult<ProjectGroupResponseModel>> DeleteProjectGroupByReference(string referenceNumber, CancellationToken cancellationToken)
{
var result = await mediator.Send(new DeleteProjectGroupCommand(referenceNumber), cancellationToken);

return result switch
{
CommandSuccessResult => Ok(),
NotFoundCommandResult => NotFound(),
_ => new InternalServerErrorObjectResult("Error serving request")
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,12 @@ public async Task<ActionResult<IEnumerable<OpeningDateHistoryDto>>> GetOpeningDa
return Ok(result);
}

[HttpGet("{ukprn}/get-transfers-by-incoming-trust", Name = "GetTransfersByIncomingTrust")]
public async Task<ActionResult<IEnumerable<AcademyTransferProjectSummaryResponse>>> GetTransfersByincomingTrust(string ukprn, CancellationToken cancellationToken)
[HttpGet("{ukprn}/get-transfers-for-group", Name = "GetTransfersProjectsForGroup")]
public async Task<ActionResult<IEnumerable<AcademyTransferProjectSummaryResponse>>> GetTransfersProjectsForGroup(string ukprn, CancellationToken cancellationToken)
{
_logger.LogInformation("Getting transfer projects by incoming trust ukprn: {value}", ukprn);

var result = await _transferProjectQueryService.GetTransferProjectsByIncomingTrustUkprn(ukprn, cancellationToken);
var result = await _transferProjectQueryService.GetTransfersProjectsForGroup(ukprn, cancellationToken);

return result is null ? NotFound() : Ok(result);
}
Expand Down

0 comments on commit d39a5fd

Please sign in to comment.