public bool Update(int id, TaskGroupViewModel group) { if (!TryFindGroupInDb(id, out var groupFromDb)) { return(false); } var updatedGroup = _mapper.Map <TaskGroupEntity>(group); _dbContext.Entry(groupFromDb).CurrentValues.SetValues(updatedGroup); foreach (var userTaskFromDb in groupFromDb.UserTasks.ToList()) { var task = updatedGroup.UserTasks.SingleOrDefault(t => t.Id == userTaskFromDb.Id); if (task == null) { _dbContext.Remove(userTaskFromDb); } else { _dbContext.Entry(userTaskFromDb).CurrentValues.SetValues(task); } } foreach (var newUserTasks in updatedGroup.UserTasks .Where(newUserTasks => newUserTasks.Id == 0 || groupFromDb.UserTasks.All(t => t.Id != newUserTasks.Id))) { groupFromDb.UserTasks.Add(newUserTasks); } _dbContext.SaveChanges(); return(true); }
public void UpsertShouldModifyTheGivenComment() { var options = new DbContextOptionsBuilder <TasksDbContext>() .UseInMemoryDatabase(databaseName: nameof(UpsertShouldModifyTheGivenComment)) .Options; using (var context = new TasksDbContext(options)) { var commentsService = new CommentsService(context); var toAdd = new CommentPostDTO() { Important = true, Text = "A nice task...", }; var added = commentsService.Create(toAdd, null); context.Entry(added).State = EntityState.Detached; var update = new Comment() { Important = false, Text = "A nice task...", }; var updateResult = commentsService.Upsert(added.Id, update); Assert.NotNull(updateResult); Assert.False(updateResult.Important); Assert.AreEqual(added.Text, updateResult.Text); } }
public bool Update(int id, UserTaskViewModel taskDetailsViewModel) { if (!TryFindTaskInDb(id, out var taskFromDb)) { return(false); } var updatedTask = _mapper.Map <UserTaskEntity>(taskDetailsViewModel); _dbContext.Entry(taskFromDb).CurrentValues.SetValues(updatedTask); _dbContext.SaveChanges(); return(true); }
public IActionResult MarkComplete(int id) { Tasks found = _context.Task.Find(id); if (found != null) { //change the things (we're changing the completed status) found.Completed = !found.Completed; //modify the state of this entry in the database _context.Entry(found).State = Microsoft.EntityFrameworkCore.EntityState.Modified; _context.Update(found); _context.SaveChanges(); } return(RedirectToAction("TasksIndex", new { id = found.Id })); }
public void UpsertShouldModifyTheGivenTask() { var options = new DbContextOptionsBuilder <TasksDbContext>() .UseInMemoryDatabase(databaseName: nameof(UpsertShouldModifyTheGivenTask)) .Options; using (var context = new TasksDbContext(options)) { var taskService = new TasksService(context); var added = taskService.Create(new TaskPostDTO { Title = "Booking1010", Description = "Verify booking commision", DateAdded = DateTime.Parse("2019-06-15T00:00:00"), Deadline = DateTime.Parse("2019-06-17T00:00:00"), TaskImportance = "High", TaskState = "Closed", DateClosed = null, Comments = new List <Comment>() { new Comment { Important = true, Text = "A nice task...", } }, }, null); context.Entry(added).State = EntityState.Detached; var update = new Task() { Title = "Updated", }; var updateResult = taskService.Upsert(added.Id, update); Assert.NotNull(updateResult); Assert.AreNotEqual(added.Title, updateResult.Title); Assert.AreEqual("Updated", updateResult.Title); } }
Task <ActionResult> UpdateTask(int id, Task newTask) { if (!ModelState.IsValid) { return(BadRequest("Model not matching requirements")); } newTask.Id = id; _context.Entry(newTask).State = EntityState.Modified; var taskToUpdate = await _context.Tasks. SingleOrDefaultAsync(x => x.Id == id); if (taskToUpdate == null) { return(NotFound("Task not found")); } _context.Update(taskToUpdate); await _context.SaveChangesAsync(); return(Ok("Task successfully Updated")); }
public async Task <ActionResult> UpdateTask(int id, ToDoTask newToDoTask) { if (!ModelState.IsValid) { return(BadRequest("Model not matching requirements")); } newToDoTask.Id = id; _context.Entry(newToDoTask).State = EntityState.Modified; var taskToUpdate = await _context.ToDoTasks.FindAsync(id); if (taskToUpdate == null) { return(NotFound("Task not found.")); } _context.Update(taskToUpdate); await _context.SaveChangesAsync(); return(Ok("Task updated.")); }
public void Update(TEntity entity) { _dbSet.Attach(entity); _context.Entry(entity).State = EntityState.Modified; _context.SaveChanges(); }