From ad6e12b348321b5e7c3791b2606ab66008b3aaf9 Mon Sep 17 00:00:00 2001 From: Michael Spiegel Date: Wed, 10 Jul 2024 15:34:33 +0200 Subject: [PATCH 1/7] Moved contents of TournamentConfirmedEvent.cs into Tournament.cs and updated related code. --- .../TournamentConfirmedEventHandler.cs | 7 +++-- .../Tournament/TournamentConfirmedEvent.cs | 28 ++---------------- ClubService.Domain/Model/Entity/Tournament.cs | 29 +++++++++++++++++++ .../ReadModel/TournamentReadModel.cs | 10 +++---- 4 files changed, 40 insertions(+), 34 deletions(-) create mode 100644 ClubService.Domain/Model/Entity/Tournament.cs diff --git a/ClubService.Application/EventHandlers/TournamentEventHandlers/TournamentConfirmedEventHandler.cs b/ClubService.Application/EventHandlers/TournamentEventHandlers/TournamentConfirmedEventHandler.cs index 13de8dba..ef4b8e99 100644 --- a/ClubService.Application/EventHandlers/TournamentEventHandlers/TournamentConfirmedEventHandler.cs +++ b/ClubService.Application/EventHandlers/TournamentEventHandlers/TournamentConfirmedEventHandler.cs @@ -27,11 +27,11 @@ public async Task Handle(DomainEnvelope domainEnvelope) var tournamentConfirmedEvent = (TournamentConfirmedEvent)domainEnvelope.EventData; var tennisClubReadModel = - await tennisClubReadModelRepository.GetTennisClubById(tournamentConfirmedEvent.ClubId); + await tennisClubReadModelRepository.GetTennisClubById(tournamentConfirmedEvent.Tournament.ClubId); if (tennisClubReadModel == null) { - loggerService.LogTennisClubNotFound(tournamentConfirmedEvent.ClubId); + loggerService.LogTennisClubNotFound(tournamentConfirmedEvent.Tournament.ClubId); throw new TennisClubNotFoundException(domainEnvelope.EntityId); } @@ -42,7 +42,8 @@ public async Task Handle(DomainEnvelope domainEnvelope) foreach (var member in members) { var emailMessage = new EmailMessage(Guid.NewGuid(), member.Email, - tournamentConfirmedEvent.Name, tournamentConfirmedEvent.Description, DateTime.UtcNow); + tournamentConfirmedEvent.Tournament.Name, tournamentConfirmedEvent.Tournament.Description, + DateTime.UtcNow); await emailOutboxRepository.Add(emailMessage); } diff --git a/ClubService.Domain/Event/Tournament/TournamentConfirmedEvent.cs b/ClubService.Domain/Event/Tournament/TournamentConfirmedEvent.cs index 4a0f87b9..5ef1aadb 100644 --- a/ClubService.Domain/Event/Tournament/TournamentConfirmedEvent.cs +++ b/ClubService.Domain/Event/Tournament/TournamentConfirmedEvent.cs @@ -1,30 +1,6 @@ -using ClubService.Domain.Model.Entity; -using ClubService.Domain.Model.Enum; - namespace ClubService.Domain.Event.Tournament; -public class TournamentConfirmedEvent( - Guid id, - Guid clubId, - bool isCanceled, - string name, - string description, - int maximumParticipants, - HashSet courts, - HashSet days, - HashSet participants, - int version, - TournamentStatus status) : ITournamentDomainEvent +public class TournamentConfirmedEvent(Model.Entity.Tournament tournament) : ITournamentDomainEvent { - public Guid Id { get; } = id; - public Guid ClubId { get; } = clubId; - public bool IsCanceled { get; } = isCanceled; - public string Name { get; } = name; - public string Description { get; } = description; - public int MaximumParticipants { get; } = maximumParticipants; - public HashSet Courts { get; } = courts; - public HashSet Days { get; } = days; - public HashSet Participants { get; } = participants; - public int Version { get; } = version; - public TournamentStatus Status { get; } = status; + public Model.Entity.Tournament Tournament { get; } = tournament; } \ No newline at end of file diff --git a/ClubService.Domain/Model/Entity/Tournament.cs b/ClubService.Domain/Model/Entity/Tournament.cs new file mode 100644 index 00000000..8596b37e --- /dev/null +++ b/ClubService.Domain/Model/Entity/Tournament.cs @@ -0,0 +1,29 @@ +using ClubService.Domain.Model.Enum; + +namespace ClubService.Domain.Model.Entity; + +public class Tournament( + Guid id, + Guid clubId, + bool isCanceled, + string name, + string description, + int maximumParticipants, + HashSet courts, + HashSet days, + HashSet participants, + int version, + TournamentStatus status) +{ + public Guid Id { get; } = id; + public Guid ClubId { get; } = clubId; + public bool IsCanceled { get; } = isCanceled; + public string Name { get; } = name; + public string Description { get; } = description; + public int MaximumParticipants { get; } = maximumParticipants; + public HashSet Courts { get; } = courts; + public HashSet Days { get; } = days; + public HashSet Participants { get; } = participants; + public int Version { get; } = version; + public TournamentStatus Status { get; } = status; +} \ No newline at end of file diff --git a/ClubService.Domain/ReadModel/TournamentReadModel.cs b/ClubService.Domain/ReadModel/TournamentReadModel.cs index 43bda4db..cfa2a9f3 100644 --- a/ClubService.Domain/ReadModel/TournamentReadModel.cs +++ b/ClubService.Domain/ReadModel/TournamentReadModel.cs @@ -21,13 +21,13 @@ private TournamentReadModel(Guid tournamentId, Guid tennisClubId, string name, D public static TournamentReadModel FromDomainEvent(TournamentConfirmedEvent tournamentConfirmedEvent) { - var startDate = tournamentConfirmedEvent.Days.OrderBy(d => d.Day).First(); - var endDate = tournamentConfirmedEvent.Days.OrderBy(d => d.Day).Last(); + var startDate = tournamentConfirmedEvent.Tournament.Days.OrderBy(d => d.Day).First(); + var endDate = tournamentConfirmedEvent.Tournament.Days.OrderBy(d => d.Day).Last(); return new TournamentReadModel( - tournamentConfirmedEvent.Id, - tournamentConfirmedEvent.ClubId, - tournamentConfirmedEvent.Name, + tournamentConfirmedEvent.Tournament.Id, + tournamentConfirmedEvent.Tournament.ClubId, + tournamentConfirmedEvent.Tournament.Name, startDate.Day, endDate.Day ); From 8a89a1147fcd8e14ee26de43b2548d10862e51ef Mon Sep 17 00:00:00 2001 From: Michael Spiegel Date: Wed, 10 Jul 2024 17:14:27 +0200 Subject: [PATCH 2/7] Added ignore case when parsing enum. --- ClubService.Infrastructure/EventHandling/EventParser.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ClubService.Infrastructure/EventHandling/EventParser.cs b/ClubService.Infrastructure/EventHandling/EventParser.cs index 7c9a9803..64dec917 100644 --- a/ClubService.Infrastructure/EventHandling/EventParser.cs +++ b/ClubService.Infrastructure/EventHandling/EventParser.cs @@ -26,8 +26,8 @@ public static DomainEnvelope ParseEvent(JsonElement jsonEvent) var eventId = Guid.Parse(eventIdStr); var entityId = Guid.Parse(entityIdStr); - var eventType = (EventType)Enum.Parse(typeof(EventType), eventTypeStr); - var entityType = (EntityType)Enum.Parse(typeof(EntityType), entityTypeStr); + var eventType = (EventType)Enum.Parse(typeof(EventType), eventTypeStr, true); + var entityType = (EntityType)Enum.Parse(typeof(EntityType), entityTypeStr, true); var timestamp = DateTime.Parse(timestampStr).ToUniversalTime(); var deserializedEventData = EventDeserializer.DeserializeEventData(eventType, eventData); From 6aab78f2d0643e42f47054b833d10db5a301fa9c Mon Sep 17 00:00:00 2001 From: Michael Spiegel Date: Wed, 10 Jul 2024 17:36:52 +0200 Subject: [PATCH 3/7] Updated TournamentStatus.cs to only contain CONFIRMED. --- ClubService.Domain/Model/Enum/TournamentStatus.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/ClubService.Domain/Model/Enum/TournamentStatus.cs b/ClubService.Domain/Model/Enum/TournamentStatus.cs index 8be1ca94..35f72a0d 100644 --- a/ClubService.Domain/Model/Enum/TournamentStatus.cs +++ b/ClubService.Domain/Model/Enum/TournamentStatus.cs @@ -2,8 +2,5 @@ namespace ClubService.Domain.Model.Enum; public enum TournamentStatus { - PENDING, - CREATED, - REJECTED, - CANCELED + CONFIRMED } \ No newline at end of file From af1c978867367511fda1be060b39b766d5403feb Mon Sep 17 00:00:00 2001 From: Michael Spiegel Date: Wed, 10 Jul 2024 18:11:52 +0200 Subject: [PATCH 4/7] Added logging of invalid email address in EmailMessageRelay.cs. --- ClubService.Domain/Repository/ILoggerService.cs | 1 + .../Logging/LoggerService.cs | 5 +++++ .../Mail/EmailMessageRelay.cs | 17 ++++++++++++++--- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/ClubService.Domain/Repository/ILoggerService.cs b/ClubService.Domain/Repository/ILoggerService.cs index 4ea4b739..bc9cac1a 100644 --- a/ClubService.Domain/Repository/ILoggerService.cs +++ b/ClubService.Domain/Repository/ILoggerService.cs @@ -64,4 +64,5 @@ public interface ILoggerService void LogJsonMissingProperties(string jsonValue); void LogEmailMessageRelayStop(); void LogSystemOperatorRegistered(Guid id); + void LogInvalidEMailAddress(string emailAddress); } \ No newline at end of file diff --git a/ClubService.Infrastructure/Logging/LoggerService.cs b/ClubService.Infrastructure/Logging/LoggerService.cs index 27d17399..852fa31f 100644 --- a/ClubService.Infrastructure/Logging/LoggerService.cs +++ b/ClubService.Infrastructure/Logging/LoggerService.cs @@ -320,4 +320,9 @@ public void LogSystemOperatorRegistered(Guid id) { logger.LogInformation("Registered system operator with id '{id}'.", id); } + + public void LogInvalidEMailAddress(string emailAddress) + { + logger.LogError("The following email address is invalid: {emailMessageRecipientEMailAddress}", emailAddress); + } } \ No newline at end of file diff --git a/ClubService.Infrastructure/Mail/EmailMessageRelay.cs b/ClubService.Infrastructure/Mail/EmailMessageRelay.cs index 426b22cf..c0c6af67 100644 --- a/ClubService.Infrastructure/Mail/EmailMessageRelay.cs +++ b/ClubService.Infrastructure/Mail/EmailMessageRelay.cs @@ -12,7 +12,7 @@ public class EmailMessageRelay : BackgroundService { private readonly ILoggerService _loggerService; private readonly int _pollingInterval; - private readonly string _senderEmailAddress; + private readonly MailAddress _senderEmailAddress; private readonly IServiceProvider _serviceProvider; private readonly SmtpClient _smtpClient; @@ -24,7 +24,7 @@ public EmailMessageRelay( _serviceProvider = serviceProvider; _loggerService = loggerService; _pollingInterval = smtpConfiguration.Value.PollingInterval; - _senderEmailAddress = smtpConfiguration.Value.SenderEmailAddress; + _senderEmailAddress = new MailAddress(smtpConfiguration.Value.SenderEmailAddress); _smtpClient = new SmtpClient(smtpConfiguration.Value.Host, smtpConfiguration.Value.Port); _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; } @@ -41,7 +41,18 @@ private async Task SendEmails() { await transactionManager.TransactionScope(async () => { - var mailMessage = new MailMessage(_senderEmailAddress, emailMessage.RecipientEMailAddress) + MailAddress recipientEmailAddress; + try + { + recipientEmailAddress = new MailAddress(emailMessage.RecipientEMailAddress); + } + catch (FormatException e) + { + _loggerService.LogInvalidEMailAddress(emailMessage.RecipientEMailAddress); + throw; + } + + var mailMessage = new MailMessage(_senderEmailAddress, recipientEmailAddress) { Subject = emailMessage.Subject, Body = emailMessage.Body From 4d47481de45f9368e6099f0b3c786605bee676f3 Mon Sep 17 00:00:00 2001 From: Michael Spiegel Date: Wed, 10 Jul 2024 18:19:09 +0200 Subject: [PATCH 5/7] Added more logging to EmailMessageRelay.cs. --- .../Mail/EmailMessageRelay.cs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/ClubService.Infrastructure/Mail/EmailMessageRelay.cs b/ClubService.Infrastructure/Mail/EmailMessageRelay.cs index c0c6af67..cea5bbdf 100644 --- a/ClubService.Infrastructure/Mail/EmailMessageRelay.cs +++ b/ClubService.Infrastructure/Mail/EmailMessageRelay.cs @@ -12,7 +12,7 @@ public class EmailMessageRelay : BackgroundService { private readonly ILoggerService _loggerService; private readonly int _pollingInterval; - private readonly MailAddress _senderEmailAddress; + private readonly string _senderEmailAddress; private readonly IServiceProvider _serviceProvider; private readonly SmtpClient _smtpClient; @@ -24,7 +24,7 @@ public EmailMessageRelay( _serviceProvider = serviceProvider; _loggerService = loggerService; _pollingInterval = smtpConfiguration.Value.PollingInterval; - _senderEmailAddress = new MailAddress(smtpConfiguration.Value.SenderEmailAddress); + _senderEmailAddress = smtpConfiguration.Value.SenderEmailAddress; _smtpClient = new SmtpClient(smtpConfiguration.Value.Host, smtpConfiguration.Value.Port); _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; } @@ -42,17 +42,28 @@ private async Task SendEmails() await transactionManager.TransactionScope(async () => { MailAddress recipientEmailAddress; + MailAddress senderMailAddress; + try + { + senderMailAddress = new MailAddress(_senderEmailAddress); + } + catch (FormatException) + { + _loggerService.LogInvalidEMailAddress(_senderEmailAddress); + throw; + } + try { recipientEmailAddress = new MailAddress(emailMessage.RecipientEMailAddress); } - catch (FormatException e) + catch (FormatException) { _loggerService.LogInvalidEMailAddress(emailMessage.RecipientEMailAddress); throw; } - var mailMessage = new MailMessage(_senderEmailAddress, recipientEmailAddress) + var mailMessage = new MailMessage(senderMailAddress, recipientEmailAddress) { Subject = emailMessage.Subject, Body = emailMessage.Body From cc25df19c8bba0b4cffb8b9af55f0ea5245cd768 Mon Sep 17 00:00:00 2001 From: Michael Spiegel Date: Wed, 10 Jul 2024 18:32:01 +0200 Subject: [PATCH 6/7] Fixed mistake in postgres-secret.yaml where stringData was used instead of data. Moved sender email from mailhog-secret.yaml to mailhog-config.yaml. Removed logging of sender email in EmailMessageRelay.cs. Co-authored-by: Adrian Essig --- .../Mail/EmailMessageRelay.cs | 17 +++-------------- deployment/configs/mailhog-config.yaml | 3 ++- .../deployments/club-service-deployment.yaml | 4 ++-- deployment/secrets/mailhog-secret.yaml | 7 ------- deployment/secrets/postgres-secret.yaml | 2 +- 5 files changed, 8 insertions(+), 25 deletions(-) delete mode 100644 deployment/secrets/mailhog-secret.yaml diff --git a/ClubService.Infrastructure/Mail/EmailMessageRelay.cs b/ClubService.Infrastructure/Mail/EmailMessageRelay.cs index cea5bbdf..f7f7f65f 100644 --- a/ClubService.Infrastructure/Mail/EmailMessageRelay.cs +++ b/ClubService.Infrastructure/Mail/EmailMessageRelay.cs @@ -12,7 +12,7 @@ public class EmailMessageRelay : BackgroundService { private readonly ILoggerService _loggerService; private readonly int _pollingInterval; - private readonly string _senderEmailAddress; + private readonly MailAddress _senderEmailAddress; private readonly IServiceProvider _serviceProvider; private readonly SmtpClient _smtpClient; @@ -24,7 +24,7 @@ public EmailMessageRelay( _serviceProvider = serviceProvider; _loggerService = loggerService; _pollingInterval = smtpConfiguration.Value.PollingInterval; - _senderEmailAddress = smtpConfiguration.Value.SenderEmailAddress; + _senderEmailAddress = new MailAddress(smtpConfiguration.Value.SenderEmailAddress); _smtpClient = new SmtpClient(smtpConfiguration.Value.Host, smtpConfiguration.Value.Port); _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; } @@ -42,17 +42,6 @@ private async Task SendEmails() await transactionManager.TransactionScope(async () => { MailAddress recipientEmailAddress; - MailAddress senderMailAddress; - try - { - senderMailAddress = new MailAddress(_senderEmailAddress); - } - catch (FormatException) - { - _loggerService.LogInvalidEMailAddress(_senderEmailAddress); - throw; - } - try { recipientEmailAddress = new MailAddress(emailMessage.RecipientEMailAddress); @@ -63,7 +52,7 @@ await transactionManager.TransactionScope(async () => throw; } - var mailMessage = new MailMessage(senderMailAddress, recipientEmailAddress) + var mailMessage = new MailMessage(_senderEmailAddress, recipientEmailAddress) { Subject = emailMessage.Subject, Body = emailMessage.Body diff --git a/deployment/configs/mailhog-config.yaml b/deployment/configs/mailhog-config.yaml index 5a7a2c72..8f2bb69c 100644 --- a/deployment/configs/mailhog-config.yaml +++ b/deployment/configs/mailhog-config.yaml @@ -4,4 +4,5 @@ metadata: name: club-service-mailhog-config data: mailhog-host: "club-service-mailhog-service" - mailhog-smtp-port: "1025" \ No newline at end of file + mailhog-smtp-port: "1025" + smtp-sender-email-address: "admin@thc-dornbirn.at" \ No newline at end of file diff --git a/deployment/deployments/club-service-deployment.yaml b/deployment/deployments/club-service-deployment.yaml index 8d210f7c..5222b422 100644 --- a/deployment/deployments/club-service-deployment.yaml +++ b/deployment/deployments/club-service-deployment.yaml @@ -85,6 +85,6 @@ spec: key: mailhog-smtp-port - name: SMTP_SENDER_EMAIL_ADDRESS valueFrom: - secretKeyRef: - name: club-service-mailhog-secret + configMapKeyRef: + name: club-service-mailhog-config key: smtp-sender-email-address \ No newline at end of file diff --git a/deployment/secrets/mailhog-secret.yaml b/deployment/secrets/mailhog-secret.yaml deleted file mode 100644 index 64843f0c..00000000 --- a/deployment/secrets/mailhog-secret.yaml +++ /dev/null @@ -1,7 +0,0 @@ -apiVersion: v1 -kind: Secret -metadata: - name: club-service-mailhog-secret -type: Opaque -stringData: - smtp-sender-email-address: YWRtaW5AdGhjZG9ybmJpcm4uYXQK \ No newline at end of file diff --git a/deployment/secrets/postgres-secret.yaml b/deployment/secrets/postgres-secret.yaml index a954daf8..878502e7 100644 --- a/deployment/secrets/postgres-secret.yaml +++ b/deployment/secrets/postgres-secret.yaml @@ -3,6 +3,6 @@ kind: Secret metadata: name: club-service-postgres-secret type: Opaque -stringData: +data: postgres-user: dXNlcgo= postgres-password: cGFzc3dvcmQK \ No newline at end of file From d66adea2b252426f3344a3f2b3ea9fb001386ee3 Mon Sep 17 00:00:00 2001 From: Michael Spiegel Date: Wed, 10 Jul 2024 18:44:37 +0200 Subject: [PATCH 7/7] Changed postgres-secret.yaml back to stringData --- deployment/secrets/postgres-secret.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deployment/secrets/postgres-secret.yaml b/deployment/secrets/postgres-secret.yaml index 878502e7..a954daf8 100644 --- a/deployment/secrets/postgres-secret.yaml +++ b/deployment/secrets/postgres-secret.yaml @@ -3,6 +3,6 @@ kind: Secret metadata: name: club-service-postgres-secret type: Opaque -data: +stringData: postgres-user: dXNlcgo= postgres-password: cGFzc3dvcmQK \ No newline at end of file