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()); } }
/// <summary> /// Generate an abbreviated version of the project /// </summary> /// <param name="project">The project to parse</param> /// <returns>The abbreviated project, as an anonymous type</returns> public object GetShortModel(TodoContext context, Project project) { var entry = context.Entry(project); context.EnsureDeepLoaded(entry); long[] userIds = project.OwnerUsers.Select(owner => owner.UserId).ToArray(); return(new { Pid = project.Pid, Name = project.Name, OwnerUserIds = userIds, Tags = project.Tags }); }
public IActionResult GetUser(long userId) { using (var context = new TodoContext()) { var user = context.Users.Find(userId); if (user != null) { var entry = context.Entry(user); context.EnsureDeepLoaded(entry); return(Ok(GetDetailedModel(user))); } return(NotFound()); } }
public IActionResult GetTodoItem(long id) { using (var context = new TodoContext()) { var todoItem = context.TodoItems.Find(id); if (todoItem != null) { var ent = context.Entry(todoItem); context.EnsureDeepLoaded(ent); return(Ok(GetDetailedModel(context, todoItem))); } } return(new NotFoundResult()); }
/// <summary> /// Generate a more detailed version of the project /// </summary> /// <param name="project">The project to parse</param> /// <returns>The detailed object, as an anonymous type</returns> public object GetDetailedModel(TodoContext context, Project project) { var entry = context.Entry(project); context.EnsureDeepLoaded(entry); long validStatuses = -1; if (project.ValidStatuses != null) { validStatuses = project.ValidStatuses.Id; } long[] userIds = project.OwnerUsers.Select(owner => owner.UserId).ToArray(); string[] files = project.Files.Select(file => file.RepoUrl).ToArray(); return(new { Pid = project.Pid, Name = project.Name, Description = project.Description, StatusSetId = validStatuses, OwnerUserIds = userIds, Tags = project.Tags, Files = files }); }
public IActionResult DeleteProject(long pId) { using (var context = new TodoContext()) { Project oldProj = context.Projects.Find(pId); if (oldProj == null) { return(NotFound()); } context.EnsureDeepLoaded(context.Entry(oldProj)); foreach (var file in oldProj.Files) // FIXME: Doesn't trigger physical deletion { context.FileRefs.Remove(file); } for (int i = oldProj.OwnerUsers.Count - 1; i >= 0; i--) { oldProj.OwnerUsers[i].Unjoin(context); } oldProj.Files.Clear(); context.Projects.Remove(oldProj); context.SaveChanges(); return(NoContent()); } }