Esempio n. 1
0
 public AddressListVM(Address p)
 {
     this.p = p;
 }
        private void LimpaCampos()
        {
            txtNome.Text = String.Empty;
            txtCPFCNPJ.Text = String.Empty;
            txtLogradouro.Text = String.Empty;
            txtNumero.Text = String.Empty;
            txtBairro.Text = String.Empty;
            txtComplemento.Text = String.Empty;
            dateNascimento.Text = String.Empty;
            radioSexoM.IsChecked = true;
            radioSexoF.IsChecked = false;

            comboNaturalidade.SelectedIndex = -1;
            comboEstadoCivil.SelectedIndex = -1;
            comboEstado.SelectedIndex = -1;
            comboCidade.SelectedIndex = -1;
            comboPais.SelectedIndex = -1;

            dataGrid2.ItemsSource = null;

            _editing = null;
            _editingAddress = null;
        }
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            Client client = null;
            Address address = null;

            address = new Address()
            {
                Id = Database.GetInstance.redisClient.As<Address>().GetNextSequence(),
                Place = txtLogradouro.Text,
                Number = (String.IsNullOrEmpty(txtNumero.Text)) ? 0 : Convert.ToInt32(txtNumero.Text),
                Complement = txtComplemento.Text,
                Neighborhood = txtBairro.Text,
                City = Database.GetInstance.redisClient.As<City>().GetById(comboCidade.SelectedValue),
                State = Database.GetInstance.redisClient.As<State>().GetById(comboEstado.SelectedValue),
                Country = Database.GetInstance.redisClient.As<Country>().GetById(comboPais.SelectedValue),
                LastModification = DateTime.Now
            };

            client = new Client()
            {
                Id = Database.GetInstance.redisClient.As<Client>().GetNextSequence(),
                Name = txtNome.Text,
                CpfCnpj = txtCPFCNPJ.Text,
                Sex = ((bool)radioSexoM.IsChecked) ? radioSexoM.Content.ToString() : radioSexoF.Content.ToString(),
                Birthplace = Database.GetInstance.redisClient.As<State>().GetById(comboNaturalidade.SelectedValue),
                BirthDate = (String.IsNullOrEmpty(dateNascimento.Text)) ? DateTime.MinValue : Convert.ToDateTime(dateNascimento.Text),
                MarriageState = Database.GetInstance.redisClient.As<MarriageState>().GetById(comboEstadoCivil.SelectedValue),
                AddressId = address.Id,
                LastModification = DateTime.Now
            };
            address.IdClient = client.Id;

            if (_editing != null && _editingAddress != null)
            {
                address.Id = _editingAddress.Id;
                client.Id = _editing.Id;
                address.IdClient = _editing.Id;
            }
            else if (_editingAddress == null && _editing != null)
            {
                client.Id = _editing.Id;
                address.IdClient = _editing.Id;
            }

            if (client.Validate() && address.Validate())
            {
                using (var trans = Database.GetInstance.redisClient.CreateTransaction())
                {
                    trans.QueueCommand(r => r.Store<Address>(address));

                    trans.Commit();
                }
                using (var trans = Database.GetInstance.redisClient.CreateTransaction())
                {
                    trans.QueueCommand(r => r.Store<Client>(client));

                    trans.Commit();
                }
                ListaClientes();
                LimpaCampos();
            }
        }
        private void dataGrid2_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var clienteSelecionado = (AddressListVM)dataGrid2.SelectedItem;

            if (clienteSelecionado != null)
            {
                txtLogradouro.Text = clienteSelecionado.Place;
                txtBairro.Text = clienteSelecionado.Neighborhood;
                txtNumero.Text = clienteSelecionado.Number;
                txtComplemento.Text = clienteSelecionado.Complement;

                foreach (ComboBoxItem item in comboEstado.Items)
                    if (item.Content.Equals(clienteSelecionado.State))
                        comboEstado.SelectedItem = item;

                foreach (ComboBoxItem item in comboCidade.Items)
                    if (item.Content.Equals(clienteSelecionado.City))
                        comboCidade.SelectedItem = item;

                foreach (ComboBoxItem item in comboPais.Items)
                    if (item.Content.Equals(clienteSelecionado.Country))
                        comboPais.SelectedItem = item;

                _editingAddress = new Address
                {
                    Id = clienteSelecionado.Id,
                    Place = clienteSelecionado.Place,
                    Number = Convert.ToInt32(clienteSelecionado.Number),
                    Complement = clienteSelecionado.Complement,
                    Neighborhood = clienteSelecionado.Neighborhood,
                    City = Database.GetInstance.redisClient.As<City>().GetById(comboCidade.SelectedValue),
                    State = Database.GetInstance.redisClient.As<State>().GetById(comboEstado.SelectedValue),
                    Country = Database.GetInstance.redisClient.As<Country>().GetById(comboCidade.SelectedValue),
                    LastModification = DateTime.Now
                };
            }
        }