Skip to content

Commit

Permalink
feat: added AnnouncementRecipient + Migration + Handler update
Browse files Browse the repository at this point in the history
  • Loading branch information
ericbrunner committed Jan 10, 2025
1 parent 25d9bc8 commit 095dd29
Show file tree
Hide file tree
Showing 13 changed files with 427 additions and 33 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<ItemGroup>
<ProjectReference Include="..\..\..\..\BuildingBlocks\src\BuildingBlocks.Application\BuildingBlocks.Application.csproj" />
<ProjectReference Include="..\..\..\Devices\src\Devices.Application\Devices.Application.csproj" />
<ProjectReference Include="..\Announcements.Domain\Announcements.Domain.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,60 @@
using Backbone.Modules.Announcements.Application.Announcements.DTOs;
using Backbone.Modules.Announcements.Application.Infrastructure.Persistence.Repository;
using Backbone.Modules.Announcements.Domain.Entities;
using Backbone.Modules.Devices.Application.Infrastructure.Persistence.Repository;
using Backbone.Modules.Devices.Domain.Entities.Identities;
using MediatR;
using Microsoft.Extensions.Logging;

namespace Backbone.Modules.Announcements.Application.Announcements.Commands.CreateAnnouncement;

public class Handler : IRequestHandler<CreateAnnouncementCommand, AnnouncementDTO>
{
private readonly IAnnouncementRecipientsRepository _announcementRecipientRepository;
private readonly IAnnouncementsRepository _announcementsRepository;
private readonly IIdentitiesRepository _identityRepository;
private readonly ILogger<Handler> _logger;

public Handler(IAnnouncementsRepository announcementsRepository)
public Handler(IAnnouncementsRepository announcementsRepository, ILogger<Handler> logger, IIdentitiesRepository identityRepository,
IAnnouncementRecipientsRepository announcementRecipientRepository)
{
_announcementsRepository = announcementsRepository;
_logger = logger;
_identityRepository = identityRepository;
_announcementRecipientRepository = announcementRecipientRepository;
}

public async Task<AnnouncementDTO> Handle(CreateAnnouncementCommand request, CancellationToken cancellationToken)
{
List<AnnouncementRecipient> announcementRecipients = [];

if (request.Recipients != null && request.Recipients.Count != 0)
{
var requestRecipients = request.Recipients.OrderBy(address => address).ToList();

var recipients = (await _identityRepository.Find(Identity.ContainsAddressValue(requestRecipients), cancellationToken)).ToArray();
var foundRecipients = recipients.Select(r => r.Address.Value).OrderBy(address => address).ToList();

if (!foundRecipients.SequenceEqual(requestRecipients))
{
_logger.LogError("Not all recipients were found in the database. \r\n" +
"Request Recipients: {recipients}. \r\n" +
"Found Recipients: {foundRecipients}",
string.Join(',', requestRecipients),
string.Join(',', foundRecipients));
}

announcementRecipients = requestRecipients.Select(address => new AnnouncementRecipient(address)).ToList();

foreach (var announcementRecipient in announcementRecipients)
{
await _announcementRecipientRepository.Add(announcementRecipient, cancellationToken);
}
}

var texts = request.Texts.Select(t => new AnnouncementText(AnnouncementLanguage.Parse(t.Language), t.Title, t.Body)).ToList();

var announcement = new Announcement(request.Severity, texts, request.ExpiresAt);
var announcement = new Announcement(request.Severity, texts, request.ExpiresAt, announcementRecipients);

await _announcementsRepository.Add(announcement, cancellationToken);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Backbone.Modules.Announcements.Domain.Entities;

namespace Backbone.Modules.Announcements.Application.Infrastructure.Persistence.Repository;

public interface IAnnouncementRecipientsRepository
{
Task Add(AnnouncementRecipient announcementRecipient, CancellationToken cancellationToken);
Task<List<AnnouncementRecipient>> FindAll(CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@ private Announcement()
// This constructor is for EF Core only; initializing the properties with null is therefore not a problem
Id = null!;
Texts = null!;
Recipients = null!;
}

public Announcement(AnnouncementSeverity severity, List<AnnouncementText> texts, DateTime? expiresAt)
public Announcement(AnnouncementSeverity severity, List<AnnouncementText> texts, DateTime? expiresAt, List<AnnouncementRecipient> recipients)
{
Id = AnnouncementId.New();
CreatedAt = SystemTime.UtcNow;
ExpiresAt = expiresAt;
Severity = severity;
Texts = texts;
Recipients = recipients;

RaiseDomainEvent(new AnnouncementCreatedDomainEvent(this));
}
Expand All @@ -31,6 +33,8 @@ public Announcement(AnnouncementSeverity severity, List<AnnouncementText> texts,
public AnnouncementSeverity Severity { get; }

public List<AnnouncementText> Texts { get; }

public List<AnnouncementRecipient> Recipients { get; }
}

public enum AnnouncementSeverity
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Backbone.BuildingBlocks.Domain;
using Backbone.Tooling;

namespace Backbone.Modules.Announcements.Domain.Entities;

public class AnnouncementRecipient : Entity
{
// ReSharper disable once UnusedMember.Local
public AnnouncementRecipient()
{
// This constructor is for EF Core only; initializing the properties with null is therefore not a problem
Id = null!;
AnnouncementId = null!;
Address = null!;
}

public AnnouncementRecipient(string address)
{
Id = null!; // will be set by EF Core (primary key)
AnnouncementId = null!; // will be set by EF Core (back navigation property)
Address = address;
CreatedAt = SystemTime.UtcNow;
}

public string Id { get; set; }
public AnnouncementId AnnouncementId { get; }
public string Address { get; set; }
public DateTime CreatedAt { get; set; }
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 095dd29

Please sign in to comment.