public void RemoveTodo(Todo todo) { _context.Users.First(x => todo.Category.User.UserId == x.UserId) .Categories.First(cat => cat.CategoryId == todo.Category.CategoryId) .Todos.Remove(todo); _context.SaveChanges(); }
public void UpdateTodo(User currentUser, Todo oldTodo,Todo newTodo) { /* var cTodo = _context.Users.First(u => u.UserId == currentUser.UserId) .Categories.First(cat => cat.CategoryId == oldTodo.Category.CategoryId) .Todos.First(todo => todo.TodoId == oldTodo.TodoId); */ if (oldTodo.Category.CategoryId != newTodo.Category.CategoryId) { oldTodo.Category.Todos.Remove(oldTodo); newTodo.Category.Todos.Add(newTodo); } else { oldTodo.ShortDescription = newTodo.ShortDescription; oldTodo.Description = newTodo.Description; oldTodo.StartDate = newTodo.StartDate; oldTodo.IsDone = newTodo.IsDone; oldTodo.EndDate = newTodo.EndDate; oldTodo.Priority = newTodo.Priority; } _context.SaveChanges(); }
public ActionResult Edit(AddOrEditTodoViewModel viewModel) { if (ModelState.IsValid) { var todo=CurrentUser.Categories.First(x => x.CategoryId == Int32.Parse(viewModel.CategoryId)) .Todos.First(t => t.TodoId == viewModel.TodoId); var newTodo = new Todo() { TodoId = todo.TodoId, ShortDescription = viewModel.ShortDescription, Description = viewModel.Description, StartDate = viewModel.StartDate, IsDone = viewModel.IsDone, EndDate = viewModel.EndDate.Value, Category = CurrentUser.Categories.First(x => x.CategoryId == Int32.Parse(viewModel.CategoryId)), Priority = Int32.Parse(viewModel.Priotiry) }; MainRepository.UpdateTodo(CurrentUser,todo,newTodo); return RedirectToAction("Index"); } viewModel.Categories = GetCategories(); viewModel.Priorities = GetPriorities(); return View(viewModel); }
public void AddTodo(Category cat, Todo todo) { cat.Todos.Add(todo); _context.SaveChanges(); }