/// <summary> /// Deletes the items from the database. /// </summary> /// <param name="serviceProvider">The application service provider.</param> /// <param name="token">The cancellation token for the task.</param> public async Task DeleteAsync(IServiceProvider serviceProvider, CancellationToken token) { // Check if there weren't any valid items found. if (Items == null) { // Throw an exception. throw new TaskException("No valid items could be found with the provided data."); } // Get the total number of batches. var count = Math.Ceiling((double)Items.Count() / ApplicationDbContext.BatchSize); // Go over each batch. for (var index = 0; index < count; index++) { // Check if the cancellation was requested. if (token.IsCancellationRequested) { // Break. break; } // Get the items in the current batch. var batchItems = Items .Skip(index * ApplicationDbContext.BatchSize) .Take(ApplicationDbContext.BatchSize); // Get the IDs of the items in the current batch. var batchIds = batchItems.Select(item => item.Id); // Define the list of items to get. var controlPaths = new List <ControlPath>(); // Use a new scope. using (var scope = serviceProvider.CreateScope()) { // Use a new context instance. using var context = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>(); // Get the items with the provided IDs. var items = context.ControlPaths .Where(item => batchIds.Contains(item.Id)); // Check if there were no items found. if (items == null || !items.Any()) { // Continue. continue; } // Get the items found. controlPaths = items .ToList(); } // Get the IDs of the items. var controlPathIds = controlPaths .Select(item => item.Id); // Delete the dependent entities. await ControlPathExtensions.DeleteDependentAnalysesAsync(controlPathIds, serviceProvider, token); // Delete the items. await IEnumerableExtensions.DeleteAsync(controlPaths, serviceProvider, token); } }
public async Task <IActionResult> OnPostAsync() { // Get the current user. var user = await _userManager.GetUserAsync(User); // Check if there aren't any IDs provided. if (Input.Ids == null || !Input.Ids.Any()) { // Display a message. TempData["StatusMessage"] = "Error: No or invalid IDs have been provided."; // Redirect to the index page. return(RedirectToPage("/Content/DatabaseTypes/PPI/Created/Analyses/Index")); } // Get the item with the provided ID. var items = _context.ControlPaths .Where(item => item.Analysis.AnalysisDatabases.Any(item1 => item1.Database.DatabaseType.Name == "PPI")) .Where(item => item.Analysis.IsPublic || item.Analysis.AnalysisUsers.Any(item1 => item1.User == user)) .Where(item => Input.Ids.Contains(item.Id)); // Check if there was no item found. if (items == null || !items.Any()) { // Display a message. TempData["StatusMessage"] = "Error: No control paths have been found with the provided ID, or you don't have access to them."; // Redirect to the index page. return(RedirectToPage("/Content/DatabaseTypes/PPI/Created/Analyses/Index")); } // Define the view. View = new ViewModel { Analysis = items .Select(item => item.Analysis) .First(), SourceNodes = items .Select(item => item.Analysis.AnalysisNodes) .SelectMany(item => item) .Where(item => item.Type == AnalysisNodeType.Source) .Select(item => item.Node) .ToHashSet(), Items = items .Include(item => item.Paths) .ThenInclude(item => item.PathNodes) .ThenInclude(item => item.Node) }; // Check if the reCaptcha is valid. if (!await _reCaptchaChecker.IsValid(Input.ReCaptchaToken)) { // Add an error to the model. ModelState.AddModelError(string.Empty, "The reCaptcha verification failed."); // Return the page. return(Page()); } // Check if the provided model isn't valid. if (!ModelState.IsValid) { // Add an error to the model. ModelState.AddModelError(string.Empty, "An error has been encountered. Please check again the input fields."); // Redisplay the page. return(Page()); } // Return the streamed file. return(new FileCallbackResult(MediaTypeNames.Application.Zip, async(zipStream, _) => { // Define a new ZIP archive. using var archive = new ZipArchive(zipStream, ZipArchiveMode.Create); // Check if the overview file should be added. if (true) { // Create a new entry in the archive and open it. using var stream = archive.CreateEntry($"Control-Paths-List.txt", CompressionLevel.Fastest).Open(); // Write to the entry the corresponding file content. await ControlPathExtensions.WriteToStreamOverviewTextFileContent(View.Items.Select(item => item.Id), stream, _serviceProvider, HttpContext.Request.Scheme, HttpContext.Request.Host); } // Check which should be the format of the files within the archive. if (Input.FileFormat == "txt") { // Go over each of the analyses to download. foreach (var controlPath in View.Items) { // Create a new entry in the archive and open it. using var stream = archive.CreateEntry($"Control-Path-{controlPath.Analysis.Name.Replace(" ", "-")}-{controlPath.Id}.txt", CompressionLevel.Fastest).Open(); // Write to the entry the corresponding file content. await controlPath.WriteToStreamTxtFileContent(stream, _serviceProvider); } } else if (Input.FileFormat == "sif") { // Go over each of the analyses to download. foreach (var controlPath in View.Items) { // Create a new entry in the archive and open it. using var stream = archive.CreateEntry($"Control-Path-{controlPath.Analysis.Name.Replace(" ", "-")}-{controlPath.Id}.sif", CompressionLevel.Fastest).Open(); // Write to the entry the corresponding file content. await controlPath.WriteToStreamSifFileContent(stream, _serviceProvider); } } else if (Input.FileFormat == "json") { // Go over each of the analyses to download. foreach (var controlPath in View.Items) { // Create a new entry in the archive and open it. using var stream = archive.CreateEntry($"Control-Path-{controlPath.Analysis.Name.Replace(" ", "-")}-{controlPath.Id}.json", CompressionLevel.Fastest).Open(); // Write to the entry the corresponding file content. await controlPath.WriteToStreamJsonFileContent(stream, _serviceProvider); } } else if (Input.FileFormat == "cyjs") { // Go over each of the analyses to download. foreach (var controlPath in View.Items) { // Create a new entry in the archive and open it. using var stream = archive.CreateEntry($"Control-Path-{controlPath.Analysis.Name.Replace(" ", "-")}-{controlPath.Id}.cyjs", CompressionLevel.Fastest).Open(); // Write to the entry the corresponding file content. await controlPath.WriteToStreamCyjsFileContent(stream, _serviceProvider); } } else if (Input.FileFormat == "xlsx") { // Go over each of the analyses to download. foreach (var controlPath in View.Items) { // Create a new entry in the archive and open it. using var stream = archive.CreateEntry($"Control-Path-{controlPath.Analysis.Name.Replace(" ", "-")}-{controlPath.Id}.xlsx", CompressionLevel.Fastest).Open(); // Write to the entry the corresponding file content. await controlPath.WriteToStreamXlsxFileContent(stream, _serviceProvider); } } }) { FileDownloadName = $"NetControl4BioMed-Control-Paths.zip" });