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!");
            }
        }
Example #2
0
        public static void AddFoods(IEnumerable <FoodDto> foods)
        {
            using (var context = new EverydayJournalContext())
            {
                foreach (var foodDto in foods)
                {
                    if (foodDto.Name == null)
                    {
                        Console.WriteLine("Invalid data format.");
                    }
                    else
                    {
                        var food = new Food
                        {
                            Name     = foodDto.Name,
                            DateId   = foodDto.DateId,
                            PersonId = foodDto.PersonId
                        };

                        context.Foods.Add(food);
                        Console.WriteLine($"Record {food.Name} successfully imported.");
                    }
                }

                context.SaveChanges();
            }
        }
Example #3
0
        private void Button_Click_UpdateTask(object sender, RoutedEventArgs e)
        {
            try
            {
                var taskToUpdate = 0;
                //getting selected food Id
                taskToUpdate = int.Parse(Tasks.SelectedItem.ToString().Substring(3, 5));

                using (var context = new EverydayJournalContext())
                {
                    var updatedTaskName = UpdatedTaskName.Text;

                    var task = context.Tasks.FirstOrDefault(x => x.Id == taskToUpdate);

                    if (updatedTaskName.Length > 4 && updatedTaskName != task.Name && taskToUpdate > 0)
                    {
                        task.Name = updatedTaskName;
                        context.SaveChanges();
                        MessageBox.Show("Successfully updated task");

                        //Reloading the page
                        TasksPage tasksPage = new TasksPage();
                        this.NavigationService?.Navigate(tasksPage);
                    }
                    else
                    {
                        MessageBox.Show("The length should be greater than 4 symbols!");
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Please, select Task first!");
            }
        }
Example #4
0
        private void Button_Click_AddTask(object sender, RoutedEventArgs e)
        {
            //Check for length
            var taskToAdd = AddTask.Text;

            if (taskToAdd.Length < 5)
            {
                MessageBox.Show("The task should be at least 5 letters long!");
                return;
            }

            //adding the task to DB
            using (var context = new EverydayJournalContext())
            {
                var person = context.People.Find(LoggerUtility.UserId);
                var task   = new Task()
                {
                    Name = taskToAdd,
                    Date = new Date()
                    {
                        ExactDate = DateTime.Now
                    },
                    Person = person
                };

                context.Tasks.AddOrUpdate(task);
                context.SaveChanges();
                MessageBox.Show("Successfully added task");
                //Refresh the page
                TasksPage tasksPage = new TasksPage();
                this.NavigationService?.Navigate(tasksPage);
            }
        }
Example #5
0
        private void Button_Click_Delete(object sender, RoutedEventArgs e)
        {
            try
            {
                var selectedTaskId = 0;
                selectedTaskId = int.Parse(Tasks.SelectedItem.ToString().Substring(3, 5));
                //Deleting the selected task
                using (var context = new EverydayJournalContext())
                {
                    var taskToDelete = context.Tasks.FirstOrDefault(x => x.Id == selectedTaskId);
                    if (taskToDelete != null)
                    {
                        context.Tasks.Remove(taskToDelete);
                        context.SaveChanges();

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

                        //Refresh the page
                        TasksPage tasksPage = new TasksPage();
                        this.NavigationService?.Navigate(tasksPage);
                    }
                    else
                    {
                        MessageBox.Show("There is nothing to delete!");
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Please, select task first");
            }
        }
Example #6
0
        public static void AddTasks(IEnumerable <TaskDto> tasks)
        {
            using (var context = new EverydayJournalContext())
            {
                foreach (var taskDto in tasks)
                {
                    if (taskDto.Name == null || taskDto.DateId == null || taskDto.PersonId == null)
                    {
                        Console.WriteLine("Invalid data format.");
                    }
                    else
                    {
                        var task = new Task
                        {
                            Name     = taskDto.Name,
                            DateId   = taskDto.DateId,
                            PersonId = taskDto.PersonId
                        };

                        context.Tasks.Add(task);
                        Console.WriteLine($"Record {task.Name} successfully imported.");
                    }
                }

                context.SaveChanges();
            }
        }
Example #7
0
        public static void AddDates(IEnumerable <DateDto> dates)
        {
            using (var context = new EverydayJournalContext())
            {
                foreach (var dateDto in dates)
                {
                    if (dateDto.ExactDate == null)
                    {
                        Console.WriteLine("Invalid data format.");
                    }
                    else
                    {
                        var date = new Date
                        {
                            ExactDate = dateDto.ExactDate,
                        };

                        context.Dates.Add(date);

                        Console.WriteLine($"Record {date.ExactDate} successfully imported.");
                    }
                }

                context.SaveChanges();
            }
        }
Example #8
0
        public static void AddPeople(IEnumerable <PersonDto> people)
        {
            using (var context = new EverydayJournalContext())
            {
                foreach (var personDto in people)
                {
                    if (personDto.Name == null ||
                        personDto.Email == null ||
                        personDto.Password == null ||
                        personDto.PhysicalCondition == null)
                    {
                        Console.WriteLine("Invalid data format.");
                    }
                    else
                    {
                        var person = new Person
                        {
                            Name              = personDto.Name,
                            Email             = personDto.Email,
                            Password          = personDto.Password,
                            PhysicalCondition = personDto.PhysicalCondition
                        };

                        context.People.Add(person);

                        Console.WriteLine($"Record {person.Name} successfully imported.");
                    }
                }

                context.SaveChanges();
            }
        }
Example #9
0
        private void RegisterSubmit_Click(object sender, RoutedEventArgs e)
        {
            //Getting the data from the page
            var name     = NameInput.Text;
            var password = PasswordInput.Password;
            var email    = EmailInput.Text;

            //Check before using adding to DB
            if (name.Length < 4)
            {
                MessageBox.Show("The name should be greater than 3 symbols!");
            }
            else if (password.Length < 5)
            {
                MessageBox.Show("The password should be greater than 4 symbols!");
            }
            else if (!email.Contains("@") || email.Length < 6)
            {
                MessageBox.Show("Invalid Email!");
            }
            else
            {
                using (var context = new EverydayJournalContext())
                {
                    try
                    {
                        context.People
                        .Add(new Person()
                        {
                            Name     = name,
                            Password = password,
                            Email    = email
                        });

                        context.SaveChanges();
                        MessageBox.Show("Registration successful!");

                        //Saving Id and UserName for the current session.
                        LoggerUtility.UserId   = context.People.Where(n => n.Name == name).Select(x => x.Id).FirstOrDefault();
                        LoggerUtility.UserName = name;

                        UserHomePage homePage = new UserHomePage();
                        this.NavigationService?.Navigate(homePage);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Error with registering. Please, try again!");
                        NameInput.Clear();
                        PasswordInput.Clear();
                        EmailInput.Clear();
                    }
                }
            }
        }
        private void Button_Click_SaveChanges(object sender, RoutedEventArgs e)
        {
            //Updating the condition with the text from the text box
            using (var context = new EverydayJournalContext())
            {
                var updatedCondition = PhysicalConditionTextBox.Text;
                //Updating the user condition
                context.People.Find(LoggerUtility.UserId).PhysicalCondition = updatedCondition;
                context.SaveChanges();

                MessageBox.Show("Successfully updated condition!");
                PhysicalConditionTextBox.Clear();
            }
        }
        private void Button_Click_SaveChanges(object sender, RoutedEventArgs e)
        {
            using (var context = new EverydayJournalContext())
            {
                //Getting current values of the text boxes
                var username             = UsernameChange.Text;
                var email                = EmailChange.Text;
                var password             = Password.Password;
                var passwordConfirmation = ConfirmPassword.Password;
                //Getting user from DB
                var userPassword = context.People.Find(LoggerUtility.UserId);

                if (password == passwordConfirmation &&
                    userPassword?.Password == password &&
                    username.Length > 3 &&
                    email.Length > 3)
                {
                    try
                    {
                        //Updating the user
                        userPassword.Name  = username;
                        userPassword.Email = email;

                        context.SaveChanges();

                        MessageBox.Show("Successfully updated information!");

                        UserHomePage userHomePage = new UserHomePage();
                        this.NavigationService?.Navigate(userHomePage);
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Please, try again with correct information!");
                        EmailChange.Clear();
                        UsernameChange.Clear();
                        Password.Clear();
                        ConfirmPassword.Clear();
                    }
                }
                else
                {
                    MessageBox.Show(
                        "Invalid data. Please, try with correct password and Username/Email greater than 4 symbols!");
                }
            }
        }
        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);
                }
            }
        }