Skip to content

Commit dbbf1d1

Browse files
authored
Merge pull request #343 from TechnologyEnhancedLearning/Develop/Fixes/TD-3023
TD-3023 fix for deleted blocks which are valid in an active resource version
2 parents 56365f6 + 26abd61 commit dbbf1d1

File tree

2 files changed

+88
-38
lines changed

2 files changed

+88
-38
lines changed

LearningHub.Nhs.WebUI/Controllers/Api/ContributeController.cs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -493,12 +493,12 @@ public async Task<ActionResult> SaveWeblinkDetailAsync([FromBody] WebLinkViewMod
493493
public async Task<ActionResult> SaveCaseDetailAsync([FromBody] CaseViewModel request)
494494
{
495495
var existingResourceState = await this.resourceService.GetResourceVersionExtendedAsync(request.ResourceVersionId);
496-
int resourceVersionId = await this.contributeService.SaveCaseDetailAsync(request);
497496
if (existingResourceState.CaseDetails?.BlockCollection != null)
498497
{
499-
this.RemoveBlockCollectionFiles(existingResourceState.CaseDetails.BlockCollection, request.BlockCollection);
498+
await this.RemoveBlockCollectionFiles(request.ResourceVersionId, existingResourceState.CaseDetails.BlockCollection, request.BlockCollection);
500499
}
501500

501+
int resourceVersionId = await this.contributeService.SaveCaseDetailAsync(request);
502502
return this.Ok(resourceVersionId);
503503
}
504504

@@ -512,12 +512,12 @@ public async Task<ActionResult> SaveCaseDetailAsync([FromBody] CaseViewModel req
512512
public async Task<ActionResult> SaveAssessmentDetailAsync([FromBody] AssessmentViewModel request)
513513
{
514514
var existingResourceState = await this.resourceService.GetResourceVersionExtendedAsync(request.ResourceVersionId);
515-
int resourceVersionId = await this.contributeService.SaveAssessmentDetailAsync(request);
516515
if (existingResourceState != null && existingResourceState.AssessmentDetails != null)
517516
{
518-
this.RemoveBlockCollectionFiles(existingResourceState.AssessmentDetails, request);
517+
await this.RemoveBlockCollectionFiles(request.ResourceVersionId, existingResourceState.AssessmentDetails, request);
519518
}
520519

520+
int resourceVersionId = await this.contributeService.SaveAssessmentDetailAsync(request);
521521
return this.Ok(resourceVersionId);
522522
}
523523

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

641-
private void RemoveBlockCollectionFiles(AssessmentViewModel existingModel, AssessmentViewModel newModel)
641+
private async Task RemoveBlockCollectionFiles(int resourceVersionId, AssessmentViewModel existingModel, AssessmentViewModel newModel)
642642
{
643643
if (existingModel is { EndGuidance: { } } && existingModel.EndGuidance.Blocks != null)
644644
{
645-
this.RemoveBlockCollectionFiles(existingModel.EndGuidance, newModel.EndGuidance);
645+
await this.RemoveBlockCollectionFiles(resourceVersionId, existingModel.EndGuidance, newModel.EndGuidance);
646646
}
647647

648648
if (existingModel is { AssessmentContent: { } } && existingModel.AssessmentContent.Blocks != null)
649649
{
650-
this.RemoveBlockCollectionFiles(existingModel.AssessmentContent, newModel.AssessmentContent);
650+
await this.RemoveBlockCollectionFiles(resourceVersionId, existingModel.AssessmentContent, newModel.AssessmentContent);
651651
}
652652
}
653653

654-
private void RemoveBlockCollectionFiles(BlockCollectionViewModel existingResource, BlockCollectionViewModel newResource)
654+
private async Task RemoveBlockCollectionFiles(int resourceVersionId, BlockCollectionViewModel existingResource, BlockCollectionViewModel newResource)
655655
{
656656
try
657657
{
658+
var obsoleteFiles = await this.resourceService.GetObsoleteResourceFile(resourceVersionId, true);
658659
var filePaths = new List<string>();
659660
if (existingResource != null)
660661
{
@@ -706,8 +707,8 @@ private void RemoveBlockCollectionFiles(BlockCollectionViewModel existingResourc
706707
{
707708
foreach (var oldblock in existingImages)
708709
{
709-
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);
710-
if (entry == null)
710+
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);
711+
if (entry == null)
711712
{
712713
filePaths.Add(oldblock?.MediaBlock?.Image?.File?.FilePath);
713714
}
@@ -771,7 +772,18 @@ private void RemoveBlockCollectionFiles(BlockCollectionViewModel existingResourc
771772

772773
if (filePaths != null && filePaths.Any())
773774
{
774-
_ = Task.Run(async () => { await this.fileService.PurgeResourceFile(null, filePaths); });
775+
var deleteList = new List<string>();
776+
foreach (var e in filePaths)
777+
{
778+
if (!obsoleteFiles.Contains(e))
779+
{
780+
continue;
781+
}
782+
783+
deleteList.Add(e);
784+
}
785+
786+
_ = Task.Run(async () => { await this.fileService.PurgeResourceFile(null, deleteList); });
775787
}
776788
}
777789
catch (Exception ex)
@@ -838,12 +850,15 @@ private List<string> CheckQuestionBlock(BlockCollectionViewModel model)
838850
}
839851
else if (questionBlock.BlockType == BlockType.WholeSlideImage && questionBlock.WholeSlideImageBlock != null)
840852
{
841-
var existingWholeSlideImages = questionBlock.WholeSlideImageBlock.WholeSlideImageBlockItems.ToList();
842-
if (existingWholeSlideImages.Any())
853+
var existingWholeSlideImages = questionBlock.WholeSlideImageBlock?.WholeSlideImageBlockItems;
854+
if (existingWholeSlideImages != null && existingWholeSlideImages.Any())
843855
{
844856
foreach (var wsi in existingWholeSlideImages)
845857
{
846-
filePath.Add(wsi.WholeSlideImage?.File?.FilePath);
858+
if (wsi.WholeSlideImage != null && wsi.WholeSlideImage.File != null)
859+
{
860+
filePath.Add(wsi.WholeSlideImage.File.FilePath);
861+
}
847862
}
848863
}
849864
}

WebAPI/LearningHub.Nhs.Services/ResourceService.cs

Lines changed: 59 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,7 @@ public async Task<List<string>> GetObsoleteResourceFile(int resourceVersionId, b
14771477
{
14781478
if (resource.AssessmentDetails is { EndGuidance: { } } && resource.AssessmentDetails.EndGuidance.Blocks != null)
14791479
{
1480-
var assessmentFiles = this.CheckBlockFile(resource.AssessmentDetails.EndGuidance);
1480+
var assessmentFiles = this.CheckBlockFile(extendedResourceVersion.AssessmentDetails.EndGuidance, resource.AssessmentDetails.EndGuidance);
14811481
if (assessmentFiles.Any())
14821482
{
14831483
retVal.AddRange(assessmentFiles);
@@ -1486,7 +1486,7 @@ public async Task<List<string>> GetObsoleteResourceFile(int resourceVersionId, b
14861486

14871487
if (resource.AssessmentDetails is { AssessmentContent: { } } && resource.AssessmentDetails.AssessmentContent.Blocks != null)
14881488
{
1489-
var assessmentFiles = this.CheckBlockFile(resource.AssessmentDetails.AssessmentContent);
1489+
var assessmentFiles = this.CheckBlockFile(extendedResourceVersion.AssessmentDetails.AssessmentContent, resource.AssessmentDetails.AssessmentContent);
14901490
if (assessmentFiles.Any())
14911491
{
14921492
retVal.AddRange(assessmentFiles);
@@ -1498,7 +1498,7 @@ public async Task<List<string>> GetObsoleteResourceFile(int resourceVersionId, b
14981498
{
14991499
if (deletedResource)
15001500
{
1501-
var caseFiles = this.CheckBlockFile(resource.CaseDetails.BlockCollection);
1501+
var caseFiles = this.CheckBlockFile(extendedResourceVersion.CaseDetails.BlockCollection, resource.CaseDetails.BlockCollection);
15021502
if (caseFiles.Any())
15031503
{
15041504
retVal.AddRange(caseFiles);
@@ -1557,7 +1557,7 @@ public async Task<List<string>> GetObsoleteResourceFile(int resourceVersionId, b
15571557
{
15581558
if (resource.AssessmentDetails is { EndGuidance: { } } && resource.AssessmentDetails.EndGuidance.Blocks != null)
15591559
{
1560-
var assessmentFiles = this.CheckBlockFile(resource.AssessmentDetails.EndGuidance);
1560+
var assessmentFiles = this.CheckBlockFile(null, resource.AssessmentDetails.EndGuidance);
15611561
if (assessmentFiles.Any())
15621562
{
15631563
retVal.AddRange(assessmentFiles);
@@ -1566,7 +1566,7 @@ public async Task<List<string>> GetObsoleteResourceFile(int resourceVersionId, b
15661566

15671567
if (resource.AssessmentDetails is { AssessmentContent: { } } && resource.AssessmentDetails.AssessmentContent.Blocks != null)
15681568
{
1569-
var assessmentFiles = this.CheckBlockFile(resource.AssessmentDetails.AssessmentContent);
1569+
var assessmentFiles = this.CheckBlockFile(null, resource.AssessmentDetails.AssessmentContent);
15701570
if (assessmentFiles.Any())
15711571
{
15721572
retVal.AddRange(assessmentFiles);
@@ -1576,7 +1576,7 @@ public async Task<List<string>> GetObsoleteResourceFile(int resourceVersionId, b
15761576
}
15771577
else if (resource.ResourceTypeEnum == ResourceTypeEnum.Case)
15781578
{
1579-
var caseFiles = this.CheckBlockFile(resource.CaseDetails.BlockCollection);
1579+
var caseFiles = this.CheckBlockFile(null, resource.CaseDetails.BlockCollection);
15801580
if (caseFiles.Any())
15811581
{
15821582
retVal.AddRange(caseFiles);
@@ -4884,7 +4884,7 @@ private async Task<int> CreateNewResource(ResourceTypeEnum resourceType, string
48844884
return resourceVersionId;
48854885
}
48864886

4887-
private List<string> CheckBlockFile(BlockCollectionViewModel model)
4887+
private List<string> CheckBlockFile(BlockCollectionViewModel? publishedBlock, BlockCollectionViewModel model)
48884888
{
48894889
var retVal = new List<string>();
48904890
var caseBlockCollection = model;
@@ -4898,7 +4898,11 @@ private List<string> CheckBlockFile(BlockCollectionViewModel model)
48984898
{
48994899
foreach (var oldblock in existingAttachements)
49004900
{
4901-
retVal.Add(oldblock.MediaBlock.Attachment?.File?.FilePath);
4901+
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;
4902+
if (publishedEntry == null)
4903+
{
4904+
retVal.Add(oldblock.MediaBlock.Attachment?.File?.FilePath);
4905+
}
49024906
}
49034907
}
49044908

@@ -4907,19 +4911,23 @@ private List<string> CheckBlockFile(BlockCollectionViewModel model)
49074911
{
49084912
foreach (var oldblock in existingVideos)
49094913
{
4910-
if (!string.IsNullOrWhiteSpace(oldblock.MediaBlock.Video.File.FilePath))
4914+
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;
4915+
if (publishedEntry == null)
49114916
{
4912-
retVal.Add(oldblock.MediaBlock.Video.File.FilePath);
4913-
}
4917+
if (!string.IsNullOrWhiteSpace(oldblock.MediaBlock.Video.File.FilePath))
4918+
{
4919+
retVal.Add(oldblock.MediaBlock.Video.File.FilePath);
4920+
}
49144921

4915-
if (!string.IsNullOrWhiteSpace(oldblock.MediaBlock.Video?.File?.VideoFile?.File?.FilePath))
4916-
{
4917-
retVal.Add(oldblock.MediaBlock.Video.File.VideoFile.File.FilePath);
4918-
}
4922+
if (!string.IsNullOrWhiteSpace(oldblock.MediaBlock.Video?.File?.VideoFile?.File?.FilePath))
4923+
{
4924+
retVal.Add(oldblock.MediaBlock.Video.File.VideoFile.File.FilePath);
4925+
}
49194926

4920-
if (oldblock.MediaBlock?.Video?.File?.VideoFile?.TranscriptFile?.File?.FilePath != null)
4921-
{
4922-
retVal.Add(oldblock.MediaBlock.Video.File.VideoFile.TranscriptFile.File.FilePath);
4927+
if (oldblock.MediaBlock?.Video?.File?.VideoFile?.TranscriptFile?.File?.FilePath != null)
4928+
{
4929+
retVal.Add(oldblock.MediaBlock.Video.File.VideoFile.TranscriptFile.File.FilePath);
4930+
}
49234931
}
49244932
}
49254933
}
@@ -4929,7 +4937,11 @@ private List<string> CheckBlockFile(BlockCollectionViewModel model)
49294937
{
49304938
foreach (var oldblock in existingImages)
49314939
{
4932-
retVal.Add(oldblock.MediaBlock?.Image?.File?.FilePath);
4940+
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;
4941+
if (publishedEntry == null)
4942+
{
4943+
retVal.Add(oldblock.MediaBlock?.Image?.File?.FilePath);
4944+
}
49334945
}
49344946
}
49354947

@@ -4940,7 +4952,11 @@ private List<string> CheckBlockFile(BlockCollectionViewModel model)
49404952
{
49414953
foreach (var oldblock in imageBlock.ImageCarouselBlock.ImageBlockCollection.Blocks)
49424954
{
4943-
retVal.Add(oldblock.MediaBlock?.Image?.File?.FilePath);
4955+
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;
4956+
if (publishedEntry == null)
4957+
{
4958+
retVal.Add(oldblock.MediaBlock?.Image?.File?.FilePath);
4959+
}
49444960
}
49454961
}
49464962
}
@@ -4952,17 +4968,33 @@ private List<string> CheckBlockFile(BlockCollectionViewModel model)
49524968
{
49534969
foreach (var oldblock in wsi?.WholeSlideImageBlock?.WholeSlideImageBlockItems)
49544970
{
4955-
retVal.Add(oldblock.WholeSlideImage?.File?.FilePath);
4971+
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;
4972+
if (publishedEntry == null)
4973+
{
4974+
retVal.Add(oldblock.WholeSlideImage?.File?.FilePath);
4975+
}
49564976
}
49574977
}
49584978
}
49594979
}
49604980

49614981
var questionFiles = this.CheckQuestionBlock(caseBlockCollection);
4962-
if (questionFiles.Any())
4982+
var publishedQuestionFiles = (publishedBlock != null && publishedBlock.Blocks.Any()) ? this.CheckQuestionBlock(publishedBlock) : new List<string>();
4983+
if (questionFiles.Any() && !publishedQuestionFiles.Any())
49634984
{
49644985
retVal.AddRange(questionFiles);
49654986
}
4987+
else if (questionFiles.Any() && publishedQuestionFiles.Any())
4988+
{
4989+
foreach (var file in questionFiles)
4990+
{
4991+
var publishedEntry = publishedQuestionFiles.FirstOrDefault(x => x.Equals(file));
4992+
if (publishedEntry == null)
4993+
{
4994+
retVal.Add(file);
4995+
}
4996+
}
4997+
}
49664998
}
49674999

49685000
return retVal;
@@ -5027,12 +5059,15 @@ private List<string> CheckQuestionBlock(BlockCollectionViewModel model)
50275059
}
50285060
else if (questionBlock.BlockType == BlockType.WholeSlideImage && questionBlock.WholeSlideImageBlock != null)
50295061
{
5030-
var existingWholeSlideImages = questionBlock.WholeSlideImageBlock.WholeSlideImageBlockItems.ToList();
5062+
var existingWholeSlideImages = questionBlock.WholeSlideImageBlock.WholeSlideImageBlockItems;
50315063
if (existingWholeSlideImages.Any())
50325064
{
50335065
foreach (var wsi in existingWholeSlideImages)
50345066
{
5035-
filePath.Add(wsi.WholeSlideImage?.File?.FilePath);
5067+
if (wsi.WholeSlideImage != null && wsi.WholeSlideImage.File != null)
5068+
{
5069+
filePath.Add(wsi.WholeSlideImage.File.FilePath);
5070+
}
50365071
}
50375072
}
50385073
}

0 commit comments

Comments
 (0)