44| Project Topics Library
55\=============================================================================================================================*/
66using System ;
7+ using System . Collections . Generic ;
78using System . IO ;
89using System . Linq ;
910using System . Text ;
2324using OnTopic . Internal . Diagnostics ;
2425using OnTopic . Mapping ;
2526using OnTopic . Metadata ;
27+ using OnTopic . Querying ;
2628using OnTopic . Repositories ;
2729
2830namespace OnTopic . Editor . AspNetCore . Controllers {
@@ -694,6 +696,11 @@ public async Task<IActionResult> Import(IFormFile jsonFile, [Bind(Prefix = "Impo
694696 return View ( editorViewModel ) ;
695697 }
696698
699+ /*------------------------------------------------------------------------------------------------------------------------
700+ | INDEX TOPICS IN SCOPE
701+ \-----------------------------------------------------------------------------------------------------------------------*/
702+ var topics = target . FindAll ( t => t . Id >= 0 ) . ToList ( ) ;
703+
697704 /*------------------------------------------------------------------------------------------------------------------------
698705 | IMPORT INTO TOPIC GRAPH
699706 >-------------------------------------------------------------------------------------------------------------------------
@@ -704,6 +711,20 @@ public async Task<IActionResult> Import(IFormFile jsonFile, [Bind(Prefix = "Impo
704711 target . Import ( topicData , options ) ;
705712 target . Import ( topicData , options ) ;
706713
714+ /*------------------------------------------------------------------------------------------------------------------------
715+ | DELETE UNMATCHED TOPICS
716+ >-------------------------------------------------------------------------------------------------------------------------
717+ | ### HACK JJC20200327: The Data Transfer library doesnt have access to the ITopicRepository, so it can't delete topics.
718+ | Instead, it removes them from the topic graph. But the ITopicRepository implementations don't have a means of detecting
719+ | removed topics during a recursive save and, therefore, the deletions aren't persited to the database. To mitigate this,
720+ | we evaluate the topic graph after the save, and then delete any orphans.
721+ \-----------------------------------------------------------------------------------------------------------------------*/
722+ var unmatchedTopics = topics . Except ( target . FindAll ( t => t . Id >= 0 ) ) ;
723+
724+ foreach ( var unmatchedTopic in unmatchedTopics ) {
725+ TopicRepository . Delete ( unmatchedTopic ) ;
726+ }
727+
707728 /*------------------------------------------------------------------------------------------------------------------------
708729 | SAVE
709730 >-------------------------------------------------------------------------------------------------------------------------
0 commit comments