Exemplo n.º 1
0
        private async void button_get_exchange_rates_from_the_time_period_Click(object sender, RoutedEventArgs e)
        {
            // Read input
            string inputStartDate = textBox_insertDate_2.Text;
            string inputEndDate   = textBox_insertDate_3.Text;

            // Validate input
            bool isInputStartDateFormatValid = InputValidator.validateDateFormat(inputStartDate);
            bool isInputStartDateValid       = InputValidator.validateDate(inputStartDate);

            bool isInputEndDateFormatValid = InputValidator.validateDateFormat(inputEndDate);
            bool isInputEndDateValid       = InputValidator.validateDate(inputEndDate);

            bool isValid = isInputStartDateFormatValid && isInputStartDateValid &&
                           isInputEndDateFormatValid && isInputEndDateValid;

            if (isValid)
            {
                // Send HTTP requests
                Loger.appBeginTextWithTime(textBox_AppLoger, "Sending GET HTTP request for EUR rate from " + inputStartDate + " to " + inputEndDate + "...\n");
                string eurResponseJSON = await clientNBP.getEURFromPeriodOfTime(inputStartDate, inputEndDate);

                Loger.appBeginTextWithTime(textBox_AppLoger, "Sending GET HTTP request for USD rate from " + inputStartDate + " to " + inputEndDate + "...\n");
                string usdResponseJSON = await clientNBP.getUSDFromPeriodOfTime(inputStartDate, inputEndDate);

                Loger.appBeginTextWithTime(textBox_AppLoger, "Sending GET HTTP request for GBP rate from " + inputStartDate + " to " + inputEndDate + "...\n");
                string gbpResponseJSON = await clientNBP.getGBPFromPeriodOfTime(inputStartDate, inputEndDate);

                Loger.appBeginTextWithTime(textBox_AppLoger, "Sending GET HTTP request for Gold rate from " + inputStartDate + " to " + inputEndDate + "...\n");
                string goldResponseJSON = await clientNBP.getGoldFromPeriodOfTime(inputStartDate, inputEndDate);

                //Loger.appBeginTextWithTime(textBox_AppLoger, "Response: " + goldResponseJSON);

                // Map JSON to POCO if JSON's presents
                EURFromWebAPI  = eurResponseJSON != clientNBP.resourceNotFoundString ? JsonConvert.DeserializeObject <CurrencyModel>(eurResponseJSON) : null;
                USDFromWebAPI  = usdResponseJSON != clientNBP.resourceNotFoundString ? JsonConvert.DeserializeObject <CurrencyModel>(usdResponseJSON) : null;
                GBPFromWebAPI  = gbpResponseJSON != clientNBP.resourceNotFoundString ? JsonConvert.DeserializeObject <CurrencyModel>(gbpResponseJSON) : null;
                GoldFromWebAPI = goldResponseJSON != clientNBP.resourceNotFoundString ? JsonConvert.DeserializeObject <GoldModel[]>(goldResponseJSON) : null;

                // Print output
                textBox_DatabaseLoger.Text = ""; // Clear db log
                string output = "";
                int    quantityOfDaysBetween = (DateTime.Parse(inputEndDate).Date - DateTime.Parse(inputStartDate).Date).Days;
                if (quantityOfDaysBetween >= 367)
                {
                    output += "400 BadRequest - Przekroczony limit 367 dni / Limit of 367 days has been exceeded\n";
                }


                if (EURFromWebAPI != null && USDFromWebAPI != null && GBPFromWebAPI != null && GoldFromWebAPI != null)
                {
                    Loger.appBeginTextWithTime(textBox_AppLoger, "Printing output in database loger...");

                    output += "Currency rates from " + inputStartDate + " to " + inputEndDate + "\n";

                    EURFromWebAPI.Rates.ToList().ForEach(x => output = output + "EUR: " + x.mid + "   " + x.effectiveDate.ToString(InputValidator.DATE_FORMAT) + "\n");
                    output += "\n";
                    USDFromWebAPI.Rates.ToList().ForEach(x => output = output + "USD: " + x.mid + "   " + x.effectiveDate.ToString(InputValidator.DATE_FORMAT) + "\n");
                    output += "\n";
                    GBPFromWebAPI.Rates.ToList().ForEach(x => output = output + "GBP: " + x.mid + "   " + x.effectiveDate.ToString(InputValidator.DATE_FORMAT) + "\n");
                    output += "\n";
                    GoldFromWebAPI.ToList().ForEach(x => output = output + "Gold: " + x.cena + "   " + x.data.ToString(InputValidator.DATE_FORMAT) + "\n");

                    Loger.appBeginTextWithTime(textBox_DatabaseLoger, output);
                }
                else
                {
                    output += clientNBP.resourceNotFoundString;
                    Loger.appBeginTextWithTime(textBox_DatabaseLoger, output);
                }


                // Update UI
                textBlock_LogerForDatabaseData.Text = "Currency rates from " + inputStartDate + " to " + inputEndDate;
                // Reset currency rates from web API
                textBlock_EUR_RateFromWebAPI.Text  = CURRENCY_RATE_FORMAT + " PLN";
                textBlock_GBP_RateFromWebAPI.Text  = CURRENCY_RATE_FORMAT + " PLN";
                textBlock_USD_RateFromWebAPI.Text  = CURRENCY_RATE_FORMAT + " PLN";
                textBlock_Gold_RateFromWebAPI.Text = CURRENCY_RATE_FORMAT + " PLN / Gram";
                // Reset dates
                textBlock_EUR_RateFromWebAPI_Date.Text  = InputValidator.DATE_FORMAT;
                textBlock_GBP_RateFromWebAPI_Date.Text  = InputValidator.DATE_FORMAT;
                textBlock_USD_RateFromWebAPI_Date.Text  = InputValidator.DATE_FORMAT;
                textBlock_Gold_RateFromWebAPI_Date.Text = InputValidator.DATE_FORMAT;
            }
            else if (!isInputStartDateFormatValid || !isInputEndDateFormatValid)
            {
                MessageBox.Show("Invalid date format. Please insert start date and end date in given format: " + InputValidator.DATE_FORMAT, "WEB API INTERFACE", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else if (!isInputStartDateValid)
            {
                MessageBox.Show("Invalid start date. Please insert valid start date!", "WEB API INTERFACE", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                MessageBox.Show("Invalid end date. Please insert valid end date!", "WEB API INTERFACE", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Exemplo n.º 2
0
        private async void button_getExchangesRatesFromSpecificDate_Click(object sender, RoutedEventArgs e)
        {
            // Read input
            string inputDate = textBox_insertDate_1.Text;

            // Validate input
            bool isInputDateFormatValid = InputValidator.validateDateFormat(inputDate);
            bool isInputDateValid       = InputValidator.validateDate(inputDate);
            bool isValid = isInputDateFormatValid && isInputDateValid;

            if (isValid)
            {
                // Send HTTP requests
                Loger.appBeginTextWithTime(textBox_AppLoger, "Sending GET HTTP request for EUR rate from " + inputDate + "...\n");
                string eurResponseJSON = await clientNBP.getEURFromSpecificDateAsync(inputDate);

                Loger.appBeginTextWithTime(textBox_AppLoger, "Response: " + eurResponseJSON);

                Loger.appBeginTextWithTime(textBox_AppLoger, "Sending GET HTTP request for USD rate from " + inputDate + "...\n");
                string usdResponseJSON = await clientNBP.getUSDFromSpecificDateAsync(inputDate);

                Loger.appBeginTextWithTime(textBox_AppLoger, "Response: " + usdResponseJSON);

                Loger.appBeginTextWithTime(textBox_AppLoger, "Sending GET HTTP request for GBP rate from " + inputDate + "...\n");
                string gbpResponseJSON = await clientNBP.getGBPFromSpecificDateAsync(inputDate);

                Loger.appBeginTextWithTime(textBox_AppLoger, "Response: " + gbpResponseJSON);

                Loger.appBeginTextWithTime(textBox_AppLoger, "Sending GET HTTP request for Gold rate from " + inputDate + "...\n");
                string goldResponseJSON = await clientNBP.getGoldFromSpecificDateAsync(inputDate);

                Loger.appBeginTextWithTime(textBox_AppLoger, "Response: " + goldResponseJSON);

                // Map JSON to POCO if JSON's presents
                EURFromWebAPI  = eurResponseJSON != clientNBP.resourceNotFoundString ? JsonConvert.DeserializeObject <CurrencyModel>(eurResponseJSON) : null;
                USDFromWebAPI  = usdResponseJSON != clientNBP.resourceNotFoundString ? JsonConvert.DeserializeObject <CurrencyModel>(usdResponseJSON) : null;
                GBPFromWebAPI  = gbpResponseJSON != clientNBP.resourceNotFoundString ? JsonConvert.DeserializeObject <CurrencyModel>(gbpResponseJSON) : null;
                GoldFromWebAPI = goldResponseJSON != clientNBP.resourceNotFoundString ? JsonConvert.DeserializeObject <GoldModel[]>(goldResponseJSON) : null;

                // Extract currency rates if objects presents
                double?EURRate  = EURFromWebAPI?.Rates[0].mid;
                double?USDRate  = USDFromWebAPI?.Rates[0].mid;
                double?GBPRate  = GBPFromWebAPI?.Rates[0].mid;
                double?GoldRate = GoldFromWebAPI?[0].cena;

                // Update UI
                textBlock_EUR_RateFromWebAPI.Text  = (EURRate.HasValue ? EURRate.Value.ToString() : CURRENCY_RATE_FORMAT) + " PLN";
                textBlock_USD_RateFromWebAPI.Text  = (USDRate.HasValue ? USDRate.Value.ToString() : CURRENCY_RATE_FORMAT) + " PLN";
                textBlock_GBP_RateFromWebAPI.Text  = (GBPRate.HasValue ? GBPRate.Value.ToString() : CURRENCY_RATE_FORMAT) + " PLN";
                textBlock_Gold_RateFromWebAPI.Text = (GoldRate.HasValue ? GoldRate.Value.ToString() : CURRENCY_RATE_FORMAT) + " PLN / Gram";

                textBlock_EUR_RateFromWebAPI_Date.Text  = inputDate;
                textBlock_USD_RateFromWebAPI_Date.Text  = inputDate;
                textBlock_GBP_RateFromWebAPI_Date.Text  = inputDate;
                textBlock_Gold_RateFromWebAPI_Date.Text = inputDate;

                // Clear database loger
                textBlock_LogerForDatabaseData.Text = "Currency rates from " + InputValidator.DATE_FORMAT + " to " + InputValidator.DATE_FORMAT;
                textBox_DatabaseLoger.Text          = "";
            }
            else if (!isInputDateFormatValid)
            {
                MessageBox.Show("Invalid date format. Please insert date in given format: " + InputValidator.DATE_FORMAT, "WEB API INTERFACE", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            else
            {
                MessageBox.Show("Invalid date. Please insert valid date!", "WEB API INTERFACE", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }