Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions LearningHub.Nhs.WebUI/Controllers/Api/ContributeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,12 @@ public async Task<ActionResult> SaveWeblinkDetailAsync([FromBody] WebLinkViewMod
public async Task<ActionResult> SaveCaseDetailAsync([FromBody] CaseViewModel request)
{
var existingResourceState = await this.resourceService.GetResourceVersionExtendedAsync(request.ResourceVersionId);
int resourceVersionId = await this.contributeService.SaveCaseDetailAsync(request);
if (existingResourceState.CaseDetails?.BlockCollection != null)
{
this.RemoveBlockCollectionFiles(existingResourceState.CaseDetails.BlockCollection, request.BlockCollection);
await this.RemoveBlockCollectionFiles(request.ResourceVersionId, existingResourceState.CaseDetails.BlockCollection, request.BlockCollection);
}

int resourceVersionId = await this.contributeService.SaveCaseDetailAsync(request);
return this.Ok(resourceVersionId);
}

Expand All @@ -512,12 +512,12 @@ public async Task<ActionResult> SaveCaseDetailAsync([FromBody] CaseViewModel req
public async Task<ActionResult> SaveAssessmentDetailAsync([FromBody] AssessmentViewModel request)
{
var existingResourceState = await this.resourceService.GetResourceVersionExtendedAsync(request.ResourceVersionId);
int resourceVersionId = await this.contributeService.SaveAssessmentDetailAsync(request);
if (existingResourceState != null && existingResourceState.AssessmentDetails != null)
{
this.RemoveBlockCollectionFiles(existingResourceState.AssessmentDetails, request);
await this.RemoveBlockCollectionFiles(request.ResourceVersionId, existingResourceState.AssessmentDetails, request);
}

int resourceVersionId = await this.contributeService.SaveAssessmentDetailAsync(request);
return this.Ok(resourceVersionId);
}

Expand Down Expand Up @@ -638,23 +638,24 @@ private async Task<bool> UserCanEditCatalogue(int catalogueId)
return await this.catalogueService.CanCurrentUserEditCatalogue(catalogueId);
}

private void RemoveBlockCollectionFiles(AssessmentViewModel existingModel, AssessmentViewModel newModel)
private async Task RemoveBlockCollectionFiles(int resourceVersionId, AssessmentViewModel existingModel, AssessmentViewModel newModel)
{
if (existingModel is { EndGuidance: { } } && existingModel.EndGuidance.Blocks != null)
{
this.RemoveBlockCollectionFiles(existingModel.EndGuidance, newModel.EndGuidance);
await this.RemoveBlockCollectionFiles(resourceVersionId, existingModel.EndGuidance, newModel.EndGuidance);
}

if (existingModel is { AssessmentContent: { } } && existingModel.AssessmentContent.Blocks != null)
{
this.RemoveBlockCollectionFiles(existingModel.AssessmentContent, newModel.AssessmentContent);
await this.RemoveBlockCollectionFiles(resourceVersionId, existingModel.AssessmentContent, newModel.AssessmentContent);
}
}

private void RemoveBlockCollectionFiles(BlockCollectionViewModel existingResource, BlockCollectionViewModel newResource)
private async Task RemoveBlockCollectionFiles(int resourceVersionId, BlockCollectionViewModel existingResource, BlockCollectionViewModel newResource)
{
try
{
var obsoleteFiles = await this.resourceService.GetObsoleteResourceFile(resourceVersionId, true);
var filePaths = new List<string>();
if (existingResource != null)
{
Expand Down Expand Up @@ -706,8 +707,8 @@ private void RemoveBlockCollectionFiles(BlockCollectionViewModel existingResourc
{
foreach (var oldblock in existingImages)
{
var entry = newBlocks.FirstOrDefault(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Image && x.MediaBlock.Image != null && x.MediaBlock?.Image?.File?.FileId == oldblock.MediaBlock?.Image?.File?.FileId);
if (entry == null)
var entry = newBlocks.FirstOrDefault(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Image && x.MediaBlock.Image != null && x.MediaBlock?.Image?.File?.FileId == oldblock.MediaBlock?.Image?.File?.FileId);
if (entry == null)
{
filePaths.Add(oldblock?.MediaBlock?.Image?.File?.FilePath);
}
Expand Down Expand Up @@ -771,7 +772,18 @@ private void RemoveBlockCollectionFiles(BlockCollectionViewModel existingResourc

if (filePaths != null && filePaths.Any())
{
_ = Task.Run(async () => { await this.fileService.PurgeResourceFile(null, filePaths); });
var deleteList = new List<string>();
foreach (var e in filePaths)
{
if (!obsoleteFiles.Contains(e))
{
continue;
}

deleteList.Add(e);
}

_ = Task.Run(async () => { await this.fileService.PurgeResourceFile(null, deleteList); });
}
}
catch (Exception ex)
Expand Down Expand Up @@ -838,12 +850,15 @@ private List<string> CheckQuestionBlock(BlockCollectionViewModel model)
}
else if (questionBlock.BlockType == BlockType.WholeSlideImage && questionBlock.WholeSlideImageBlock != null)
{
var existingWholeSlideImages = questionBlock.WholeSlideImageBlock.WholeSlideImageBlockItems.ToList();
if (existingWholeSlideImages.Any())
var existingWholeSlideImages = questionBlock.WholeSlideImageBlock?.WholeSlideImageBlockItems;
if (existingWholeSlideImages != null && existingWholeSlideImages.Any())
{
foreach (var wsi in existingWholeSlideImages)
{
filePath.Add(wsi.WholeSlideImage?.File?.FilePath);
if (wsi.WholeSlideImage != null && wsi.WholeSlideImage.File != null)
{
filePath.Add(wsi.WholeSlideImage.File.FilePath);
}
}
}
}
Expand Down
83 changes: 59 additions & 24 deletions WebAPI/LearningHub.Nhs.Services/ResourceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1477,7 +1477,7 @@ public async Task<List<string>> GetObsoleteResourceFile(int resourceVersionId, b
{
if (resource.AssessmentDetails is { EndGuidance: { } } && resource.AssessmentDetails.EndGuidance.Blocks != null)
{
var assessmentFiles = this.CheckBlockFile(resource.AssessmentDetails.EndGuidance);
var assessmentFiles = this.CheckBlockFile(extendedResourceVersion.AssessmentDetails.EndGuidance, resource.AssessmentDetails.EndGuidance);
if (assessmentFiles.Any())
{
retVal.AddRange(assessmentFiles);
Expand All @@ -1486,7 +1486,7 @@ public async Task<List<string>> GetObsoleteResourceFile(int resourceVersionId, b

if (resource.AssessmentDetails is { AssessmentContent: { } } && resource.AssessmentDetails.AssessmentContent.Blocks != null)
{
var assessmentFiles = this.CheckBlockFile(resource.AssessmentDetails.AssessmentContent);
var assessmentFiles = this.CheckBlockFile(extendedResourceVersion.AssessmentDetails.AssessmentContent, resource.AssessmentDetails.AssessmentContent);
if (assessmentFiles.Any())
{
retVal.AddRange(assessmentFiles);
Expand All @@ -1498,7 +1498,7 @@ public async Task<List<string>> GetObsoleteResourceFile(int resourceVersionId, b
{
if (deletedResource)
{
var caseFiles = this.CheckBlockFile(resource.CaseDetails.BlockCollection);
var caseFiles = this.CheckBlockFile(extendedResourceVersion.CaseDetails.BlockCollection, resource.CaseDetails.BlockCollection);
if (caseFiles.Any())
{
retVal.AddRange(caseFiles);
Expand Down Expand Up @@ -1557,7 +1557,7 @@ public async Task<List<string>> GetObsoleteResourceFile(int resourceVersionId, b
{
if (resource.AssessmentDetails is { EndGuidance: { } } && resource.AssessmentDetails.EndGuidance.Blocks != null)
{
var assessmentFiles = this.CheckBlockFile(resource.AssessmentDetails.EndGuidance);
var assessmentFiles = this.CheckBlockFile(null, resource.AssessmentDetails.EndGuidance);
if (assessmentFiles.Any())
{
retVal.AddRange(assessmentFiles);
Expand All @@ -1566,7 +1566,7 @@ public async Task<List<string>> GetObsoleteResourceFile(int resourceVersionId, b

if (resource.AssessmentDetails is { AssessmentContent: { } } && resource.AssessmentDetails.AssessmentContent.Blocks != null)
{
var assessmentFiles = this.CheckBlockFile(resource.AssessmentDetails.AssessmentContent);
var assessmentFiles = this.CheckBlockFile(null, resource.AssessmentDetails.AssessmentContent);
if (assessmentFiles.Any())
{
retVal.AddRange(assessmentFiles);
Expand All @@ -1576,7 +1576,7 @@ public async Task<List<string>> GetObsoleteResourceFile(int resourceVersionId, b
}
else if (resource.ResourceTypeEnum == ResourceTypeEnum.Case)
{
var caseFiles = this.CheckBlockFile(resource.CaseDetails.BlockCollection);
var caseFiles = this.CheckBlockFile(null, resource.CaseDetails.BlockCollection);
if (caseFiles.Any())
{
retVal.AddRange(caseFiles);
Expand Down Expand Up @@ -4884,7 +4884,7 @@ private async Task<int> CreateNewResource(ResourceTypeEnum resourceType, string
return resourceVersionId;
}

private List<string> CheckBlockFile(BlockCollectionViewModel model)
private List<string> CheckBlockFile(BlockCollectionViewModel? publishedBlock, BlockCollectionViewModel model)
{
var retVal = new List<string>();
var caseBlockCollection = model;
Expand All @@ -4898,7 +4898,11 @@ private List<string> CheckBlockFile(BlockCollectionViewModel model)
{
foreach (var oldblock in existingAttachements)
{
retVal.Add(oldblock.MediaBlock.Attachment?.File?.FilePath);
var publishedEntry = (publishedBlock != null && publishedBlock.Blocks.Any()) ? publishedBlock.Blocks.FirstOrDefault(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Attachment && x.MediaBlock.Attachment != null && x.MediaBlock.Attachment.File?.FileId == oldblock.MediaBlock.Attachment?.File?.FileId) : null;
if (publishedEntry == null)
{
retVal.Add(oldblock.MediaBlock.Attachment?.File?.FilePath);
}
}
}

Expand All @@ -4907,19 +4911,23 @@ private List<string> CheckBlockFile(BlockCollectionViewModel model)
{
foreach (var oldblock in existingVideos)
{
if (!string.IsNullOrWhiteSpace(oldblock.MediaBlock.Video.File.FilePath))
var publishedEntry = (publishedBlock != null && publishedBlock.Blocks.Any()) ? publishedBlock.Blocks.FirstOrDefault(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Video && x.MediaBlock.Video != null && x.MediaBlock.Video.VideoFile?.File?.FileId == oldblock.MediaBlock?.Video?.VideoFile?.File?.FileId) : null;
if (publishedEntry == null)
{
retVal.Add(oldblock.MediaBlock.Video.File.FilePath);
}
if (!string.IsNullOrWhiteSpace(oldblock.MediaBlock.Video.File.FilePath))
{
retVal.Add(oldblock.MediaBlock.Video.File.FilePath);
}

if (!string.IsNullOrWhiteSpace(oldblock.MediaBlock.Video?.File?.VideoFile?.File?.FilePath))
{
retVal.Add(oldblock.MediaBlock.Video.File.VideoFile.File.FilePath);
}
if (!string.IsNullOrWhiteSpace(oldblock.MediaBlock.Video?.File?.VideoFile?.File?.FilePath))
{
retVal.Add(oldblock.MediaBlock.Video.File.VideoFile.File.FilePath);
}

if (oldblock.MediaBlock?.Video?.File?.VideoFile?.TranscriptFile?.File?.FilePath != null)
{
retVal.Add(oldblock.MediaBlock.Video.File.VideoFile.TranscriptFile.File.FilePath);
if (oldblock.MediaBlock?.Video?.File?.VideoFile?.TranscriptFile?.File?.FilePath != null)
{
retVal.Add(oldblock.MediaBlock.Video.File.VideoFile.TranscriptFile.File.FilePath);
}
}
}
}
Expand All @@ -4929,7 +4937,11 @@ private List<string> CheckBlockFile(BlockCollectionViewModel model)
{
foreach (var oldblock in existingImages)
{
retVal.Add(oldblock.MediaBlock?.Image?.File?.FilePath);
var publishedEntry = (publishedBlock != null && publishedBlock.Blocks.Any()) ? publishedBlock.Blocks.FirstOrDefault(x => x.BlockType == BlockType.Media && x.MediaBlock != null && x.MediaBlock.MediaType == MediaType.Image && x.MediaBlock.Image != null && x.MediaBlock?.Image?.File?.FileId == oldblock.MediaBlock?.Image?.File?.FileId) : null;
if (publishedEntry == null)
{
retVal.Add(oldblock.MediaBlock?.Image?.File?.FilePath);
}
}
}

Expand All @@ -4940,7 +4952,11 @@ private List<string> CheckBlockFile(BlockCollectionViewModel model)
{
foreach (var oldblock in imageBlock.ImageCarouselBlock.ImageBlockCollection.Blocks)
{
retVal.Add(oldblock.MediaBlock?.Image?.File?.FilePath);
var publishedEntry = (publishedBlock != null && publishedBlock.Blocks.Any()) ? publishedBlock.Blocks.FirstOrDefault(x => x.BlockType == BlockType.ImageCarousel && x.ImageCarouselBlock != null && x.ImageCarouselBlock.ImageBlockCollection != null && x.ImageCarouselBlock.ImageBlockCollection.Blocks != null && x.ImageCarouselBlock.ImageBlockCollection.Blocks.Where(x => x.MediaBlock?.Image?.File?.FileId == oldblock.MediaBlock?.Image?.File?.FileId).Any()) : null;
if (publishedEntry == null)
{
retVal.Add(oldblock.MediaBlock?.Image?.File?.FilePath);
}
}
}
}
Expand All @@ -4952,17 +4968,33 @@ private List<string> CheckBlockFile(BlockCollectionViewModel model)
{
foreach (var oldblock in wsi?.WholeSlideImageBlock?.WholeSlideImageBlockItems)
{
retVal.Add(oldblock.WholeSlideImage?.File?.FilePath);
var publishedEntry = (publishedBlock != null && publishedBlock.Blocks.Any()) ? publishedBlock.Blocks.FirstOrDefault(x => x.WholeSlideImageBlock != null && x.WholeSlideImageBlock.WholeSlideImageBlockItems.Where(x => x.WholeSlideImage?.File?.FileId == oldblock.WholeSlideImage?.File?.FileId).Any()) : null;
if (publishedEntry == null)
{
retVal.Add(oldblock.WholeSlideImage?.File?.FilePath);
}
}
}
}
}

var questionFiles = this.CheckQuestionBlock(caseBlockCollection);
if (questionFiles.Any())
var publishedQuestionFiles = (publishedBlock != null && publishedBlock.Blocks.Any()) ? this.CheckQuestionBlock(publishedBlock) : new List<string>();
if (questionFiles.Any() && !publishedQuestionFiles.Any())
{
retVal.AddRange(questionFiles);
}
else if (questionFiles.Any() && publishedQuestionFiles.Any())
{
foreach (var file in questionFiles)
{
var publishedEntry = publishedQuestionFiles.FirstOrDefault(x => x.Equals(file));
if (publishedEntry == null)
{
retVal.Add(file);
}
}
}
}

return retVal;
Expand Down Expand Up @@ -5027,12 +5059,15 @@ private List<string> CheckQuestionBlock(BlockCollectionViewModel model)
}
else if (questionBlock.BlockType == BlockType.WholeSlideImage && questionBlock.WholeSlideImageBlock != null)
{
var existingWholeSlideImages = questionBlock.WholeSlideImageBlock.WholeSlideImageBlockItems.ToList();
var existingWholeSlideImages = questionBlock.WholeSlideImageBlock.WholeSlideImageBlockItems;
if (existingWholeSlideImages.Any())
{
foreach (var wsi in existingWholeSlideImages)
{
filePath.Add(wsi.WholeSlideImage?.File?.FilePath);
if (wsi.WholeSlideImage != null && wsi.WholeSlideImage.File != null)
{
filePath.Add(wsi.WholeSlideImage.File.FilePath);
}
}
}
}
Expand Down