Skip to content

Commit

Permalink
Merge pull request #196 from DFE-Digital/117330/fix-project-note-dupl…
Browse files Browse the repository at this point in the history
…ication

117330/fix project note duplication
  • Loading branch information
Tim Wilde authored Jan 9, 2023
2 parents 03949f3 + df9d234 commit d110005
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 25 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,6 @@ terraform.rc*
!terraform.tfvars.example
.terraform/
backend.vars

# Stackify
Stackify.json
8 changes: 3 additions & 5 deletions Dfe.Academies.Academisation.Data/AcademisationContext.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
using System.Reflection.Emit;
using Dfe.Academies.Academisation.Data.ConversionAdvisoryBoardDecisionAggregate;
using Dfe.Academies.Academisation.Data.ConversionAdvisoryBoardDecisionAggregate;
using Dfe.Academies.Academisation.Data.ProjectAggregate;
using Dfe.Academies.Academisation.Domain.ApplicationAggregate;
using Dfe.Academies.Academisation.Domain.ApplicationAggregate.Schools;
using Dfe.Academies.Academisation.Domain.ApplicationAggregate.Trusts;
using Dfe.Academies.Academisation.Domain.ConversionAdvisoryBoardDecisionAggregate;
using Dfe.Academies.Academisation.Domain.Core.ApplicationAggregate;
using Dfe.Academies.Academisation.Domain.SeedWork;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.IdentityModel.Tokens;

namespace Dfe.Academies.Academisation.Data;

Expand All @@ -32,6 +28,7 @@ public AcademisationContext(DbContextOptions<AcademisationContext> options) : ba
public DbSet<FormTrust> FormTrusts { get; set; } = null!; // done

public DbSet<ProjectState> Projects { get; set; } = null!;
public DbSet<ProjectNoteState> ProjectNotes { get; set; } = null!;
public DbSet<ConversionAdvisoryBoardDecisionState> ConversionAdvisoryBoardDecisions { get; set; } = null!;

public override int SaveChanges()
Expand Down Expand Up @@ -162,6 +159,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
private static void ConfigureProject(EntityTypeBuilder<ProjectState> projectConfiguration)
{
projectConfiguration.ToTable("Project", DEFAULT_SCHEMA);

projectConfiguration
.HasMany(x => x.Notes)
.WithOne()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public class ProjectNoteState
public string? Note { get; set; }
public string? Author { get; set; }
public DateTime? Date { get; set; }
public int ProjectId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public async Task Execute(IProject project)

await _context.Projects.SingleAsync(p => p.Id == project.Id);

_context.ReplaceTracked(projectState);
_context.ReplaceTracked(projectState)
.Collection(x => x.Notes!).IsModified = false;

await _context.SaveChangesAsync();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using Dfe.Academies.Academisation.Core;
using AutoFixture;
using Dfe.Academies.Academisation.Core;
using Dfe.Academies.Academisation.Data;
using Dfe.Academies.Academisation.Data.ProjectAggregate;
using Dfe.Academies.Academisation.Data.UnitTest.Contexts;
using Dfe.Academies.Academisation.Domain.Core.ProjectAggregate;
using Dfe.Academies.Academisation.IData.ProjectAggregate;
using Dfe.Academies.Academisation.IDomain.ProjectAggregate;
Expand All @@ -12,20 +16,36 @@ namespace Dfe.Academies.Academisation.Service.UnitTest.Commands.Legacy.Project
{
public class LegacyProjectAddNoteCommandTests
{
private readonly Mock<IProjectGetDataQuery> _projectGetDataQuery;
private readonly Mock<IProjectUpdateDataCommand> _projectUpdateDataCommand;
private readonly LegacyProjectAddNoteModel _addNoteModel;
private readonly AcademisationContext _context;
private readonly Mock<IProjectGetDataQuery> _projectGetDataQuery;

public LegacyProjectAddNoteCommandTests()
{
ProjectState projectState = new Fixture().Create<ProjectState>();

var testProjectContext = new TestProjectContext();
_context = testProjectContext.CreateContext();

_context.Projects.Add(projectState);
_context.SaveChanges();

_projectGetDataQuery = new Mock<IProjectGetDataQuery>();
_projectUpdateDataCommand = new Mock<IProjectUpdateDataCommand>();
_addNoteModel = new LegacyProjectAddNoteModel("Subject", "Note", "Author", DateTime.Today, 1234);
_addNoteModel = new LegacyProjectAddNoteModel(
"Subject",
"Note",
"Author",
DateTime.Today,
projectState.Id
);
}

private LegacyProjectAddNoteCommand System_under_test()
{
return new LegacyProjectAddNoteCommand(_projectGetDataQuery.Object, _projectUpdateDataCommand.Object);
return new LegacyProjectAddNoteCommand(
_projectGetDataQuery.Object,
_context
);
}

[Fact]
Expand All @@ -38,11 +58,19 @@ public async Task Should_return_not_found_result_if_the_specified_project_does_n
LegacyProjectAddNoteCommand command = System_under_test();

CommandResult result =
await command.Execute(_addNoteModel);

_projectUpdateDataCommand.Verify(x => x.Execute(It.IsAny<IProject>()), Times.Never);
await command.Execute(_addNoteModel);

result.Should().BeOfType<NotFoundCommandResult>();

_context.ProjectNotes.Should().NotContainEquivalentOf(
new ProjectNoteState
{
Date = _addNoteModel.Date,
Subject = _addNoteModel.Subject,
Author = _addNoteModel.Author,
Note = _addNoteModel.Note,
ProjectId = _addNoteModel.ProjectId
}, x => x.Excluding(q => q.Id));
}

[Fact]
Expand All @@ -58,8 +86,15 @@ public async Task Should_add_the_new_note_to_the_project()

await command.Execute(_addNoteModel with { ProjectId = project.Id });

project.Details.Notes
.Should().ContainEquivalentOf(new ProjectNote("Subject", "Note", "Author", _addNoteModel.Date));
_context.ProjectNotes.Should().ContainEquivalentOf(
new ProjectNoteState
{
Date = _addNoteModel.Date,
Subject = _addNoteModel.Subject,
Author = _addNoteModel.Author,
Note = _addNoteModel.Note,
ProjectId = _addNoteModel.ProjectId
}, x => x.Excluding(q => q.Id));
}

[Fact]
Expand All @@ -75,8 +110,6 @@ public async Task Should_save_the_amended_project()

CommandResult result = await command.Execute(_addNoteModel with { ProjectId = project.Id });

_projectUpdateDataCommand.Verify(x => x.Execute(project), Times.Once());

result.Should().BeOfType<CommandSuccessResult>();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

<ItemGroup>
<ProjectReference Include="..\Dfe.Academies.Academisation.Core\Dfe.Academies.Academisation.Core.csproj" />
<ProjectReference Include="..\Dfe.Academies.Academisation.Data.UnitTest\Dfe.Academies.Academisation.Data.UnitTest.csproj" />
<ProjectReference Include="..\Dfe.Academies.Academisation.Domain\Dfe.Academies.Academisation.Domain.csproj" />
<ProjectReference Include="..\Dfe.Academies.Academisation.IDomain\Dfe.Academies.Academisation.IDomain.csproj" />
<ProjectReference Include="..\Dfe.Academies.Academisation.Service\Dfe.Academies.Academisation.Service.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
using Dfe.Academies.Academisation.Core;
using Dfe.Academies.Academisation.Data;
using Dfe.Academies.Academisation.Data.ProjectAggregate;
using Dfe.Academies.Academisation.IData.ProjectAggregate;
using Dfe.Academies.Academisation.IDomain.ProjectAggregate;
using Dfe.Academies.Academisation.IService.Commands.Legacy.Project;
using Dfe.Academies.Academisation.IService.ServiceModels.Legacy.ProjectAggregate;
using Dfe.Academies.Academisation.Service.Extensions;

namespace Dfe.Academies.Academisation.Service.Commands.Legacy.Project
{
public class LegacyProjectAddNoteCommand : ILegacyProjectAddNoteCommand
{
private readonly IProjectGetDataQuery _projectGetDataQuery;
private readonly IProjectUpdateDataCommand _projectUpdateDataCommand;
private readonly AcademisationContext _context;

public LegacyProjectAddNoteCommand(IProjectGetDataQuery projectGetDataQuery, IProjectUpdateDataCommand projectUpdateDataCommand)
public LegacyProjectAddNoteCommand(IProjectGetDataQuery projectGetDataQuery, AcademisationContext context)
{
_projectGetDataQuery = projectGetDataQuery;
_projectUpdateDataCommand = projectUpdateDataCommand;
_context = context;
}

public async Task<CommandResult> Execute(LegacyProjectAddNoteModel model)
Expand All @@ -24,8 +25,16 @@ public async Task<CommandResult> Execute(LegacyProjectAddNoteModel model)

if (project is null) return new NotFoundCommandResult();

project.Details.Notes.Add(model.ToProjectNote());
await _projectUpdateDataCommand.Execute(project);
_context.ProjectNotes.Add(new ProjectNoteState
{
Author = model.Author,
Date = model.Date,
Subject = model.Subject,
Note = model.Note,
ProjectId = model.ProjectId
});

await _context.SaveChangesAsync();

return new CommandSuccessResult();
}
Expand Down

0 comments on commit d110005

Please sign in to comment.