public IActionResult UpdateProject(long pId, string projectJson) { Tuple <Project, IList <User> > newProjTup = ProjectFromJson(projectJson); if (newProjTup == null) { return(BadRequest()); } Project newProj = newProjTup.Item1; using (var context = new TodoContext()) { Project oldProj = context.Projects.Find(pId); if (oldProj == null) { return(NotFound()); } var entry = context.Entry(oldProj); context.EnsureDeepLoaded(entry); foreach (var file in oldProj.Files) { context.FileRefs.Remove(file); } oldProj.Files.Clear(); foreach (var projOwner in oldProj.OwnerUsers) { projOwner.Unjoin(context); } oldProj.Name = newProj.Name; oldProj.Description = newProj.Description; if (newProj.ValidStatuses != null) { oldProj.ValidStatuses = context.StatusSets.Find(newProj.ValidStatuses.Id); } else { oldProj.ValidStatuses = null; } foreach (var newOwner in newProjTup.Item2) { ProjectOwner.Join(context, oldProj, newOwner); } oldProj.Tags = newProj.Tags; foreach (var file in newProj.Files) { context.FileRefs.Add(file); newProj.Files.Add(file); } context.SaveChanges(); return(NoContent()); } }
public IActionResult CreateProject(string projectJson) { Tuple <Project, IList <User> > newProjTup = ProjectFromJson(projectJson); if (newProjTup == null) { return(BadRequest()); } Project newProj = newProjTup.Item1; using (var context = new TodoContext()) { foreach (var file in newProj.Files) { context.FileRefs.Add(file); } foreach (var newOwner in newProjTup.Item2) { ProjectOwner.Join(context, newProj, newOwner); } context.ChangeTracker.TrackGraph(newProj, (node => node.Entry.State = node.Entry.IsKeySet ? EntityState.Unchanged : EntityState.Added)); context.SaveChanges(); return(CreatedAtRoute("GetProject", new { id = newProj.Pid }, GetDetailedModel(context, newProj))); } }