@@ -93,23 +93,44 @@ private async Task<Word> Add(string userId, Word word)
9393 return deletedWord . Id ;
9494 }
9595
96- /// <summary> Restores words to the Frontier </summary>
97- /// <returns> A bool: true if successful, false if any don't exist or are already in the Frontier. </returns>
96+ /// <summary> Restores words to the Frontier that aren't in the Frontier </summary>
97+ /// <remarks>
98+ /// Aborts if any word can't be restored for any of the following reasons:
99+ /// doesn't exist; has Status.Deleted; or is already in the Frontier
100+ /// </remarks>
101+ /// <returns> A bool: true if all successfully restored; false if none restored. </returns>
98102 public async Task < bool > RestoreFrontierWords ( string projectId , List < string > wordIds )
99103 {
100104 using var activity = OtelService . StartActivityWithTag ( otelTagName , "restoring words to Frontier" ) ;
101105
102- var words = new List < Word > ( ) ;
103- foreach ( var id in wordIds )
106+ // Allow calls that don't specify any wordIds, but don't do any work.
107+ if ( wordIds . Count == 0 )
104108 {
105- var word = await _wordRepo . GetWord ( projectId , id ) ;
106- if ( word is null || await _wordRepo . IsInFrontier ( projectId , id ) )
107- {
108- return false ;
109- }
110- words . Add ( word ) ;
109+ return true ;
111110 }
112- await _wordRepo . AddFrontier ( words ) ;
111+
112+ wordIds = wordIds . Distinct ( ) . ToList ( ) ;
113+
114+ // Make sure none of the words are in the Frontier.
115+ if ( await _wordRepo . AreInFrontier ( projectId , wordIds , 1 ) )
116+ {
117+ return false ;
118+ }
119+
120+ // Make sure all the words exist and are valid.
121+ var wordsToRestore = await _wordRepo . GetWords ( projectId , wordIds ) ;
122+ if ( wordsToRestore . Count != wordIds . Count )
123+ {
124+ return false ;
125+ }
126+ if ( wordsToRestore . Any ( w => w . Accessibility == Status . Deleted ) )
127+ {
128+ // We should be restoring words that were removed from the Frontier,
129+ // and not their "Deleted" copies in the words collection.
130+ return false ;
131+ }
132+
133+ await _wordRepo . AddFrontier ( wordsToRestore ) ;
113134 return true ;
114135 }
115136
0 commit comments