Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #859 from bacongobbler/fix-848
Browse files Browse the repository at this point in the history
Fix domain event firing for channel deleted events
  • Loading branch information
bacongobbler authored Jun 13, 2022
2 parents e1fdbe6 + 2da2f18 commit e83bda6
Show file tree
Hide file tree
Showing 62 changed files with 336 additions and 346 deletions.
3 changes: 3 additions & 0 deletions src/Application/Apps/Commands/CreateAppCommand.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Hippo.Application.Common.Interfaces;
using Hippo.Core.Entities;
using Hippo.Core.Events;
using MediatR;
using System.ComponentModel.DataAnnotations;

Expand Down Expand Up @@ -31,6 +32,8 @@ public async Task<Guid> Handle(CreateAppCommand request, CancellationToken cance
StorageId = request.StorageId
};

entity.AddDomainEvent(new CreatedEvent<App>(entity));

_context.Apps.Add(entity);

await _context.SaveChangesAsync(cancellationToken);
Expand Down
6 changes: 5 additions & 1 deletion src/Application/Apps/Commands/DeleteAppCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Hippo.Application.Common.Exceptions;
using Hippo.Application.Common.Interfaces;
using Hippo.Core.Entities;
using Hippo.Core.Events;
using MediatR;
using Microsoft.EntityFrameworkCore;

Expand All @@ -25,14 +26,17 @@ public DeleteAppCommandHandler(IApplicationDbContext context)
public async Task<Unit> Handle(DeleteAppCommand request, CancellationToken cancellationToken)
{
var entity = await _context.Apps
.Where(l => l.Id == request.Id)
.Where(a => a.Id == request.Id)
.Include(a => a.Channels)
.SingleOrDefaultAsync(cancellationToken);

if (entity is null)
{
throw new NotFoundException(nameof(App), request.Id);
}

entity.AddDomainEvent(new DeletedEvent<App>(entity));

_context.Apps.Remove(entity);

await _context.SaveChangesAsync(cancellationToken);
Expand Down
5 changes: 3 additions & 2 deletions src/Application/Apps/Commands/PurgeAppsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ public PurgeAppsCommandHandler(IApplicationDbContext context)

public async Task<Unit> Handle(PurgeAppsCommand request, CancellationToken cancellationToken)
{
foreach (App app in _context.Apps)
foreach (var entity in _context.Apps)
{
app.DomainEvents.Add(new DeletedEvent<App>(app));
entity.AddDomainEvent(new DeletedEvent<App>(entity));
}

_context.Apps.RemoveRange(_context.Apps);

await _context.SaveChangesAsync(cancellationToken);
Expand Down
3 changes: 3 additions & 0 deletions src/Application/Apps/Commands/UpdateAppCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Hippo.Application.Common.Exceptions;
using Hippo.Application.Common.Interfaces;
using Hippo.Core.Entities;
using Hippo.Core.Events;
using MediatR;

namespace Hippo.Application.Apps.Commands;
Expand Down Expand Up @@ -40,6 +41,8 @@ public async Task<Unit> Handle(UpdateAppCommand request, CancellationToken cance
entity.Name = request.Name;
entity.StorageId = request.StorageId;

entity.AddDomainEvent(new ModifiedEvent<App>(entity));

await _context.SaveChangesAsync(cancellationToken);

return Unit.Value;
Expand Down
9 changes: 3 additions & 6 deletions src/Application/Apps/EventHandlers/AppCreatedEventHandler.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using Hippo.Application.Common.Models;
using Hippo.Core.Entities;
using Hippo.Core.Events;
using MediatR;
using Microsoft.Extensions.Logging;

namespace Hippo.Application.Apps.EventHandlers;

public class AppCreatedEventHandler : INotificationHandler<DomainEventNotification<CreatedEvent<App>>>
public class AppCreatedEventHandler : INotificationHandler<CreatedEvent<App>>
{
private readonly ILogger<AppCreatedEventHandler> _logger;

Expand All @@ -15,11 +14,9 @@ public AppCreatedEventHandler(ILogger<AppCreatedEventHandler> logger)
_logger = logger;
}

public Task Handle(DomainEventNotification<CreatedEvent<App>> notification, CancellationToken cancellationToken)
public Task Handle(CreatedEvent<App> notification, CancellationToken cancellationToken)
{
var domainEvent = notification.DomainEvent;

_logger.LogInformation("Hippo Domain Event: {DomainEvent}", domainEvent.GetType().Name);
_logger.LogInformation("Hippo Domain Event: {DomainEvent}", notification.GetType().Name);

return Task.CompletedTask;
}
Expand Down
27 changes: 21 additions & 6 deletions src/Application/Apps/EventHandlers/AppDeletedEventHandler.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,40 @@
using Hippo.Application.Common.Models;
using Hippo.Application.Common.Exceptions;
using Hippo.Application.Common.Interfaces;
using Hippo.Core.Entities;
using Hippo.Core.Events;
using MediatR;
using Microsoft.Extensions.Logging;

namespace Hippo.Application.Apps.EventHandlers;

public class AppDeletedEventHandler : INotificationHandler<DomainEventNotification<DeletedEvent<App>>>
public class AppDeletedEventHandler : INotificationHandler<DeletedEvent<App>>
{
private readonly ILogger<AppDeletedEventHandler> _logger;

public AppDeletedEventHandler(ILogger<AppDeletedEventHandler> logger)
private readonly IJobService _jobService;

public AppDeletedEventHandler(ILogger<AppDeletedEventHandler> logger, IJobService jobService)
{
_logger = logger;
_jobService = jobService;
}

public Task Handle(DomainEventNotification<DeletedEvent<App>> notification, CancellationToken cancellationToken)
public Task Handle(DeletedEvent<App> notification, CancellationToken cancellationToken)
{
var domainEvent = notification.DomainEvent;
_logger.LogInformation($"Hippo Domain Event: {notification.GetType().Name}");

_logger.LogInformation("Hippo Domain Event: {DomainEvent}", domainEvent.GetType().Name);
foreach (var channel in notification.Entity.Channels)
{
try
{
_jobService.DeleteJob(jobName: channel.Id.ToString());
}
catch (JobFailedException e)
{
_logger.LogError(e.Message);
throw;
}
}

return Task.CompletedTask;
}
Expand Down
15 changes: 5 additions & 10 deletions src/Application/Apps/EventHandlers/InitialRevisionImport.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using Hippo.Application.Common.Models;
using Hippo.Application.Revisions.Commands;
using Hippo.Core.Entities;
using Hippo.Core.Events;
Expand All @@ -7,7 +6,7 @@

namespace Hippo.Application.Apps.EventHandlers;

public class InitialRevisionImport : INotificationHandler<DomainEventNotification<CreatedEvent<App>>>
public class InitialRevisionImport : INotificationHandler<CreatedEvent<App>>
{
private readonly ILogger<InitialRevisionImport> _logger;
private readonly IMediator _mediator;
Expand All @@ -18,18 +17,14 @@ public InitialRevisionImport(ILogger<InitialRevisionImport> logger, IMediator me
_mediator = mediator;
}

public async Task Handle(DomainEventNotification<CreatedEvent<App>> notification, CancellationToken cancellationToken)
public async Task Handle(CreatedEvent<App> notification, CancellationToken cancellationToken)
{
var domainEvent = notification.DomainEvent;
var app = domainEvent.Entity;
var app = notification.Entity;

var command = new ImportRevisionsCommand
{
AppId = app.Id,
};
var command = new ImportRevisionsCommand(app);

await _mediator.Send(command, cancellationToken);

_logger.LogInformation("Hippo Domain Event: {DomainEvent}", domainEvent.GetType().Name);
_logger.LogInformation($"Hippo Domain Event: {notification.GetType().Name}");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.ComponentModel.DataAnnotations;
using Hippo.Application.Common.Interfaces;
using Hippo.Core.Entities;
using Hippo.Core.Events;
using MediatR;

namespace Hippo.Application.Certificates.Commands;
Expand Down Expand Up @@ -35,6 +36,8 @@ public async Task<Guid> Handle(CreateCertificateCommand request, CancellationTok
PrivateKey = request.PrivateKey,
};

entity.AddDomainEvent(new CreatedEvent<Certificate>(entity));

_context.Certificates.Add(entity);

await _context.SaveChangesAsync(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Hippo.Application.Common.Exceptions;
using Hippo.Application.Common.Interfaces;
using Hippo.Core.Entities;
using Hippo.Core.Events;
using MediatR;
using Microsoft.EntityFrameworkCore;

Expand Down Expand Up @@ -33,6 +34,8 @@ public async Task<Unit> Handle(DeleteCertificateCommand request, CancellationTok
throw new NotFoundException(nameof(Certificate), request.Id);
}

entity.AddDomainEvent(new DeletedEvent<Certificate>(entity));

_context.Certificates.Remove(entity);

await _context.SaveChangesAsync(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ public PurgeCertificatesCommandHandler(IApplicationDbContext context)

public async Task<Unit> Handle(PurgeCertificatesCommand request, CancellationToken cancellationToken)
{
foreach (Certificate cert in _context.Certificates)
foreach (var entity in _context.Certificates)
{
cert.DomainEvents.Add(new DeletedEvent<Certificate>(cert));
entity.AddDomainEvent(new DeletedEvent<Certificate>(entity));
}

_context.Certificates.RemoveRange(_context.Certificates);

await _context.SaveChangesAsync(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Hippo.Application.Common.Exceptions;
using Hippo.Application.Common.Interfaces;
using Hippo.Core.Entities;
using Hippo.Core.Events;
using MediatR;

namespace Hippo.Application.Certificates.Commands;
Expand Down Expand Up @@ -44,6 +45,8 @@ public async Task<Unit> Handle(UpdateCertificateCommand request, CancellationTok
entity.PublicKey = request.PublicKey;
entity.PrivateKey = request.PrivateKey;

entity.AddDomainEvent(new ModifiedEvent<Certificate>(entity));

await _context.SaveChangesAsync(cancellationToken);

return Unit.Value;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using Hippo.Application.Common.Models;
using Hippo.Core.Entities;
using Hippo.Core.Events;
using MediatR;
using Microsoft.Extensions.Logging;

namespace Hippo.Application.Certificates.EventHandlers;

public class CertificateCreatedEventHandler : INotificationHandler<DomainEventNotification<CreatedEvent<Certificate>>>
public class CertificateCreatedEventHandler : INotificationHandler<CreatedEvent<Certificate>>
{
private readonly ILogger<CertificateCreatedEventHandler> _logger;

Expand All @@ -15,11 +14,9 @@ public CertificateCreatedEventHandler(ILogger<CertificateCreatedEventHandler> lo
_logger = logger;
}

public Task Handle(DomainEventNotification<CreatedEvent<Certificate>> notification, CancellationToken cancellationToken)
public Task Handle(CreatedEvent<Certificate> notification, CancellationToken cancellationToken)
{
var domainEvent = notification.DomainEvent;

_logger.LogInformation("Hippo Domain Event: {DomainEvent}", domainEvent.GetType().Name);
_logger.LogInformation($"Hippo Domain Event: {notification.GetType().Name}");

return Task.CompletedTask;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using Hippo.Application.Common.Models;
using Hippo.Core.Entities;
using Hippo.Core.Events;
using MediatR;
using Microsoft.Extensions.Logging;

namespace Hippo.Application.Certificates.EventHandlers;

public class CertificateDeletedEventHandler : INotificationHandler<DomainEventNotification<DeletedEvent<Certificate>>>
public class CertificateDeletedEventHandler : INotificationHandler<DeletedEvent<Certificate>>
{
private readonly ILogger<CertificateDeletedEventHandler> _logger;

Expand All @@ -15,11 +14,9 @@ public CertificateDeletedEventHandler(ILogger<CertificateDeletedEventHandler> lo
_logger = logger;
}

public Task Handle(DomainEventNotification<DeletedEvent<Certificate>> notification, CancellationToken cancellationToken)
public Task Handle(DeletedEvent<Certificate> notification, CancellationToken cancellationToken)
{
var domainEvent = notification.DomainEvent;

_logger.LogInformation("Hippo Domain Event: {DomainEvent}", domainEvent.GetType().Name);
_logger.LogInformation($"Hippo Domain Event: {notification.GetType().Name}");

return Task.CompletedTask;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Application/Channels/Commands/CreateChannelCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Hippo.Application.Rules;
using Hippo.Core.Entities;
using Hippo.Core.Enums;
using Hippo.Core.Events;
using MediatR;
using Microsoft.EntityFrameworkCore;

Expand Down Expand Up @@ -82,6 +83,8 @@ public async Task<Guid> Handle(CreateChannelCommand request, CancellationToken c

entity.ActiveRevision = await GetActiveRevision(request, entity, cancellationToken);

entity.AddDomainEvent(new CreatedEvent<Channel>(entity));

_context.Channels.Add(entity);

await _context.SaveChangesAsync(cancellationToken);
Expand Down
3 changes: 3 additions & 0 deletions src/Application/Channels/Commands/DeleteChannelCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Hippo.Application.Common.Exceptions;
using Hippo.Application.Common.Interfaces;
using Hippo.Core.Entities;
using Hippo.Core.Events;
using MediatR;
using Microsoft.EntityFrameworkCore;

Expand Down Expand Up @@ -33,6 +34,8 @@ public async Task<Unit> Handle(DeleteChannelCommand request, CancellationToken c
throw new NotFoundException(nameof(Channel), request.Id);
}

entity.AddDomainEvent(new DeletedEvent<Channel>(entity));

_context.Channels.Remove(entity);

await _context.SaveChangesAsync(cancellationToken);
Expand Down
5 changes: 3 additions & 2 deletions src/Application/Channels/Commands/PurgeChannelsCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ public PurgeChannelsCommandHandler(IApplicationDbContext context)

public async Task<Unit> Handle(PurgeChannelsCommand request, CancellationToken cancellationToken)
{
foreach (Channel channel in _context.Channels)
foreach (var entity in _context.Channels)
{
channel.DomainEvents.Add(new DeletedEvent<Channel>(channel));
entity.AddDomainEvent(new DeletedEvent<Channel>(entity));
}

_context.Channels.RemoveRange(_context.Channels);

await _context.SaveChangesAsync(cancellationToken);
Expand Down
3 changes: 3 additions & 0 deletions src/Application/Channels/Commands/UpdateChannelCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Hippo.Application.Common.Interfaces;
using Hippo.Core.Entities;
using Hippo.Core.Enums;
using Hippo.Core.Events;
using MediatR;
using Microsoft.EntityFrameworkCore;

Expand Down Expand Up @@ -62,6 +63,8 @@ public async Task<Unit> Handle(UpdateChannelCommand request, CancellationToken c
entity.RangeRule = "*";
}

entity.AddDomainEvent(new ModifiedEvent<Channel>(entity));

await _context.SaveChangesAsync(cancellationToken);

return Unit.Value;
Expand Down
Loading

0 comments on commit e83bda6

Please sign in to comment.