Beispiel #1
0
        private void InsertClient_Click(object sender, RoutedEventArgs e)
        {
            if (!InValidContext())
            {
                PresentationMessage.ShowMessegeQueue(this.SnackBarClients.MessageQueue, "Por favor llene todos los campos para crear el cliente.");
                return;
            }
            var clientViewModel = new ClientViewModel()
            {
                Name           = Name.Text,
                MiddleName     = MiddleName.Text,
                LastName       = LastName.Text,
                SecondSurName  = SecondSurName.Text,
                Age            = int.Parse(Age.Text),
                Address        = Address.Text,
                Identification = Identification.Text,
                Phone          = Phone.Text
            };

            try
            {
                var rutaDesktop = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\KallpaBox\\Assets";
                var idClient    = _clientRepository.CreateClient(clientViewModel);
                PresentationMessage.ShowMessegeQueue(this.SnackBarClients.MessageQueue, "Cliente creado con exito");

                var photo = new Photo()
                {
                    ClientId    = idClient,
                    PhotoClient = rutaDesktop + "\\" + idClient + ".jpg",
                };

                if (ImageClient.Source != null)
                {
                    if (!(Directory.Exists(rutaDesktop)))
                    {
                        Directory.CreateDirectory(rutaDesktop);
                    }

                    if (Directory.Exists(rutaDesktop))
                    {
                        BitmapImage bitmap = new BitmapImage();
                        bitmap.BeginInit();
                        bitmap.UriSource = new Uri(_fileName);
                        bitmap.EndInit();
                        BitmapEncoder encoder = new PngBitmapEncoder();
                        encoder.Frames.Add(BitmapFrame.Create(bitmap));

                        FileIOPermission f = new FileIOPermission(FileIOPermissionAccess.Write, rutaDesktop);
                        f.AllLocalFiles = FileIOPermissionAccess.Write;
                        try
                        {
                            f.Demand();
                        }
                        catch (SecurityException s)
                        {
                            Console.WriteLine(s.Message);
                        }

                        using (var fileStream = new FileStream(photo.PhotoClient, FileMode.Create))
                        {
                            encoder.Save(fileStream);
                        }

                        photo.LengthPhoto = FileInfoExtension.ObtenerTamanoArchivo(photo.PhotoClient);

                        _photoRepository.CreatePhoto(photo);
                    }
                }

                CleanControls();
            }
            catch (Exception ex)
            {
                throw new Exception("Error en aplicativo contacte con el administrador. {0}", ex);
            }
        }
        private void EditClient_Click(object sender, RoutedEventArgs e)
        {
            if (!InValidContext())
            {
                return;
            }
            var clientViewModel = new ClientViewModel()
            {
                Id             = int.Parse(Id.Text),
                Name           = Name.Text,
                MiddleName     = MiddleName.Text,
                LastName       = LastName.Text,
                SecondSurName  = SecondSurName.Text,
                Age            = int.Parse(Age.Text),
                Address        = Address.Text,
                Identification = Identification.Text,
                IdentityGuid   = IdentityGuid.Text,
                Phone          = Phone.Text
            };

            try
            {
                _clientRepository.UpateClient(clientViewModel.Id, clientViewModel);
                var rutaBorrar  = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\KallpaBox\\TempAssets";
                var rutaDesktop = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\KallpaBox\\Assets";
                var photo       = _photoRepository.GetPhotoClientId(_idClient);
                var rutaTemp    = rutaBorrar + "\\" + _idClient + ".jpg";
                if (ImageClient.Source != null)
                {
                    if (!(Directory.Exists(rutaDesktop)))
                    {
                        Directory.CreateDirectory(rutaDesktop);
                    }

                    if (!(Directory.Exists(rutaBorrar)))
                    {
                        Directory.CreateDirectory(rutaBorrar);
                    }

                    var imagetemp = ImageClient.Source as BitmapSource;

                    ImageClient.Source = null;
                    ImageClient.UpdateLayout();
                    File.Delete(photo.PhotoClient);
                    Thread.Sleep(1000);

                    if (photo != null)
                    {
                        photo.PhotoClient = rutaDesktop + "\\" + _idClient + ".jpg";
                        _photoRepository.UpdatePhoto(photo);
                    }
                    else
                    {
                        photo = new Photo()
                        {
                            ClientId    = _idClient,
                            PhotoClient = rutaDesktop + "\\" + _idClient + ".jpg",
                            LengthPhoto = FileInfoExtension.ObtenerTamanoArchivo(photo.PhotoClient)
                        };

                        _photoRepository.CreatePhoto(photo);
                    }


                    BitmapEncoder encoder = new PngBitmapEncoder();
                    encoder.Frames.Add(BitmapFrame.Create(imagetemp));

                    FileIOPermission f = new FileIOPermission(FileIOPermissionAccess.Write, rutaDesktop);
                    f.AllLocalFiles = FileIOPermissionAccess.Write;
                    try
                    {
                        f.Demand();
                    }
                    catch (SecurityException s)
                    {
                        Console.WriteLine(s.Message);
                    }

                    using (var fileStream = new FileStream(photo.PhotoClient, FileMode.Create))
                    {
                        encoder.Save(fileStream);
                    }
                }

                CleanControls();
                PresentationMessage.ShowMessegeQueue(this.SnackBarEditClients.MessageQueue, "Cliente editado con exito");
            }
            catch (Exception ex)
            {
                throw new Exception($"Error {ex.Message}", ex);
            }
        }