示例#1
0
        public EditForecastWindow(UserForecast forecast, User user)
        {
            InitializeComponent();
            WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;

            loadedForecast = forecast;
            this.user      = user;
        }
        //Handler for if user wants to edit forecast
        private void BtnEdit_Click(object sender, RoutedEventArgs e)
        {
            Button editBtn = (Button)sender;
            //Useful bits refers to the city ID and date of the forecast
            string usefulBits = editBtn.Name.Substring(7);
            var usefulBitsArr = usefulBits.Split('_');
            int cityId = Convert.ToInt32(usefulBitsArr[0]);
            DateTime date = new DateTime(Convert.ToInt16(usefulBitsArr[1]), Convert.ToInt16(usefulBitsArr[2]), Convert.ToInt16(usefulBitsArr[3]));
            UserForecast clickedForecast = forecasts.Where(o => o.CityID == cityId && o.ForecastDate.Date.Equals(date.Date)).ToList()[0];

            new EditForecastWindow(clickedForecast, user).Show();
            this.Hide();
        }
        public static void EditForecast(int forecastID, UserForecast newForecast)
        {
            DB_Forecast forecast = db.DB_Forecast.Find(forecastID);

            forecast.CityID        = newForecast.CityID;
            forecast.ForecastDate  = newForecast.ForecastDate;
            forecast.MinTemp       = newForecast.MinimumTemp;
            forecast.MaxTemp       = newForecast.MaximumTemp;
            forecast.WindSpeed     = newForecast.WindSpeed;
            forecast.Humidity      = newForecast.Humidity;
            forecast.Precipitation = newForecast.Precipitation;

            db.SaveChanges();
        }
示例#4
0
        //If inputs are valid add forecast as new line in text file
        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            if (!checkValidInputsSave() || !unique())
            {
                return;
            }

            crdError.Visibility = Visibility.Hidden;

            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));

            DataUtilities.InsertForecast(forecast);
            MessageBox.Show("Forecast added!");
            clearForm();
        }
        public static void InsertForecast(UserForecast newForecast)
        {
            DB_Forecast dbForecast = new DB_Forecast();

            dbForecast.CityID        = newForecast.CityID;
            dbForecast.ForecastDate  = newForecast.ForecastDate;
            dbForecast.MinTemp       = newForecast.MinimumTemp;
            dbForecast.MaxTemp       = newForecast.MaximumTemp;
            dbForecast.WindSpeed     = newForecast.WindSpeed;
            dbForecast.Humidity      = newForecast.Humidity;
            dbForecast.Precipitation = newForecast.Precipitation;


            db.DB_Forecast.Add(dbForecast);
            db.SaveChanges();
        }
示例#6
0
        //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();
        }
 public static void DeleteForecast(UserForecast forecast)
 {
     db.DB_Forecast.Remove(db.DB_Forecast.Find(forecast.ForecastID));
     db.SaveChanges();
 }