Skip to content

Commit

Permalink
Merge pull request #34 from DFE-Digital/add-notes-new-notes-table
Browse files Browse the repository at this point in the history
Add notes new notes table
  • Loading branch information
paullocknimble authored Jan 10, 2025
2 parents 9dfd632 + c997891 commit f4c02b8
Show file tree
Hide file tree
Showing 28 changed files with 899 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Dfe.RegionalImprovementForStandardsAndExcellence.Application.SupportProject.Commands.CreateSupportProject;
using Dfe.RegionalImprovementForStandardsAndExcellence.Domain.Interfaces.Repositories;
using Dfe.RegionalImprovementForStandardsAndExcellence.Domain.ValueObjects;
using Dfe.RegionalImprovementForStandardsAndExcellence.Utils;
using MediatR;

namespace Dfe.RegionalImprovementForStandardsAndExcellence.Application.SupportProject.Commands.CreateSupportProjectNote;

public class CreateSupportProjectNote
{
public record CreateSupportProjectNoteCommand(
SupportProjectId SupportProjectId,
string Note,
string Author
) : IRequest<SupportProjectNoteId>;

public class CreateSupportProjectNoteCommandHandler(ISupportProjectRepository supportProjectRepository, IDateTimeProvider _dateTimeProvider)
: IRequestHandler<CreateSupportProjectNoteCommand, SupportProjectNoteId>
{
public async Task<SupportProjectNoteId> Handle(CreateSupportProjectNoteCommand request, CancellationToken cancellationToken)
{

var supportProject = await supportProjectRepository.FindAsync(x => x.Id == request.SupportProjectId, cancellationToken);

var supportProjectNoteId = new SupportProjectNoteId(Guid.NewGuid());

supportProject.AddNote(supportProjectNoteId,request.Note,request.Author,_dateTimeProvider.Now,request.SupportProjectId);

await supportProjectRepository.UpdateAsync(supportProject);

return supportProjectNoteId;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Dfe.RegionalImprovementForStandardsAndExcellence.Application.SupportProject.Commands.CreateSupportProject;
using Dfe.RegionalImprovementForStandardsAndExcellence.Domain.Entities.SupportProject;
using Dfe.RegionalImprovementForStandardsAndExcellence.Domain.Interfaces.Repositories;
using Dfe.RegionalImprovementForStandardsAndExcellence.Domain.ValueObjects;
using Dfe.RegionalImprovementForStandardsAndExcellence.Utils;
using MediatR;

namespace Dfe.RegionalImprovementForStandardsAndExcellence.Application.SupportProject.Commands.EditSupportProjectNote;

public class EditSupportProjectNote
{
public record EditSupportProjectNoteCommand(
SupportProjectId SupportProjectId,
string Note,
SupportProjectNoteId Id,
string Author
) : IRequest<SupportProjectNoteId>;

public class EditSupportProjectNoteCommandHandler(ISupportProjectRepository supportProjectRepository,IDateTimeProvider _dateTimeProvider)
: IRequestHandler<EditSupportProjectNoteCommand, SupportProjectNoteId>
{
public async Task<SupportProjectNoteId> Handle(EditSupportProjectNoteCommand request, CancellationToken cancellationToken)
{
var supportProject = await supportProjectRepository.GetSupportProjectById(request.SupportProjectId, cancellationToken);

supportProject.EditSupportProjectNote(request.Id,request.Note,request.Author,_dateTimeProvider.Now);

await supportProjectRepository.UpdateAsync(supportProject);

return request.Id;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dfe.RegionalImprovementForStandardsAndExcellence.Domain.Entities.SupportProject;

namespace Dfe.RegionalImprovementForStandardsAndExcellence.Application.SupportProject.Models
{
Expand All @@ -13,6 +14,7 @@ public record SupportProjectDto(int id,
string localAuthority,
string region,
string assignedAdviserFullName,
string assignedAdviserEmailAddress);
string assignedAdviserEmailAddress,
IEnumerable<SupportProjectNote> notes);

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public async Task<Result<IEnumerable<SupportProjectDto>>> GetAllSupportProjects(
public async Task<Result<SupportProjectDto?>> GetSupportProject(int id, CancellationToken cancellationToken)
{
var supportProjectId = new SupportProjectId(id);
var supportProject = await supportProjectRepository.FindAsync(x => x.Id == supportProjectId, cancellationToken);
var supportProject = await supportProjectRepository.GetSupportProjectById(supportProjectId, cancellationToken);

var result = mapper.Map<SupportProjectDto?>(supportProject);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,18 @@ public SupportProject(
public string CreatedBy { get; private set; }

public string LocalAuthority { get; private set; }

public DateTime? LastModifiedOn { get; private set; }

public string? LastModifiedBy { get; private set; }

public string? AssignedAdviserFullName { get; private set; }

public string? AssignedAdviserEmailAddress { get; private set; }


public IEnumerable<SupportProjectNote> Notes => _notes.AsReadOnly();

private readonly List<SupportProjectNote> _notes = new();
public static SupportProject Create(
string schoolName,
string schoolUrn,
Expand All @@ -70,4 +74,18 @@ public void SetAdviser(string assignedAdviserFullName, string assignedAdviserEma
AssignedAdviserFullName = assignedAdviserFullName;
AssignedAdviserEmailAddress = assignedAdviserEmailAddress;
}

public void AddNote(SupportProjectNoteId id, string note, string author, DateTime date, SupportProjectId supportProjectId)
{
_notes.Add(new SupportProjectNote(id, note, author, date,supportProjectId));
}

public void EditSupportProjectNote(SupportProjectNoteId id, string note, string author, DateTime date)
{
var noteToUpdate = _notes.SingleOrDefault(x => x.Id == id);
if (noteToUpdate != null)
{
noteToUpdate.SetNote(note, author, date);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Dfe.RegionalImprovementForStandardsAndExcellence.Domain.Common;
using Dfe.RegionalImprovementForStandardsAndExcellence.Domain.ValueObjects;

namespace Dfe.RegionalImprovementForStandardsAndExcellence.Domain.Entities.SupportProject;

public class SupportProjectNote : BaseAggregateRoot, IEntity<SupportProjectNoteId>
{
private SupportProjectNote() { }

Check warning on line 8 in src/Dfe.RegionalImprovementForStandardsAndExcellence.Domain/Entities/SupportProject/SupportProjectNote.cs

View workflow job for this annotation

GitHub Actions / Build, Test and Analyse

Non-nullable property 'SupportProjectId' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 8 in src/Dfe.RegionalImprovementForStandardsAndExcellence.Domain/Entities/SupportProject/SupportProjectNote.cs

View workflow job for this annotation

GitHub Actions / Build, Test and Analyse

Non-nullable property 'Id' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 8 in src/Dfe.RegionalImprovementForStandardsAndExcellence.Domain/Entities/SupportProject/SupportProjectNote.cs

View workflow job for this annotation

GitHub Actions / Build, Test and Analyse

Non-nullable property 'Note' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

Check warning on line 8 in src/Dfe.RegionalImprovementForStandardsAndExcellence.Domain/Entities/SupportProject/SupportProjectNote.cs

View workflow job for this annotation

GitHub Actions / Build, Test and Analyse

Non-nullable property 'CreatedBy' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

public SupportProjectNote(SupportProjectNoteId id,
string note,
string author,
DateTime date, SupportProjectId supportProjectId)
{
Id = id;
Note = note;
CreatedBy = author;
CreatedOn = date;
SupportProjectId = supportProjectId;
}

public SupportProjectId SupportProjectId { get; private set; }
public SupportProjectNoteId Id { get; private set; }
public string Note { get; private set; }
public DateTime CreatedOn { get; private set; }
public string CreatedBy { get; private set; }

public DateTime? LastModifiedOn { get; private set; }

public string? LastModifiedBy { get; private set; }

public void SetNote(string note, string author, DateTime dateUpdated)
{
Note = note;
LastModifiedBy = author;
LastModifiedOn = dateUpdated;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Dfe.RegionalImprovementForStandardsAndExcellence.Domain.Entities.SupportProject;
using Dfe.RegionalImprovementForStandardsAndExcellence.Domain.ValueObjects;


namespace Dfe.RegionalImprovementForStandardsAndExcellence.Domain.Interfaces.Repositories
Expand All @@ -17,5 +18,7 @@ public interface ISupportProjectRepository : IRepository<SupportProject>

Task<IEnumerable<string>> GetAllProjectRegions(CancellationToken cancellationToken);
Task<IEnumerable<string>> GetAllProjectLocalAuthorities(CancellationToken cancellationToken);

Task<SupportProject> GetSupportProjectById(SupportProjectId id, CancellationToken cancellationToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using Dfe.RegionalImprovementForStandardsAndExcellence.Domain.Common;

namespace Dfe.RegionalImprovementForStandardsAndExcellence.Domain.ValueObjects
{
public record SupportProjectNoteId(Guid Value) : IStronglyTypedId;

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ public RegionalImprovementForStandardsAndExcellenceContext(DbContextOptions<Regi
_mediator = mediator;
}
public DbSet<SupportProject> SupportProjects { get; set; } = null!;

public DbSet<SupportProjectNote> ProjectNotes { get; set; } = null!;

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
Expand All @@ -37,6 +39,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<SupportProject>(ConfigureSupportProject);
modelBuilder.Entity<SupportProjectNote>(ConfigureSupportProjectNotes);

base.OnModelCreating(modelBuilder);
}
Expand All @@ -50,6 +53,22 @@ private static void ConfigureSupportProject(EntityTypeBuilder<SupportProject> su
.HasConversion(
v => v!.Value,
v => new SupportProjectId(v));

supportProjectConfiguration
.HasMany(a => a.Notes)
.WithOne()
.HasForeignKey("SupportProjectId")
.IsRequired();
}

private static void ConfigureSupportProjectNotes(EntityTypeBuilder<SupportProjectNote> supportProjectNoteConfiguration)
{
supportProjectNoteConfiguration.ToTable("SupportProjectNotes", DefaultSchema, b => b.IsTemporal());
supportProjectNoteConfiguration.HasKey(a => a.Id);
supportProjectNoteConfiguration.Property(e => e.Id)
.HasConversion(
v => v!.Value,
v => new SupportProjectNoteId(v));
}

}
Loading

0 comments on commit f4c02b8

Please sign in to comment.