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(); } }
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(); } }
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(); } }
public static void Main() { EverydayJournalContext.Create(); var context = new EverydayJournalContext(); Console.WriteLine(context.People.Count()); }
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!"); } }
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); } }
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!"); } }
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"); } }
private void Button_Click_Login(object sender, RoutedEventArgs e) { using (var context = new EverydayJournalContext()) { //Getting username and password from the login form var username = UsernameInput.Text; var password = PasswordInput.Password; //Check if the data successfully match in the database if (context.People.Any(a => a.Password == password && a.Name == username)) { var userId = context.People .Where(a => a.Password == password && a.Name == username) .Select(i => new { i.Id, i.Name }) .FirstOrDefault(); MessageBox.Show("Successfully logged!"); //Saving Id and UserName for the current session. LoggerUtility.UserId = userId.Id; LoggerUtility.UserName = userId.Name; //Navigate to UserHomePage UserHomePage userHomePage = new UserHomePage(); this.NavigationService?.Navigate(userHomePage); } else { //Invalid credentials MessageBox.Show("Invalid Username or Password!"); UsernameInput.Clear(); PasswordInput.Clear(); } } }
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(); } }
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(); } } } }
public PhysicalConditionsPage() { InitializeComponent(); TitlePhysical.Content = (LoggerUtility.UserName ?? "User") + " Condition"; //Loading the current condition in the text box using (var context = new EverydayJournalContext()) { var username = context.People.Find(LoggerUtility.UserId); PhysicalConditionTextBox.Text = username.PhysicalCondition; } }
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(); } }
public ProfilePage() { InitializeComponent(); ProfileTitle.Content = (LoggerUtility.UserName ?? "User") + " Profile"; //Loading logged used credentials in the text boxes using (var context = new EverydayJournalContext()) { var username = context.People.FirstOrDefault(n => n.Id == LoggerUtility.UserId); UsernameChange.Text = username?.Name; EmailChange.Text = username?.Email; } }
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!"); } } }
public TasksPage() { InitializeComponent(); //Title TitleTasks.Content = (LoggerUtility.UserName ?? "User") + " Tasks"; //Loading data from DB using (var context = new EverydayJournalContext()) { var tasks = context.Tasks .Where(x => x.PersonId == LoggerUtility.UserId) .OrderBy(z => z.Date.ExactDate) .Select(y => "Id." + y.Id.ToString() + " " + y.Name + " - " + y.Date.ExactDate) .ToArray(); Tasks.ItemsSource = tasks; } }
public FoodByDatePage() { InitializeComponent(); //Tittle of the Page TitleFoods.Content = (LoggerUtility.UserName ?? "User") + " Foods"; using (var context = new EverydayJournalContext()) { //Getting ID, Name and Date for each food from the DB for the currently logged User var foods = context.Foods .Where(x => x.PersonId == LoggerUtility.UserId) .OrderBy(z => z.Date.ExactDate) .Select(y => "Id." + y.Id.ToString() + " " + y.Name + " - " + y.Date.ExactDate) .ToArray(); //Loading Foods in the List Box Foods.ItemsSource = foods; } }
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); } } }
public static void AddTasks() { using (var context = new EverydayJournalContext()) { var people = context.People.ToList(); var tasks = context.Tasks.ToList(); int index = 1; foreach (var person in people) { if (index == 9) { index = 1; } person.Tasks.Add(tasks[index]); person.Tasks.Add(tasks[index + 1]); index++; } } }
protected override void Seed(EverydayJournalContext context) { // use the if statement id AddOrUpdate method doesn't work // if (!context.Dates.Any()) // { var dates = new Date[] { new Date() { ExactDate = new DateTime(2017, 4, 15) }, new Date() { ExactDate = new DateTime(2017, 4, 14) }, new Date() { ExactDate = new DateTime(2017, 4, 13) }, new Date() { ExactDate = new DateTime(2017, 4, 12) }, new Date() { ExactDate = new DateTime(2017, 4, 11) }, new Date() { ExactDate = new DateTime(2017, 4, 10) }, new Date() { ExactDate = new DateTime(2017, 4, 9) }, new Date() { ExactDate = new DateTime(2017, 4, 8) }, new Date() { ExactDate = new DateTime(2017, 4, 7) }, new Date() { ExactDate = new DateTime(2017, 4, 6) }, new Date() { ExactDate = new DateTime(2017, 4, 5) }, new Date() { ExactDate = new DateTime(2017, 4, 4) }, new Date() { ExactDate = new DateTime(2017, 4, 3) }, new Date() { ExactDate = new DateTime(2017, 4, 2) }, new Date() { ExactDate = new DateTime(2017, 4, 1) } }; //foreach (var date in dates) //{ // context.Dates.AddOrUpdate(d => d.ExactDate, date); //} var tasksNames = new string[] { "Go for a walk", "Clean the house", "Change my diet", "Go to the gym", "Skip gym today", "Buy vegetables", "Finish the project", "Eat healthy", "Cook dinner", "Get my kid from school", "Buy beer", "Do the laundry", "Call mom", "Water the flowers", "Edit my journal", "Drink more water", "Go out with friends", "Search for good restaurant", "Go to dances", "Relax", "Code" }; var foodsNames = new string[] { "Avocado", "Fruit salad", "Vegetable salad", "Marakuya", "Mango", "Strawberries", "Blueberries", "Shopska salad", "Chicken", "Chicken with rice", "Green vegetables", "Carrots", "Yogurt", "Peppers", "Spinach" }; //for (int i = 0; i < dates.Length; i++) //{ // context.Tasks.AddOrUpdate(t => t.Name, new Task() {Name = tasksNames[i], DateId = i + 1}); // context.Foods.AddOrUpdate(f => f.Name, new Food() {Name = foodsNames[i], DateId = i + 1}); //} //context.SaveChanges(); // } }
public static void InitDB() { var context = new EverydayJournalContext(); context.Database.Initialize(true); }