예제 #1
0
        public ICommandResult Handle(CreateResidentCommand command)
        {
            // Fail Fast Validation
            command.Validate();
            if (command.Invalid)
            {
                return(new GenericCommandResult(false, "Ops, erro ao cadastrar morador.", command.Notifications));
            }

            // Recupera apartamento se informado
            Apartment apartment = null;

            if (command.ApartmentId != null && command.ApartmentId != Guid.Empty)
            {
                apartment = _apartmentRepository.GetById(command.ApartmentId, command.User);
            }

            // Cria morador
            var resident = new Resident(apartment, command.Name, command.BirthDate, command.Phone, command.Cpf, command.Email, command.User);

            try
            {
                // Salva morador
                _repository.Create(resident);
            }
            catch (Exception ex)
            {
                return(new GenericCommandResult(false, "Erro inesperado!", ex.Message));
            }

            var residentResult = new ResidentCommandResult
            {
                Name      = resident.Name,
                BirthDate = resident.BirthDate,
                Cpf       = resident.Cpf,
                Email     = resident.Email,
                Phone     = resident.Phone,
                User      = resident.User
            };

            return(new GenericCommandResult(true, "Morador salvo com sucesso!", residentResult));
        }