Exemple #1
0
        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 <IActionResult> Put(int id, [FromBody] SuperheroUpdateDTO superhero)
        {
            if (id != superhero.Id)
            {
                ModelState.AddModelError("id", "id in URL must match id in body");

                return(BadRequest(ModelState));
            }

            var response = await _repository.Update(superhero);

            return(new StatusCodeResult((int)response));
        }
Exemple #3
0
 public void Update(string name, Superhero superhero)
 {
     _superheroRepository.Update(name, superhero);
     InvalidateCache();
 }
        public async Task <IActionResult> Put(int id, [FromBody] SuperheroUpdateDTO superhero)
        {
            var response = await _repository.Update(superhero);

            return(new StatusCodeResult((int)response));
        }