Skip to content

Commit

Permalink
feat(weapons): assign weapon holder on 'WarMaterialDeliveryCertificat…
Browse files Browse the repository at this point in the history
…eFormat' created
  • Loading branch information
CarlosPavajeau committed Sep 30, 2021
1 parent e2dc588 commit f21ef17
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public AssignWeaponHolderCommandHandler(WeaponHolderAssigner assigner)

protected override async Task Handle(AssignWeaponHolderCommand request, CancellationToken cancellationToken)
{
await _assigner.AssignOwner(request.WeaponSerial, request.TroopId);
await _assigner.AssignHolder(request.WeaponSerial, request.TroopId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Threading;
using System.Threading.Tasks;
using Armory.Shared.Domain.Bus.Event;
using Armory.Shared.Domain.Formats.WarMaterialDeliveryCertificateFormats;

namespace Armory.Armament.Weapons.Application.AssignHolder
{
public class
AssignWeaponHolderOnWarMaterialDeliveryCertificateFormatCreated : IDomainEventSubscriber<
WarMaterialDeliveryCertificateFormatCreatedDomainEvent>
{
private readonly WeaponHolderAssigner _assigner;

public AssignWeaponHolderOnWarMaterialDeliveryCertificateFormatCreated(WeaponHolderAssigner assigner)
{
_assigner = assigner;
}

public async Task Handle(WarMaterialDeliveryCertificateFormatCreatedDomainEvent notification,
CancellationToken cancellationToken)
{
var holder = notification.TroopId;
foreach (var weapon in notification.Weapons)
{
await _assigner.AssignHolder(weapon, holder);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public WeaponHolderAssigner(IWeaponsRepository repository, IUnitWork unitWork)
_unitWork = unitWork;
}

public async Task AssignOwner(string weaponSerial, string troopId)
public async Task AssignHolder(string weaponSerial, string troopId)
{
var weapon = await _repository.Find(weaponSerial, false);
if (weapon == null) throw new WeaponNotFound();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,17 @@
using System.Threading.Tasks;
using Armory.Formats.Shared.Domain;
using Armory.Formats.WarMaterialAndSpecialEquipmentAssignmentFormats.Domain;
using Armory.Shared.Domain.Bus.Event;
using Armory.Shared.Domain.Persistence.EntityFramework.Transactions;

namespace Armory.Formats.WarMaterialAndSpecialEquipmentAssignmentFormats.Application.Create
{
public class WarMaterialAndSpecialEquipmentAssignmentFormatCreator
{
private readonly IEventBus _eventBus;
private readonly ITransactionInitializer _initializer;
private readonly IWarMaterialAndSpecialEquipmentAssignmentFormatsRepository _repository;

public WarMaterialAndSpecialEquipmentAssignmentFormatCreator(
IWarMaterialAndSpecialEquipmentAssignmentFormatsRepository repository, IEventBus eventBus,
ITransactionInitializer initializer)
IWarMaterialAndSpecialEquipmentAssignmentFormatsRepository repository)
{
_repository = repository;
_eventBus = eventBus;
_initializer = initializer;
}

public async Task<WarMaterialAndSpecialEquipmentAssignmentFormat> Create(
Expand Down Expand Up @@ -57,12 +50,7 @@ public async Task<WarMaterialAndSpecialEquipmentAssignmentFormat> Create(
equipments,
explosives);

var transaction = await _initializer.Begin();

await _repository.Save(format);
await _eventBus.Publish(format.PullDomainEvents());

await transaction.CommitAsync();

return format;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Armory.Formats.WarMaterialDeliveryCertificateFormats.Domain;
using Armory.Shared.Domain.Bus.Event;
using Armory.Shared.Domain.Persistence.EntityFramework.Transactions;

namespace Armory.Formats.WarMaterialDeliveryCertificateFormats.Application.Create
{
public class WarMaterialDeliveryCertificateFormatCreator
{
private readonly IEventBus _eventBus;
private readonly ITransactionInitializer _initializer;
private readonly IWarMaterialDeliveryCertificateFormatsRepository _repository;

public WarMaterialDeliveryCertificateFormatCreator(IWarMaterialDeliveryCertificateFormatsRepository repository)
public WarMaterialDeliveryCertificateFormatCreator(IWarMaterialDeliveryCertificateFormatsRepository repository,
IEventBus eventBus, ITransactionInitializer initializer)
{
_repository = repository;
_eventBus = eventBus;
_initializer = initializer;
}

public async Task<WarMaterialDeliveryCertificateFormat> Create(
Expand Down Expand Up @@ -42,7 +49,12 @@ public async Task<WarMaterialDeliveryCertificateFormat> Create(
equipments,
explosives);

var transaction = await _initializer.Begin();

await _repository.Save(format);
await _eventBus.Publish(format.PullDomainEvents());

await transaction.CommitAsync();

return format;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Armory.Fireteams.Domain;
using Armory.Flights.Domain;
using Armory.Shared.Domain.Aggregate;
using Armory.Shared.Domain.Formats.WarMaterialDeliveryCertificateFormats;
using Armory.Squads.Domain;
using Armory.Troopers.Domain;

Expand Down Expand Up @@ -107,6 +108,15 @@ public static WarMaterialDeliveryCertificateFormat Create(string code, DateTime
WarMaterialDeliveryCertificateFormatExplosive.Create(format, explosiveSerial, quantity));
}

format.Record(new WarMaterialDeliveryCertificateFormatCreatedDomainEvent
{
Weapons = weapons,
Ammunition = ammunition,
Equipments = equipments,
Explosives = explosives,
TroopId = troopId
});

return format;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Collections.Generic;
using Armory.Shared.Domain.Bus.Event;

namespace Armory.Shared.Domain.Formats.WarMaterialDeliveryCertificateFormats
{
public class WarMaterialDeliveryCertificateFormatCreatedDomainEvent : DomainEvent
{
public IEnumerable<string> Weapons { get; init; }
public IDictionary<string, int> Ammunition { get; init; }
public IDictionary<string, int> Equipments { get; init; }
public IDictionary<string, int> Explosives { get; init; }
public string TroopId { get; init; }

public override string EventName()
{
return "war_material_delivery_certificate_format.created";
}
}
}
3 changes: 2 additions & 1 deletion src/Shared/Domain/Bus/Event/DomainEvent.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using MediatR;

namespace Armory.Shared.Domain.Bus.Event
Expand All @@ -11,7 +12,7 @@ protected DomainEvent(string aggregateId, string eventId, string occurredOn)
OccurredOn = occurredOn;
}

protected DomainEvent()
protected DomainEvent() : this(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), DateTime.Now.ToString("d"))
{
}

Expand Down

0 comments on commit f21ef17

Please sign in to comment.