Beispiel #1
0
        public ICommandResult Handle(UpdateCondominiumCommand command)
        {
            // Fail Fast Validation
            command.Validate();
            if (command.Invalid)
            {
                return(new GenericCommandResult(false, "Ops, erro ao atualizar condomínio.", command.Notifications));
            }

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

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

            condominium.Update(command.Name, command.Street, command.Number, command.Neighborhood, command.City, command.State, command.Country, command.ZipCode);

            try
            {
                // Atualiza 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 atualizado com sucesso!", condominiumResult));
        }
Beispiel #2
0
        public ICommandResult Handle(CreateApartmentCommand command)
        {
            // Fail Fast Validation
            command.Validate();
            if (command.Invalid)
            {
                return(new GenericCommandResult(false, "Ops, erro ao cadastrar apartamento.", command.Notifications));
            }
            try
            {
                // Recupera condomínio
                var condominium = _condominiumRepository.GetById(command.CondominiumId.Value, command.User);
                if (condominium == null)
                {
                    return(new GenericCommandResult(false, "Ops, erro ao cadastrar apartamento.", "Condomínio não encontrado"));
                }

                // Cria apartamento
                var apartment = new Apartment(condominium, command.Number, command.Block, command.User);

                if (command.Residents.Count > 0)
                {
                    foreach (var resident in command.Residents)
                    {
                        var res = _residentRepository.GetById(resident, command.User);
                        apartment.AddResident(res);
                    }
                }

                // Salva morador
                _repository.Create(apartment);

                var apartmentResult = new ApartmentCommandResult
                {
                    Block = apartment.Block,
                    //Condominium = apartment.Condominium.Name,
                    Number = apartment.Number,
                    User   = apartment.User
                };

                return(new GenericCommandResult(true, "Apartamento salvo com sucesso!", apartmentResult));
            }
            catch (Exception ex)
            {
                return(new GenericCommandResult(false, "Erro inesperado!", ex.Message));
            }
        }
Beispiel #3
0
 public Condominium Get(int id)
 {
     return(_condominiumRepository.GetById(id));
 }