Ejemplo n.º 1
0
        private async void DeleteProduct()
        {
            var answer = await Application.Current.MainPage.DisplayAlert(
                Languages.Confirm,
                Languages.DeleteConfirmation,
                Languages.Yes,
                Languages.No
                );

            if (!answer)
            {
                return;
            }

            var Connection = await this.apiService.CheckConnection();

            if (!Connection.Logrado)
            {
                await Application.Current.MainPage.DisplayAlert(Languages.Error, Connection.Mensaje, Languages.Accept);

                return;
            }

            //conectamos con el diccionario de recursos para traer la url de nuestro backend de datos
            var url                   = Application.Current.Resources["UrlAPI"].ToString();
            var urlPrefix             = Application.Current.Resources["UrlPrefix"].ToString();
            var urlProductsController = Application.Current.Resources["UrlProductsController"].ToString();

            //hacemos nuestra conexion al backend para traer una lista de datos de la clase products que esta en el Common
            var response = await this.apiService.Delete(url, urlPrefix, urlProductsController, this.ProductoId, Settings.TokenType, Settings.AccessToken);

            if (!response.Logrado)
            {
                //si no lo hizo mostrara error en unos mensajes que se tienen en el Common y lo saca
                await Application.Current.MainPage.DisplayAlert(Languages.Error, response.Mensaje, Languages.Accept);

                return;
            }

            var productsViewModel = ProductsViewModel.GetInstance();
            var DeleteProduct     = productsViewModel.MyProducts.Where(p => p.ProductoId == this.ProductoId).FirstOrDefault();

            if (DeleteProduct != null)
            {
                productsViewModel.MyProducts.Remove(DeleteProduct);
            }

            productsViewModel.RefreshList();
        }
Ejemplo n.º 2
0
        private async void Save()
        {
            if (string.IsNullOrEmpty(this.Name))
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    Languages.NameError,
                    Languages.Accept);

                return;
            }

            if (string.IsNullOrEmpty(this.Price))
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    Languages.PriceError,
                    Languages.Accept);

                return;
            }

            var price = decimal.Parse(this.Price);

            if (price < 0)
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    Languages.PriceError,
                    Languages.Accept);

                return;
            }

            if (string.IsNullOrEmpty(this.Description))
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    Languages.DescriptionError,
                    Languages.Accept);

                return;
            }

            if (this.Category == null)
            {
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    Languages.CategoryError,
                    Languages.Accept);

                return;
            }

            this.IsRunning = true;
            this.IsEnable  = false;

            var Connection = await this.api.CheckConnection();

            if (!Connection.Logrado)
            {
                this.IsRunning = false;
                this.IsEnable  = true;
                await Application.Current.MainPage.DisplayAlert(Languages.Error, Connection.Mensaje, Languages.Accept);

                return;
            }

            byte[] imageArray = null;
            if (this.file != null)
            {
                imageArray = FilesHelper.ReadFully(this.file.GetStream());
            }

            var producto = new Productos
            {
                NombreProducto      = this.Name,
                PrecioProducto      = price,
                DescripcionProducto = this.Description,
                ImageArray          = imageArray,
                IdCategoria         = this.Category.CategoriaId,
                UserId = MainViewModel.GetInstance().UserASP.Id,
            };

            //conectamos con el diccionario de recursos para traer la url de nuestro backend de datos
            var url                   = Application.Current.Resources["UrlAPI"].ToString();
            var urlPrefix             = Application.Current.Resources["UrlPrefix"].ToString();
            var urlProductsController = Application.Current.Resources["UrlProductsController"].ToString();

            //hacemos nuestra conexion al backend para traer una lista de datos de la clase products que esta en el Common
            var response = await this.api.Post <Productos>(url, urlPrefix, urlProductsController, producto, Settings.TokenType, Settings.AccessToken);

            if (!response.Logrado)
            {
                this.IsRunning = false;
                this.IsEnable  = true;
                await Application.Current.MainPage.DisplayAlert(
                    Languages.Error,
                    response.Mensaje,
                    Languages.Accept);

                return;
            }

            var newProduct        = (Productos)response.Resultado;
            var productsViewModel = ProductsViewModel.GetInstance();

            productsViewModel.MyProducts.Add(newProduct);
            productsViewModel.RefreshList();

            this.isRunning = false;
            this.isEnable  = true;

            await App.Navigator.PopAsync();
        }