public IList <ToDoItemViewModel> GetItems(int userId) { if (db.Tasks.Where(u => u.RemoteUserId == userId).Count() == 0) { db.Tasks.AddRange(remoteService.GetItems(userId).Select(t => new Task { IsCompleted = t.IsCompleted, IsUploaded = true, Name = t.Name, RemoteUserId = t.UserId, RemoteTaskId = t.ToDoId, User = db.Users.Single(u => u.RemoteId == userId) })); db.SaveChanges(); } //return (new ToDoService()).GetItems(userId); return(db.Set <Task>().Where(u => u.RemoteUserId == userId && !u.IsDeleted).Select( t => new ToDoItemViewModel { UserId = t.UserId, Name = t.Name, ToDoId = t.RemoteTaskId, IsCompleted = t.IsCompleted } ).ToList()); }
public async Task <IList <ToDoItemViewModel> > GetItemsAsync(int userId) { return(await Task.Run(() => { db.Tasks.AddRange(remoteService.GetItems(userId).Select(t => new ToDoItem { IsDeleted = false, UserId = db.Users.Single(u => u.RemoteId == userId).Id, IsCompleted = t.IsCompleted, IsUploaded = true, IsChanged = false, Name = t.Name, RemoteUserId = t.UserId, RemoteTaskId = t.ToDoId, User = db.Users.Single(u => u.RemoteId == userId) })); db.SaveChanges(); return db.Set <ToDoItem>().Where(u => u.RemoteUserId == userId && !u.IsDeleted).Select( t => new ToDoItemViewModel { Id = t.Id, UserId = t.UserId, Name = t.Name, ToDoId = t.RemoteTaskId, IsCompleted = t.IsCompleted } ).ToList(); } )); }
private static void Update(ToDoItem item) { var dbTask = db.Set <ToDoItem>().Single(u => u.Id == item.Id); db.Entry(dbTask).CurrentValues.SetValues(item); db.Entry(dbTask).State = EntityState.Modified; db.SaveChanges(); }