public async Task <Results <ProjectVm> > GetAsync(int projectId) { var results = new Results <ProjectVm>(); try { var project = await _projectDataContext.DataSet .Include(p => p.ProjectReferences) .FirstAsync(p => p.Id == projectId); if (project.UserId != await _getCurrentUserProcessor.GetUserIdAsync()) { throw new Exception(); } results.Data.Id = project.Id; results.Data.Name = project.Name; results.Data.NumberOfReferences = project.ProjectReferences.Count(); } catch (Exception e) { results.AddException(new Exception("Could not load the project", e)); } return(results); }
public async Task <Results <bool> > HideResultAsync(int projectId, string documentId) { var results = new Results <bool>(); try { var projectResults = await _getProjectForUserProcessor.GetAsync(projectId); if (results.HasProblem) { results.Merge(projectResults); return(results); } if (projectResults.Data.ProjectHiddenResults.All(ph => ph.ReferenceId != documentId)) { projectResults.Data.ProjectHiddenResults.Add(new ProjectHiddenResult { ReferenceId = documentId }); await _projectContext.SaveChangesAsync(); } results.Data = true; } catch (System.Exception e) { results.AddException(new System.Exception("Failed to hide result.", e)); } return(results); }
public async Task <Results <List <ReferenceVm> > > GetCompletedScrapesAsync(int projectId, IEnumerable <string> documentIds) { var results = new Results <List <ReferenceVm> >(); try { var projectResults = await _getProjectForUserProcessor.GetAsync(projectId); if (projectResults.HasProblem) { results.Merge(projectResults); return(results); } var completedIds = documentIds.Where(d => projectResults.Data.ProjectReferences.Any(pr => pr.ReferenceId == d && !pr.IsPending)); var documents = await _getDocumentProcessor.GetDocumentsAsync(completedIds); results.Data = documents.Select(d => new ReferenceVm { Id = d.Id, Title = d.Title, Abstract = d.Abstract, IsPending = false }).ToList(); } catch (System.Exception e) { results.AddException(new System.Exception("Failed to check for completed scrapes.", e)); } return(results); }
public async Task <Results <bool> > DeleteAsync(int projectId) { var results = new Results <bool>(); var project = await _projectDataContext.DataSet.FirstOrDefaultAsync(p => p.Id == projectId); try { var userId = await _getCurrentUserProcessor.GetUserIdAsync(); if (project == null || project.UserId != userId) { throw new Exception(); } _projectDataContext.DataSet.Remove(project); await _projectDataContext.SaveChangesAsync(); results.Data = true; } catch (System.Exception e) { results.AddException(new Exception("Could not delete this project", e)); } return(results); }
public async Task <Results <List <string> > > AutoCompleteKeywordsAsync(string term, int resultsCount) { var results = new Results <List <string> >(); try { results.Data = await _documentContext.AutoCompleteKeywordsAsync(term, resultsCount); } catch (Exception e) { results.AddException(new Exception("Failed to autocomplete keywords.", e)); } return(results); }
public async Task <Results <ProjectVm> > SaveAsync(ProjectSaveData saveData) { var results = new Results <ProjectVm>(); try { var userId = await _getCurrentUserProcessor.GetUserIdAsync(); var user = await _userContext.DataSet .Include(u => u.Projects) .FirstAsync(u => u.Id == userId); var project = user .Projects .FirstOrDefault(p => p.Id == saveData.Id); if (saveData.Id == null) { project = new Project() { UserId = userId }; user.Projects.Add(project); } else if (project == null || project.UserId != userId) { results.AddError("This project could not be found or you do not own this project"); } if (results.HasError) { return(results); } project.Name = saveData.Name; await _userContext.SaveChangesAsync(); results.Data.Id = project.Id; results.Data.Name = project.Name; results.Data.NumberOfReferences = project.ProjectReferences.Count(); } catch (Exception e) { results.AddException(new Exception("Could not save this project", e)); } return(results); }
public async Task <Results <List <string> > > AddAsync(int projectId, IEnumerable <string> documentIds) { var documentIdList = documentIds.ToList(); var results = new Results <List <string> >(); try { var projectResults = await _getProjectForUserProcessor.GetAsync(projectId); if (results.HasProblem) { results.Merge(projectResults); return(results); } List <string> documentsToAdd = documentIdList.Except(projectResults.Data.ProjectReferences.Select(pr => pr.ReferenceId)).ToList(); projectResults.Data.ProjectReferences.AddRange(documentsToAdd.Select(d => new ProjectReference { ReferenceId = d })); await _projectContext.SaveChangesAsync(); foreach (var d in documentsToAdd) { _queueManager.QueueArticleScrape(d, _scrapeDepth); } results.Data = documentsToAdd; } catch (System.Exception e) { results.AddException(new System.Exception("Failed to add this reference", e)); } return(results); }
public async Task <Results <bool> > RemoveAsync(int projectId, string documentId) { var results = new Results <bool>(); try { var projectResults = await _getProjectForUserProcessor.GetAsync(projectId); if (results.HasProblem) { results.Merge(projectResults); return(results); } var toRemove = projectResults.Data.ProjectReferences .FirstOrDefault(pr => pr.ReferenceId == documentId); if (toRemove == null) { results.AddError("Your project does not have that reference"); } if (results.HasProblem) { return(results); } projectResults.Data.ProjectReferences.Remove(toRemove); await _projectContext.SaveChangesAsync(); results.Data = true; } catch (System.Exception e) { results.AddException(new System.Exception("Failed to add this reference", e)); } return(results); }
public async Task <Results <List <ReferenceVm> > > GetAllAsync(int projectId) { var results = new Results <List <ReferenceVm> >(); try { var projectResults = await _getProjectForUserProcessor.GetAsync(projectId); if (projectResults.HasProblem) { results.Merge(projectResults); return(results); } foreach (ProjectReference pendingReferences in projectResults.Data.ProjectReferences.Where(pr => pr.IsPending)) { _queueManager.QueueArticleScrape(pendingReferences.ReferenceId, _scrapeDepth); } var documents = await _getDocumentProcessor.GetDocumentsAsync(projectResults.Data.ProjectReferences .Select(pr => pr.ReferenceId)); results.Data = projectResults.Data.ProjectReferences.Join(documents, l => l.ReferenceId, r => r.Id, (l, r) => new ReferenceVm { Id = l.ReferenceId, Title = r.Title, Abstract = r.Abstract, IsPending = l.IsPending }).ToList(); } catch (System.Exception e) { results.AddException(new System.Exception("Failed to get your references", e)); } return(results); }