コード例 #1
0
        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 product = new Product
            {
                CategoryId   = mainViewModel.Category.CategoryId,
                Description  = Description,
                ImageArray   = imageArray,
                IsActive     = IsActive,
                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 on local DB don't forget to " +
                    "upload the record when you have WiFi.");
            }
            else
            {
                var urlAPI = Application.Current.Resources["URLAPI"].ToString();

                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;
        }
コード例 #2
0
        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();

            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(
                "http://productszuluapi.azurewebsites.net",
                "/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;
        }