private void Button_Click_Delete(object sender, RoutedEventArgs e)
        {
            try
            {
                var selectedFoodId = 0;
                //Getting selected food Id from the List
                selectedFoodId = int.Parse(Foods.SelectedItem.ToString().Substring(3, 5));

                using (var context = new EverydayJournalContext())
                {
                    var foodToDelete = context.Foods.FirstOrDefault(x => x.Id == selectedFoodId);

                    //Making check before adding to the DB
                    if (foodToDelete != null && selectedFoodId > 0)
                    {
                        context.Foods.Remove(foodToDelete);
                        context.SaveChanges();

                        MessageBox.Show("Successfully deleted food!");

                        //Reloading the page
                        FoodByDatePage foodByDatePage = new FoodByDatePage();
                        this.NavigationService?.Navigate(foodByDatePage);
                    }
                    else
                    {
                        MessageBox.Show("There is nothing to delete!");
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Please, select food first!");
            }
        }
Beispiel #2
0
        private void Button_Click_FoodByDate(object sender, RoutedEventArgs e)
        {
            //Navigate to Food page
            FoodByDatePage foodPage = new FoodByDatePage();

            this.NavigationService?.Navigate(foodPage);
        }
        private void Button_Click_UpdateFood(object sender, RoutedEventArgs e)
        {
            try
            {
                var foodToUpdate = 0;

                //Getting selected food Id from the List box
                foodToUpdate = int.Parse(Foods.SelectedItem.ToString().Substring(3, 5));

                using (var context = new EverydayJournalContext())
                {
                    var updatedFoodName = UpdatedFoodName.Text;

                    //Getting selected Food from the List
                    var food = context.Foods.FirstOrDefault(x => x.Id == foodToUpdate);

                    //Making check of the new food name andd adding it to the DB
                    if (updatedFoodName.Length > 4 && updatedFoodName != food.Name && foodToUpdate > 0)
                    {
                        food.Name = updatedFoodName;
                        context.SaveChanges();

                        MessageBox.Show("Successfully updated food");

                        //Reloading the page to refresh it
                        FoodByDatePage foodByDatePage = new FoodByDatePage();
                        this.NavigationService?.Navigate(foodByDatePage);
                    }
                    else
                    {
                        MessageBox.Show("The food should be more than 4 symbols!");
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Please, select Food first!");
            }
        }
        private void Button_Click_AddFood(object sender, RoutedEventArgs e)
        {
            //Getting TextBox value
            var foodToAdd = AddFood.Text;

            //Making check of the new food
            if (foodToAdd.Length < 6 || foodToAdd.Length > 50)
            {
                MessageBox.Show("The food name length should be between 6 and 50 symbols.");
            }
            else
            {
                using (var context = new EverydayJournalContext())
                {
                    //Finding currently logged user
                    var person = context.People.Find(LoggerUtility.UserId);
                    var food   = new Food()
                    {
                        Name = foodToAdd,
                        Date = new Date()
                        {
                            ExactDate = DateTime.Now
                        },
                        Person = person
                    };

                    //Adding it to the DB
                    context.Foods.AddOrUpdate(food);
                    context.SaveChanges();

                    MessageBox.Show("Successfully added food");

                    //Reloading the page to refresh it
                    FoodByDatePage foodByDatePage = new FoodByDatePage();
                    this.NavigationService?.Navigate(foodByDatePage);
                }
            }
        }