77 using System . Text . RegularExpressions ;
88 using System . Threading . Tasks ;
99 using System . Web ;
10+ using LearningHub . Nhs . Models . Entities . Resource ;
1011 using LearningHub . Nhs . Models . Enums ;
1112 using LearningHub . Nhs . Models . Resource ;
1213 using LearningHub . Nhs . Models . Resource . Blocks ;
@@ -493,7 +494,11 @@ public async Task<ActionResult> SaveCaseDetailAsync([FromBody] CaseViewModel req
493494 {
494495 var existingResourceState = await this . resourceService . GetResourceVersionExtendedAsync ( request . ResourceVersionId ) ;
495496 int resourceVersionId = await this . contributeService . SaveCaseDetailAsync ( request ) ;
496- this . RemoveDeletedCaseFiles ( existingResourceState ? . CaseDetails , request ) ;
497+ if ( existingResourceState . CaseDetails ? . BlockCollection != null )
498+ {
499+ this . RemoveBlockCollectionFiles ( existingResourceState . CaseDetails . BlockCollection , request . BlockCollection ) ;
500+ }
501+
497502 return this . Ok ( resourceVersionId ) ;
498503 }
499504
@@ -506,7 +511,13 @@ public async Task<ActionResult> SaveCaseDetailAsync([FromBody] CaseViewModel req
506511 [ Route ( "SaveAssessmentDetail" ) ]
507512 public async Task < ActionResult > SaveAssessmentDetailAsync ( [ FromBody ] AssessmentViewModel request )
508513 {
514+ var existingResourceState = await this . resourceService . GetResourceVersionExtendedAsync ( request . ResourceVersionId ) ;
509515 int resourceVersionId = await this . contributeService . SaveAssessmentDetailAsync ( request ) ;
516+ if ( existingResourceState != null && existingResourceState . AssessmentDetails != null )
517+ {
518+ this . RemoveBlockCollectionFiles ( existingResourceState . AssessmentDetails , request ) ;
519+ }
520+
510521 return this . Ok ( resourceVersionId ) ;
511522 }
512523
@@ -627,15 +638,28 @@ private async Task<bool> UserCanEditCatalogue(int catalogueId)
627638 return await this . catalogueService . CanCurrentUserEditCatalogue ( catalogueId ) ;
628639 }
629640
630- private void RemoveDeletedCaseFiles ( CaseViewModel existingResource , CaseViewModel newResource )
641+ private void RemoveBlockCollectionFiles ( AssessmentViewModel existingModel , AssessmentViewModel newModel )
642+ {
643+ if ( existingModel is { EndGuidance : { } } && existingModel . EndGuidance . Blocks != null )
644+ {
645+ this . RemoveBlockCollectionFiles ( existingModel . EndGuidance , newModel . EndGuidance ) ;
646+ }
647+
648+ if ( existingModel is { AssessmentContent : { } } && existingModel . AssessmentContent . Blocks != null )
649+ {
650+ this . RemoveBlockCollectionFiles ( existingModel . AssessmentContent , newModel . AssessmentContent ) ;
651+ }
652+ }
653+
654+ private void RemoveBlockCollectionFiles ( BlockCollectionViewModel existingResource , BlockCollectionViewModel newResource )
631655 {
632656 try
633657 {
634658 var filePaths = new List < string > ( ) ;
635- if ( existingResource != null && existingResource . BlockCollection != null )
659+ if ( existingResource != null )
636660 {
637- var allBlocks = existingResource . BlockCollection . Blocks . ToList ( ) ;
638- var newBlocks = newResource . BlockCollection . Blocks . ToList ( ) ;
661+ var allBlocks = existingResource . Blocks . ToList ( ) ;
662+ var newBlocks = newResource . Blocks . ToList ( ) ;
639663 if ( allBlocks . Any ( ) )
640664 {
641665 var existingAttachements = allBlocks . Where ( x => x . BlockType == BlockType . Media && x . MediaBlock != null && x . MediaBlock . MediaType == MediaType . Attachment && x . MediaBlock . Attachment != null ) . ToList ( ) ;
@@ -659,10 +683,19 @@ private void RemoveDeletedCaseFiles(CaseViewModel existingResource, CaseViewMode
659683 var entry = newBlocks . 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 ) ;
660684 if ( entry == null )
661685 {
662- filePaths . Add ( oldblock . MediaBlock . Video ? . VideoFile ? . File ? . FilePath ) ;
663- if ( oldblock . MediaBlock ? . Video ? . VideoFile ? . TranscriptFile ? . File ? . FilePath != null )
686+ if ( ! string . IsNullOrWhiteSpace ( oldblock . MediaBlock . Video . File . FilePath ) )
687+ {
688+ filePaths . Add ( oldblock . MediaBlock . Video . File . FilePath ) ;
689+ }
690+
691+ if ( ! string . IsNullOrWhiteSpace ( oldblock . MediaBlock . Video ? . File ? . VideoFile ? . File ? . FilePath ) )
664692 {
665- filePaths . Add ( oldblock . MediaBlock . Video ? . VideoFile ? . TranscriptFile ? . File ? . FilePath ) ;
693+ filePaths . Add ( oldblock . MediaBlock . Video . File . VideoFile . File . FilePath ) ;
694+ }
695+
696+ if ( oldblock . MediaBlock ? . Video ? . File ? . VideoFile ? . TranscriptFile ? . File ? . FilePath != null )
697+ {
698+ filePaths . Add ( oldblock . MediaBlock . Video . File . VideoFile . TranscriptFile . File . FilePath ) ;
666699 }
667700 }
668701 }
@@ -713,6 +746,27 @@ private void RemoveDeletedCaseFiles(CaseViewModel existingResource, CaseViewMode
713746 }
714747 }
715748 }
749+
750+ var existingQuestionFiles = this . CheckQuestionBlock ( existingResource ) ;
751+ var newQuestionFiles = this . CheckQuestionBlock ( newResource ) ;
752+ if ( existingQuestionFiles . Any ( ) )
753+ {
754+ if ( ! newQuestionFiles . Any ( ) )
755+ {
756+ filePaths . AddRange ( existingQuestionFiles ) ;
757+ }
758+ else
759+ {
760+ foreach ( var file in existingQuestionFiles )
761+ {
762+ var entry = newQuestionFiles . FirstOrDefault ( x => x . Equals ( file ) ) ;
763+ if ( entry == null )
764+ {
765+ filePaths . Add ( file ) ;
766+ }
767+ }
768+ }
769+ }
716770 }
717771
718772 if ( filePaths != null && filePaths . Any ( ) )
@@ -724,5 +778,117 @@ private void RemoveDeletedCaseFiles(CaseViewModel existingResource, CaseViewMode
724778 {
725779 }
726780 }
781+
782+ private List < string > CheckQuestionBlock ( BlockCollectionViewModel model )
783+ {
784+ var filePath = new List < string > ( ) ;
785+ foreach ( var block in model . Blocks )
786+ {
787+ if ( block . BlockType == BlockType . Question && block . QuestionBlock != null )
788+ {
789+ if ( block . QuestionBlock . QuestionType == QuestionTypeEnum . MatchGame && block . QuestionBlock . Answers != null )
790+ {
791+ foreach ( var answerBlock in block . QuestionBlock . Answers )
792+ {
793+ if ( answerBlock . BlockCollection != null && answerBlock . BlockCollection . Blocks != null )
794+ {
795+ foreach ( var imageBlock in answerBlock . BlockCollection . Blocks )
796+ {
797+ if ( imageBlock . BlockType == BlockType . Media && imageBlock . MediaBlock != null )
798+ {
799+ filePath . Add ( imageBlock . MediaBlock . Image . File . FilePath ) ;
800+ }
801+ }
802+ }
803+ }
804+ }
805+
806+ var questionBlockCollection = block . QuestionBlock . QuestionBlockCollection ;
807+ if ( questionBlockCollection != null && questionBlockCollection . Blocks != null )
808+ {
809+ foreach ( var questionBlock in questionBlockCollection . Blocks )
810+ {
811+ if ( questionBlock . BlockType == BlockType . Media && questionBlock . MediaBlock != null )
812+ {
813+ if ( questionBlock . MediaBlock . Image != null )
814+ {
815+ filePath . Add ( questionBlock . MediaBlock . Image . File . FilePath ) ;
816+ }
817+
818+ if ( questionBlock . MediaBlock . Video != null )
819+ {
820+ if ( questionBlock . MediaBlock . Video . File != null )
821+ {
822+ filePath . Add ( questionBlock . MediaBlock . Video . File . FilePath ) ;
823+ }
824+
825+ if ( questionBlock . MediaBlock . Video . VideoFile != null )
826+ {
827+ if ( questionBlock . MediaBlock . Video . VideoFile . TranscriptFile != null )
828+ {
829+ filePath . Add ( questionBlock . MediaBlock . Video . VideoFile . TranscriptFile . File . FilePath ) ;
830+ }
831+
832+ if ( questionBlock . MediaBlock . Video . VideoFile . CaptionsFile != null )
833+ {
834+ filePath . Add ( questionBlock . MediaBlock . Video . VideoFile . CaptionsFile . File . FilePath ) ;
835+ }
836+ }
837+ }
838+ }
839+ else if ( questionBlock . BlockType == BlockType . WholeSlideImage && questionBlock . WholeSlideImageBlock != null )
840+ {
841+ var existingWholeSlideImages = questionBlock . WholeSlideImageBlock . WholeSlideImageBlockItems . ToList ( ) ;
842+ if ( existingWholeSlideImages . Any ( ) )
843+ {
844+ foreach ( var wsi in existingWholeSlideImages )
845+ {
846+ filePath . Add ( wsi . WholeSlideImage ? . File ? . FilePath ) ;
847+ }
848+ }
849+ }
850+ }
851+ }
852+
853+ var feedbackBlockCollection = block . QuestionBlock . FeedbackBlockCollection ;
854+ if ( feedbackBlockCollection != null && feedbackBlockCollection . Blocks != null )
855+ {
856+ foreach ( var feedbackBlock in feedbackBlockCollection . Blocks )
857+ {
858+ if ( feedbackBlock . BlockType == BlockType . Media && feedbackBlock . MediaBlock != null )
859+ {
860+ if ( feedbackBlock . MediaBlock . Image != null )
861+ {
862+ filePath . Add ( feedbackBlock . MediaBlock . Image . File . FilePath ) ;
863+ }
864+
865+ if ( feedbackBlock . MediaBlock . Video != null )
866+ {
867+ if ( feedbackBlock . MediaBlock . Video . File != null )
868+ {
869+ filePath . Add ( feedbackBlock . MediaBlock . Video . File . FilePath ) ;
870+ }
871+
872+ if ( feedbackBlock . MediaBlock . Video . VideoFile != null )
873+ {
874+ if ( feedbackBlock . MediaBlock . Video . VideoFile . TranscriptFile != null )
875+ {
876+ filePath . Add ( feedbackBlock . MediaBlock . Video . VideoFile . TranscriptFile . File . FilePath ) ;
877+ }
878+
879+ if ( feedbackBlock . MediaBlock . Video . VideoFile . CaptionsFile != null )
880+ {
881+ filePath . Add ( feedbackBlock . MediaBlock . Video . VideoFile . CaptionsFile . File . FilePath ) ;
882+ }
883+ }
884+ }
885+ }
886+ }
887+ }
888+ }
889+ }
890+
891+ return filePath ;
892+ }
727893 }
728894}
0 commit comments