Example #1
0
        async void Save()
        {
            if (string.IsNullOrEmpty(Description))
            {
                await dialogService.ShowMessage(
                    "Error",
                    "You must enter an Product.");

                return;
            }

            if (string.IsNullOrEmpty(Price))
            {
                await dialogService.ShowMessage(
                    "Error",
                    "You must enter an Price.");

                return;
            }
            var price = decimal.Parse(Price);

            if (price <= 0)
            {
                await dialogService.ShowMessage(
                    "Error",
                    "You must enter aun Price > 0");
            }

            if (string.IsNullOrEmpty(Stock))
            {
                await dialogService.ShowMessage(
                    "Error",
                    "You must enter an Stock.");

                return;
            }
            var stock = Double.Parse(Price);

            if (stock <= 0)
            {
                await dialogService.ShowMessage(
                    "Error",
                    "You must enter aun Stock > 0");
            }

            if (string.IsNullOrEmpty(Stock))
            {
                await dialogService.ShowMessage(
                    "Error",
                    "You must enter an Stock.");

                return;
            }


            byte[] imageArray = null;
            if (file != null)
            {
                imageArray = FilesHelper.ReadFully(file.GetStream());
                file.Dispose();
            }
            var mainViewModels = MainViewModels.GetInstance();
            var product        = new Product
            {
                CategoryId   = mainViewModels.Category.CategoryId,
                Description  = Description,
                ImageArray   = imageArray,
                IsActive     = IsActive,
                LastPurchase = LastPurchase,
                Price        = price,
                Remarks      = Remarks,
                Stock        = stock,
            };

            IsRunning = true;
            IsEnabled = false;

            var connection = await apiService.CheckConnection();

            if (!connection.IsSuccess)
            {
                product.PendingToSave = true;
                dataService.Insert(product);
                await dialogService.ShowMessage(
                    "Message",
                    "The product was save to DB Local, don't forget to upload when to have connection to internet"
                    );
            }
            else
            {
                var response = await apiService.Post(
                    "http://productsapiis.azurewebsites.net",
                    "/api",
                    "/Products",
                    mainViewModels.Token.TokenType,
                    mainViewModels.Token.AccessToken
                    , product);

                if (!response.IsSuccess)
                {
                    IsRunning = false;
                    IsEnabled = true;
                    await dialogService.ShowMessage(
                        "Error",
                        response.Message);

                    return;
                }

                product = (Product)response.Result;
            }

            var productsViewModel = ProductsViewModel.GetInstance();

            productsViewModel.Add(product);

            await navigationService.BackOnMaster();

            IsRunning = false;
            IsEnabled = true;
        }
        async void Save()
        {
            if (string.IsNullOrEmpty(Description))
            {
                await dialogService.ShowMessage(
                    "Error",
                    "You must enter a product description.");

                return;
            }

            if (string.IsNullOrEmpty(Price))
            {
                await dialogService.ShowMessage(
                    "Error",
                    "You must enter a product price.");

                return;
            }

            var price = decimal.Parse(Price);

            if (price < 0)
            {
                await dialogService.ShowMessage(
                    "Error",
                    "The price must be a value greather or equals than zero.");

                return;
            }

            if (string.IsNullOrEmpty(Stock))
            {
                await dialogService.ShowMessage(
                    "Error",
                    "You must enter a product stock.");

                return;
            }

            var stock = double.Parse(Stock);

            if (stock < 0)
            {
                await dialogService.ShowMessage(
                    "Error",
                    "The stock must be a value greather or equals than zero.");

                return;
            }

            IsRunning = true;
            IsEnabled = false;

            var connection = await apiService.CheckConnection();

            if (!connection.IsSuccess)
            {
                IsRunning = false;
                IsEnabled = true;
                await dialogService.ShowMessage("Error", connection.Message);

                return;
            }

            var mainViewModel = MainViewModel.GetInstance();
            var urlAPI        = Application.Current.Resources["URLAPI"].ToString();

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

            product.Description  = Description;
            product.IsActive     = IsActive;
            product.LastPurchase = LastPurchase;
            product.Price        = price;
            product.Remarks      = Remarks;
            product.Stock        = stock;
            product.ImageArray   = imageArray;

            var response = await apiService.Put(
                urlAPI,
                "/api",
                "/Products",
                mainViewModel.Token.TokenType,
                mainViewModel.Token.AccessToken,
                product);

            if (!response.IsSuccess)
            {
                IsRunning = false;
                IsEnabled = true;
                await dialogService.ShowMessage(
                    "Error",
                    response.Message);

                return;
            }

            ProductsViewModel.GetInstance().Update(product);

            await navigationService.BackOnMaster();

            IsRunning = false;
            IsEnabled = true;
        }
        async void Save()
        {
            if (String.IsNullOrEmpty(Description))
            {
                await dialogService.ShowMessage("Error", "You must enter a description");

                return;
            }

            IsRunning = true;
            IsEnabled = false;

            var connection = await apiService.CheckConnection();

            if (!connection.IsSuccess)
            {
                IsRunning = false;
                IsEnabled = true;
                await dialogService.ShowMessage("Error", connection.Message);

                return;
            }

            var mainViewModel = MainViewModel.GetInstance();

            var product = new Product
            {
                Description  = Description,
                Price        = Price,
                IsActive     = IsActive,
                LastPurchase = LastPurchase,
                Stock        = Stock,
                Remarks      = Remarks,
                CategoryId   = mainViewModel.category.CategoryId,
                Image        = "Sin Imagen"
            };

            var response = await apiService.Post(
                "http://200.76.182.140:8085",
                "/api",
                "/Products",
                mainViewModel.Token.TokenType,
                mainViewModel.Token.AccessToken,
                product);

            if (!response.IsSuccess)
            {
                IsRunning = false;
                IsEnabled = true;
                await dialogService.ShowMessage("Error", response.Message);

                return;
            }

            product = (Product)response.Result;
            var productsViewModel = ProductsViewModel.GetInstance(mainViewModel.category.Products);

            productsViewModel.AddProduct(product);
            await navigationService.Back();

            IsRunning = false;
            IsEnabled = true;
        }
Example #4
0
        private async void SaveNewProduct()
        {
            if (Price <= 0)
            {
                await dialogService.ShowMessage(Languages.Error, Languages.NewProductErrorEmptyPrice);

                return;
            }
            if (string.IsNullOrEmpty(Description))
            {
                await dialogService.ShowMessage(Languages.Error, Languages.NewProductErrorEmptyDescription);

                return;
            }

            IsRunning = true;
            IsEnable  = false;

            var connection = await apiService.CheckConnection();

            if (!connection.isSuccess)
            {
                IsRunning = false;
                IsEnable  = true;
                await dialogService.ShowMessage(Languages.Error, connection.Message);

                return;
            }

            var product = new Product
            {
                Description  = Description,
                Price        = Price,
                IsActive     = IsActive,
                Stock        = Stock,
                CategoryId   = _categoryId,
                LastPurchase = DateTime.Now,
            };

            var mainViewModel = MainViewModel.GetInstance();
            var response      = await apiService.Post(
                Application.Current.Resources["URLAPI"].ToString(),
                Application.Current.Resources["URLPREFIX"].ToString(),
                Application.Current.Resources["PRODUCTCONTROLLER"].ToString(),
                mainViewModel.Token.TokenType,
                mainViewModel.Token.AccessToken,
                product
                );

            if (!response.isSuccess)
            {
                IsEnable = true;
                await dialogService.ShowMessage(Languages.Error, response.Message);

                IsRunning = false;
                IsEnable  = true;
                return;
            }

            product = (Product)response.Result;

            var productViewModel = ProductsViewModel.GetInstance();

            productViewModel.AddProduct(product);
            await navigationService.Back();

            IsRunning = false;
            IsEnable  = true;
        }
        async void Save()
        {
            if (string.IsNullOrEmpty(Description))
            {
                await dialogService.ShowMessage(
                    "Error",
                    "You must enter a product description.");

                return;
            }

            if (string.IsNullOrEmpty(Price))
            {
                await dialogService.ShowMessage(
                    "Error",
                    "You must enter a product price.");

                return;
            }

            var price = decimal.Parse(Price);

            if (price < 0)
            {
                await dialogService.ShowMessage(
                    "Error",
                    "The price must be a value greather or equals than zero.");

                return;
            }

            if (string.IsNullOrEmpty(Stock))
            {
                await dialogService.ShowMessage(
                    "Error",
                    "You must enter a product stock.");

                return;
            }

            var stock = double.Parse(Stock);

            if (stock < 0)
            {
                await dialogService.ShowMessage(
                    "Error",
                    "The stock must be a value greather or equals than zero.");

                return;
            }

            IsRunning = true;
            IsEnabled = false;

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

            var mainViewModel = MainViewModel.GetInstance();
            var urlAPI        = Application.Current.Resources["URLAPI"].ToString();

            var product = new Product
            {
                CategoryId   = mainViewModel.Category.CategoryId,
                Description  = Description,
                ImageArray   = imageArray,
                IsActive     = IsToggled,
                LastPurchase = LastPurchase,
                Price        = price,
                Remarks      = Remarks,
                Stock        = stock,
            };

            var connection = await apiService.CheckConnection();

            if (!connection.IsSuccess)
            {
                product.PendingToSave = true;
                dataService.Insert(product);
                await dialogService.ShowMessage(
                    "Message",
                    "The product was save local DB don't forget to" +
                    "upload the record when you have WiFi.");
            }
            else
            {
                var response = await apiService.Post(
                    urlAPI,
                    "/api",
                    "/Products",
                    mainViewModel.Token.TokenType,
                    mainViewModel.Token.AccessToken,
                    product);

                if (!response.IsSuccess)
                {
                    IsRunning = false;
                    IsEnabled = true;
                    await dialogService.ShowMessage(
                        "Error",
                        response.Message);

                    return;
                }

                product = (Product)response.Result;
            }

            var productsViewModel = ProductsViewModel.GetInstance();

            productsViewModel.Add(product);

            await navigationService.BackOnMaster();

            IsRunning = false;
            IsEnabled = true;
        }