public async Task Handle(UpdateSuperheroCommand notification, CancellationToken cancellationToken) { if (!notification.IsValid()) { NotifyValidationErrors(notification); return; } List <string> validationErrors = new List <string>(); var existingSuperhero = await _superheroRepository.GetByNameAsync(notification.Name); if (existingSuperhero != null && existingSuperhero.Id != notification.Id) { validationErrors.Add("There's already a superhero with this name"); } if (await _protectionAreaRepository.GetAsync(notification.ProtectionAreaId) == null) { validationErrors.Add("Invalid protection area id"); } if (validationErrors.Count > 0) { foreach (var error in validationErrors) { await _bus.RaiseEvent(new DomainNotification(notification.MessageType, error)); } return; } existingSuperhero = await _superheroRepository.GetAsync(notification.Id); var superhero = new Superhero(notification.Id, notification.Name, notification.Alias, notification.ProtectionAreaId, existingSuperhero.DateCreated); _superheroRepository.Update(superhero); if (await CommitAsync()) { var superheroEvent = new SuperheroUpdatedEvent(superhero.Id, superhero.Name, superhero.Alias); await _bus.RaiseEvent(superheroEvent); await _auditEventService.Subscribe(new AuditEvent( Guid.NewGuid(), nameof(Superhero), superhero.Id, notification.Username, AuditEventAction.Update )); } }
public async Task <SuperheroViewModel> GetAsync(Guid id) { var result = await _cache.GetAsync <Superhero>($"superheroGetAsync_{id}", async() => await _superheroRepository.GetAsync(id)); return(_mapper.Map <SuperheroViewModel>(result)); }