public GenericCommandResult AddApartment(
            [FromBody] AddApartmentCommand command,
            [FromServices] CondominiumHandler handler
            )
        {
            command.User = User.Claims.FirstOrDefault(x => x.Type == "user_id")?.Value;
            var result = (GenericCommandResult)handler.Handle(command);

            return(result);
        }
Example #2
0
        public ICommandResult Handle(AddApartmentCommand command)
        {
            // Fail Fast Validation
            command.Validate();
            if (command.Invalid)
            {
                return(new GenericCommandResult(false, "Ops, erro ao adicionar apartamento ao condomínio.", command.Notifications));
            }

            // Recupera apartamento
            var apartment = _apartmentRepository.GetById(command.ApartmentId, command.User);

            if (apartment == null)
            {
                return(new GenericCommandResult(false, "Ops, erro ao adicionar apartamento ao condomínio.", command.ApartmentId));
            }

            // Recupera condomínio
            var condominium = _repository.GetById(command.Id, command.User);

            if (condominium == null)
            {
                return(new GenericCommandResult(false, "Ops, erro ao adicionar apartamento ao condomínio.", command.Id));
            }

            condominium.AddApartment(apartment);

            try
            {
                // Salva condomínio
                _repository.Update(condominium);
            }
            catch (Exception ex)
            {
                return(new GenericCommandResult(false, "Erro inesperado!", ex.Message));
            }

            var condominiumResult = new CondominiumCommandResult
            {
                City         = condominium.City,
                Country      = condominium.Country,
                Name         = condominium.Name,
                Neighborhood = condominium.Neighborhood,
                Number       = condominium.Number,
                State        = condominium.State,
                Street       = condominium.Street
            };

            return(new GenericCommandResult(true, "Condomínio salvo com sucesso!", condominiumResult));
        }