Example #1
0
        public Task <Unit> Handle(UpdateRestaurantCommand message, CancellationToken cancellationToken)
        {
            if (!message.IsValid())
            {
                NotifyValidationErrors(message);
                return(Unit.Task);
            }

            var restaurant         = new Restaurant(message.Id, message.Name, message.Description, message.IsActive, message.ExpertiseId, message.Address);
            var existingRestaurant = _restaurantRepository.GetByAddress(restaurant.Address.Street, restaurant.Address.Number).Result;

            if (existingRestaurant != null && existingRestaurant.Id != restaurant.Id)
            {
                if (!existingRestaurant.Equals(restaurant))
                {
                    Bus.RaiseEvent(new DomainNotification(message.MessageType, "Já existe um restaurante com este nome"));
                    return(Unit.Task);
                }
            }

            _restaurantRepository.Update(restaurant);

            if (Commit())
            {
                Bus.RaiseEvent(new RestaurantUpdatedEvent(restaurant.Id, restaurant.Name, restaurant.Description, restaurant.IsActive, restaurant.ExpertiseId));
            }

            return(Unit.Task);
        }
 public ActionResult <RestaurantDto> PutRestaurant([FromBody] RestaurantDto restaurant)
 {
     try
     {
         UpdateRestaurantCommand updateRestaurantCommand = CommandFactory.CreateUpdateRestaurantCommand(restaurant);
         updateRestaurantCommand.Execute();
         RestaurantDto updatedRestaurant = updateRestaurantCommand.GetResult();
         _logger?.LogInformation($"Restaurante con ID {updatedRestaurant.Id} actualizado correctamente");
         return(updatedRestaurant);
     }
     catch (RestaurantNotFoundExeption e)
     {
         _logger?.LogWarning($"Restaurant con ID {restaurant.Id} no conseguido");
         return(NotFound(new ErrorMessage(e.Message)));
     }
     catch (InvalidAttributeException e)
     {
         _logger?.LogError(e, "Error en campos de la entidad restaurante");
         return(BadRequest(new ErrorMessage(e.Message)));
     }catch (DatabaseException ex)
     {
         _logger?.LogError(ex, "Database exception when trying to update a restaurant");
         return(StatusCode(500, ex.Message));
     }
 }
Example #3
0
        public Task <HttpResponseMessage> Update([FromBody] dynamic body)
        {
            var command = new UpdateRestaurantCommand(
                restaurantId: (int)body.restaurantId,
                restaurantName: (string)body.restaurantName
                );

            var restaurant = _service.Update(command);

            return(CreateResponse(HttpStatusCode.Created, restaurant));
        }
        public _Restaurant Update(UpdateRestaurantCommand command)
        {
            var restaurant = _repository.GetById(command.RestaurantId);

            if (restaurant == null)
            {
                return(null);
            }

            restaurant.Update(command.RestaurantName);
            _repository.Update(restaurant);

            if (Commit())
            {
                return(restaurant);
            }

            return(null);
        }
        public async Task <IActionResult> Update([FromBody] UpdateRestaurantCommand command)
        {
            await Mediator.Send(command);

            return(NoContent());
        }