public async Task<IActionResult> List(string userid) { if(userid == null && !User.Identity.IsAuthenticated) // if no user is currently logged in { return RedirectToAction("index", "home"); // return to home } //else try to obtain a user model try { User user = null; if (User.Identity.IsAuthenticated) user = await _userManager.FindByNameAsync(User.Identity.Name); else user = await _userManager.FindByIdAsync(userid); var todos = new List<ToDoItem>(); using (var context = new ToDoListDBContext()) { todos = context.ToDoItem.Where(item => item.UserId == user.Id).ToList(); } var viewModel = new ToDoListViewModel() { IdentityUser = user, ToDoItems = todos }; return View(viewModel); } catch(Exception e) { return RedirectToAction("error", "home", new { error = e.Message }); } }
public async Task<IActionResult> Create(ToDoItemViewModel toDoItem) { if (ModelState.IsValid) // if the form is valid { try // try creating a new item { using (var context = new ToDoListDBContext()) { var newItem = new ToDoItem() // create new item from our ViewModel { Finished = false, Title = toDoItem.Title, UserId = toDoItem.UserId }; //add new item to the database context.ToDoItem.Add(newItem); await context.SaveChangesAsync(); } } catch(Exception e) // if something went wrong { return RedirectToAction("error", "home", new { error = e.Message }); // return error page } } return RedirectToAction("list","account", new { userid = toDoItem.UserId }); }
public void Delete(int Id) { ToDoListDBContext db = DBContext; var item = db.ToDoList.Where(x => x.Id == Id).FirstOrDefault(); db.ToDoList.Remove(item); db.SaveChanges(); }
public void SetIsDone(int Id, bool IsDone) { ToDoListDBContext db = DBContext; var item = db.ToDoList.Where(x => x.Id == Id).FirstOrDefault(); item.IsDone = IsDone; db.SaveChanges(); }
public void Update(ToDoList toDoList) { ToDoListDBContext db = DBContext; var item = db.ToDoList.Where(x => x.Id == toDoList.Id).FirstOrDefault(); item.TodoTitle = toDoList.TodoTitle; item.DueDate = toDoList.DueDate; db.SaveChanges(); }
public void Add(ToDoList toDoList) { ToDoListDBContext db = DBContext; var Itemslst = GetList(); int maxId = Itemslst != null && Itemslst.Count > 0 ? Itemslst.Max(x => x.Id) : 0; toDoList.Id = maxId + 1; db.ToDoList.Add(toDoList); db.SaveChanges(); }
public Form1() { InitializeComponent(); toDoListDBContext = new ToDoListDBContext(); var statuses = toDoListDBContext.Statuses.ToList(); foreach (Status s in statuses) { cboStatus.Items.Add(s); } refreshData(); }
public IActionResult Delete(int itemId) { try { using (var context = new ToDoListDBContext()) { var item = context.ToDoItem.Where(item => item.ItemId == itemId).First(); context.ToDoItem.Remove(item); context.SaveChanges(); } return Json(new { success = true }); } catch (Exception e) // if something went wrong { return RedirectToAction("error", "home", new { error = e.Message }); // return error page } }
public IActionResult Edit(ToDoItemViewModel viewModel) { var id = viewModel.ItemId; try { using (var context = new ToDoListDBContext()) { var entity = context.ToDoItem.Where(x => x.ItemId == id).First(); if (entity == null) //if there's no item with such id throw new NullReferenceException("Item not found"); // throw an error //else - update the item and refresh the list entity.Title = viewModel.Title; entity.Finished = viewModel.Finished; context.ToDoItem.Update(entity); context.SaveChanges(); } return RedirectToAction("list", "account", new { userid = viewModel.UserId }); } catch (Exception e) // if something went wrong { return RedirectToAction("error", "home", new { error = e.Message }); // return error page } }
public ToDoListController(ToDoListDBContext toDoListDBContext, ICurrentUser currentUser) { _toDoListDBContext = toDoListDBContext; _currentUser = currentUser; }
public ToDoList GetById(int Id) { ToDoListDBContext db = DBContext; return(db.ToDoList.Where(x => x.Id == Id).FirstOrDefault()); }
public List <ToDoList> GetList() { ToDoListDBContext db = DBContext; return(db.ToDoList.ToList()); }
public SQLActivityRepository(ToDoListDBContext db) { this.db = db; }
public ToDoController(ToDoListDBContext db) { _db = db; }