//Remove forecast from file private void BtnDelete_Click(object sender, RoutedEventArgs e) { var confirmResult = MessageBox.Show("Are you sure you want to delete forecast for " + cityCodeDict[loadedForecast.CityID] + "?", "Delete Forecast", System.Windows.MessageBoxButton.YesNo); if (confirmResult == System.Windows.MessageBoxResult.No) { return; } //Get all forecasts var forecasts = DataUtilities.GetForecastsFromDB(); //Remove this forecast from list int index = forecasts.FindIndex(f => f.CityID == loadedForecast.CityID && f.ForecastDate.Date.Equals(loadedForecast.ForecastDate.Date)); forecasts.RemoveAt(index); //Remove this forecast from DB DataUtilities.DeleteForecast(loadedForecast); new ViewForecastsWindow(user).Show(); this.Hide(); }
//Check text file for forecast with same city and date private bool unique() { List <UserForecast> fc = DataUtilities.GetForecastsFromDB(); DateTime selectedDate = ((DateTime)dtpDate.SelectedDate).Date; int selectedCityID = ((City)lstCities.SelectedItem).id; //If there are any forecasts with the same city ID and forecast date if (fc.Where(f => f.CityID == selectedCityID && f.ForecastDate.Date == selectedDate).ToList().Count > 0) { crdError.Visibility = Visibility.Visible; lblError.Text = "Already have a forecast for this city on this date"; return(false); } return(true); }
//Check if there is already a forecast for city on selected date private bool unique() { List <UserForecast> fc = DataUtilities.GetForecastsFromDB(); DateTime selectedDate = ((DateTime)dtpDate.SelectedDate).Date; int selectedCityID = ((City)lstCities.SelectedItem).id; //if there is a forecast one the same day for the same city which is not from this forecast, original forecast being edited still exists in file so if there is already //a forecast it might be this one if (fc.Where(f => f.CityID == selectedCityID && f.ForecastDate.Date == selectedDate.Date && f.ForecastDate.Date != loadedForecast.ForecastDate.Date).ToList().Count > 0) { crdError.Visibility = Visibility.Visible; lblError.Text = "Already have a forecast for this city on this date"; return(false); } return(true); }
private void Window_Loaded(object sender, RoutedEventArgs e) { if (user.UserType == UserType.GeneralUser) { btnAddForecast.Visibility = Visibility.Hidden; } grdMain.Background = DataUtilities.ChooseBackground(); //Randomly select background codeCityDict = CityUtilities.getCityCodeDict(); //Get dictionary of City Codes to City Objects forecasts = DataUtilities.GetForecastsFromDB(); //Get forecasts from file populateListBox(); //Populate list box with cities //Set defaults lstCities.SelectedIndex = 0; dtpFrom.SelectedDate = DateTime.Now; dtpTo.SelectedDate = DateTime.Now; }
//Check if inputs are valid, then overwrite file with new values private void BtnSave_Click(object sender, RoutedEventArgs e) { if (!checkValidInputsSave() || !unique()) { return; } crdError.Visibility = Visibility.Hidden; //Get forecasts and then replace the original forecast object with the new one made from the values in the fields on form var forecasts = DataUtilities.GetForecastsFromDB(); UserForecast forecast = new UserForecast(0, ((City)lstCities.SelectedItem).id, (DateTime)dtpDate.SelectedDate, Convert.ToInt16(txtMin.Text), Convert.ToInt16(txtMax.Text), Convert.ToInt16(txtWind.Text), Convert.ToInt16(txtHumidity.Text), Convert.ToInt16(txtPrecip.Text)); forecasts[forecasts.FindIndex(f => f.CityID == loadedForecast.CityID && f.ForecastDate.Date.Equals(loadedForecast.ForecastDate.Date))] = forecast; //Overwrite original file DataUtilities.EditForecast(loadedForecast.ForecastID, forecast); MessageBox.Show("Forecast edited!"); new ViewForecastsWindow(user).Show(); this.Hide(); }