Skip to content

Commit 21b27bf

Browse files
authored
Allow getting a word from the Frontier (#4148)
1 parent bbbdacf commit 21b27bf

13 files changed

Lines changed: 66 additions & 46 deletions

File tree

Backend.Tests/Controllers/AudioControllerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,13 @@ public void TestDeleteAudioFile()
182182
Assert.That(_wordRepo.GetAllWords(_projId).Result, Has.Count.EqualTo(2));
183183

184184
// Get the new word from the database
185-
var frontier = _wordRepo.GetFrontier(_projId).Result;
185+
var frontier = _wordRepo.GetAllFrontier(_projId).Result;
186186

187187
// Ensure the new word has no audio files
188188
Assert.That(frontier[0].Audio, Has.Count.EqualTo(0));
189189

190190
// Test the frontier
191-
Assert.That(_wordRepo.GetFrontier(_projId).Result, Has.Count.EqualTo(1));
191+
Assert.That(_wordRepo.GetAllFrontier(_projId).Result, Has.Count.EqualTo(1));
192192

193193
// Ensure the word with deleted audio is in the frontier
194194
Assert.That(frontier, Has.Count.EqualTo(1));

Backend.Tests/Controllers/WordControllerTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public async Task TestDeleteFrontierWord()
7171
w.Id == wordToDelete.Id ||
7272
w.Id == otherWord.Id ||
7373
w.Accessibility == Status.Deleted));
74-
var updatedFrontier = await _wordRepo.GetFrontier(ProjId);
74+
var updatedFrontier = await _wordRepo.GetAllFrontier(ProjId);
7575
Assert.That(updatedFrontier, Has.Count.EqualTo(1));
7676
Assert.That(updatedFrontier.First().Id, Is.EqualTo(otherWord.Id));
7777
}
@@ -248,7 +248,7 @@ public async Task TestRevertWords()
248248
});
249249
var reverted = (Dictionary<string, string>)((OkObjectResult)result).Value!;
250250
Assert.That(reverted, Has.Count.EqualTo(1));
251-
var frontierIds = (await _wordRepo.GetFrontier(ProjId)).Select(w => w.Id).ToList();
251+
var frontierIds = (await _wordRepo.GetAllFrontier(ProjId)).Select(w => w.Id).ToList();
252252
Assert.That(frontierIds, Has.Count.EqualTo(2));
253253
Assert.That(frontierIds, Does.Contain(frontierWord1.Id));
254254
Assert.That(frontierIds, Does.Contain(reverted[frontierWord0.Id]));
@@ -317,7 +317,7 @@ public async Task TestCreateWord()
317317
var allWords = await _wordRepo.GetAllWords(ProjId);
318318
Assert.That(allWords[0], Is.EqualTo(word).UsingPropertiesComparer());
319319

320-
var frontier = await _wordRepo.GetFrontier(ProjId);
320+
var frontier = await _wordRepo.GetAllFrontier(ProjId);
321321
Assert.That(frontier[0], Is.EqualTo(word).UsingPropertiesComparer());
322322
}
323323

@@ -350,7 +350,7 @@ public async Task TestUpdateWord()
350350
Assert.That(allWords, Does.Contain(origWord).UsingPropertiesComparer());
351351
Assert.That(allWords, Does.Contain(finalWord).UsingPropertiesComparer());
352352

353-
var frontier = await _wordRepo.GetFrontier(ProjId);
353+
var frontier = await _wordRepo.GetAllFrontier(ProjId);
354354
Assert.That(frontier, Has.Count.EqualTo(1));
355355
Assert.That(frontier, Does.Contain(finalWord).UsingPropertiesComparer());
356356
}
@@ -381,14 +381,14 @@ public async Task TestRestoreWord()
381381
await _wordRepo.DeleteFrontier(ProjId, word.Id);
382382

383383
Assert.That(await _wordRepo.GetAllWords(ProjId), Does.Contain(word).UsingPropertiesComparer());
384-
Assert.That(await _wordRepo.GetFrontier(ProjId), Is.Empty);
384+
Assert.That(await _wordRepo.GetAllFrontier(ProjId), Is.Empty);
385385

386386
var result = await _wordController.RestoreWord(ProjId, word.Id);
387387

388388
Assert.That(result, Is.InstanceOf<OkObjectResult>());
389389
Assert.That(((OkObjectResult)result).Value, Is.True);
390390
Assert.That(await _wordRepo.GetAllWords(ProjId), Does.Contain(word).UsingPropertiesComparer());
391-
Assert.That(await _wordRepo.GetFrontier(ProjId), Does.Contain(word).UsingPropertiesComparer());
391+
Assert.That(await _wordRepo.GetAllFrontier(ProjId), Does.Contain(word).UsingPropertiesComparer());
392392
}
393393

394394
[Test]

Backend.Tests/Mocks/WordRepositoryMock.cs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ internal sealed class WordRepositoryMock : IWordRepository
1313
private readonly List<Word> _words = [];
1414
private readonly List<Word> _frontier = [];
1515

16-
private Task<bool>? _getFrontierDelay;
17-
private int _getFrontierCallCount;
16+
private Task<bool>? _getAllFrontierDelay;
17+
private int _getAllFrontierCallCount;
1818

1919
/// <summary>
2020
/// Sets a delay for the GetFrontier method. The first call to GetFrontier will wait
2121
/// until the provided Task is completed.
2222
/// </summary>
2323
public void SetGetFrontierDelay(Task<bool> delay)
2424
{
25-
_getFrontierDelay = delay;
26-
_getFrontierCallCount = 0;
25+
_getAllFrontierDelay = delay;
26+
_getAllFrontierCallCount = 0;
2727
}
2828

2929
public Task<List<Word>> GetAllWords(string projectId)
@@ -99,21 +99,28 @@ public Task<int> GetFrontierCount(string projectId)
9999
return Task.FromResult(_frontier.Count(w => w.ProjectId == projectId));
100100
}
101101

102-
public async Task<List<Word>> GetFrontier(string projectId)
102+
public async Task<List<Word>> GetAllFrontier(string projectId)
103103
{
104-
if (_getFrontierDelay is not null)
104+
if (_getAllFrontierDelay is not null)
105105
{
106-
var callCount = Interlocked.Increment(ref _getFrontierCallCount);
106+
var callCount = Interlocked.Increment(ref _getAllFrontierCallCount);
107107
if (callCount == 1)
108108
{
109109
// First call waits for the signal
110-
await _getFrontierDelay;
110+
await _getAllFrontierDelay;
111111
}
112112
}
113113

114114
return _frontier.Where(w => w.ProjectId == projectId).Select(w => w.Clone()).ToList();
115115
}
116116

117+
public Task<Word?> GetFrontier(string projectId, string wordId, string? audioFileName = null)
118+
{
119+
var word = _frontier.Find(w => w.ProjectId == projectId && w.Id == wordId &&
120+
(string.IsNullOrEmpty(audioFileName) || w.Audio.Any(a => a.FileName == audioFileName)));
121+
return Task.FromResult(word?.Clone());
122+
}
123+
117124
public Task<List<Word>> GetFrontierWithVernacular(string projectId, string vernacular)
118125
{
119126
return Task.FromResult(_frontier.Where(

Backend.Tests/Services/MergeServiceTests.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void MergeWordsOneChildTest()
5555
Util.AssertEqualWordContent(newWords.First(), thisWord, true);
5656

5757
// Check that the only word in the frontier is the new word
58-
var frontier = _wordRepo.GetFrontier(ProjId).Result;
58+
var frontier = _wordRepo.GetAllFrontier(ProjId).Result;
5959
Assert.That(frontier, Has.Count.EqualTo(1));
6060
Assert.That(frontier.First(), Is.EqualTo(newWords.First()).UsingPropertiesComparer());
6161

@@ -81,7 +81,7 @@ public void MergeWordsDeleteTest()
8181
var newWords = _mergeService.Merge(ProjId, UserId, [mergeObject]).Result;
8282
// There should be no word added and no words left in the frontier.
8383
Assert.That(newWords, Is.Empty);
84-
var frontier = _wordRepo.GetFrontier(ProjId).Result;
84+
var frontier = _wordRepo.GetAllFrontier(ProjId).Result;
8585
Assert.That(frontier, Is.Empty);
8686
}
8787

@@ -105,7 +105,7 @@ public void MergeWordsMultiChildTest()
105105
Assert.That(_wordRepo.GetWord(ProjId, id).Result, Is.Not.Null);
106106
mergeWords.Children.Add(new MergeSourceWord { SrcWordId = id });
107107
}
108-
Assert.That(_wordRepo.GetFrontier(ProjId).Result, Has.Count.EqualTo(numberOfChildren));
108+
Assert.That(_wordRepo.GetAllFrontier(ProjId).Result, Has.Count.EqualTo(numberOfChildren));
109109

110110
var mergeWordsList = new List<MergeWords> { mergeWords };
111111
var newWords = _mergeService.Merge(ProjId, UserId, mergeWordsList).Result;
@@ -120,7 +120,7 @@ public void MergeWordsMultiChildTest()
120120

121121
// Confirm that parent added to repo and children not in frontier.
122122
Assert.That(_wordRepo.GetWord(ProjId, dbParent.Id).Result, Is.Not.Null);
123-
Assert.That(_wordRepo.GetFrontier(ProjId).Result, Has.Count.EqualTo(1));
123+
Assert.That(_wordRepo.GetAllFrontier(ProjId).Result, Has.Count.EqualTo(1));
124124
}
125125

126126
[Test]
@@ -134,7 +134,7 @@ public void MergeWordsMultipleTest()
134134
Assert.That(newWords, Has.Count.EqualTo(wordCount));
135135
Assert.That(newWords.First().Id, Is.Not.EqualTo(newWords.Last().Id));
136136

137-
var frontier = _wordRepo.GetFrontier(ProjId).Result;
137+
var frontier = _wordRepo.GetAllFrontier(ProjId).Result;
138138
Assert.That(frontier, Has.Count.EqualTo(wordCount));
139139
Assert.That(frontier.First().Id, Is.Not.EqualTo(frontier.Last().Id));
140140
Assert.That(newWords, Does.Contain(frontier.First()).UsingPropertiesComparer());
@@ -165,7 +165,7 @@ public void UndoMergeOneChildTest()
165165
var undo = _mergeService.UndoMerge(ProjId, UserId, mergedWord).Result;
166166
Assert.That(undo, Is.True);
167167

168-
var frontierWords = _wordRepo.GetFrontier(ProjId).Result;
168+
var frontierWords = _wordRepo.GetAllFrontier(ProjId).Result;
169169
var frontierWordIds = frontierWords.Select(word => word.Id).ToList();
170170

171171
Assert.That(frontierWords, Has.Count.EqualTo(1));
@@ -185,20 +185,20 @@ public void UndoMergeMultiChildTest()
185185
Assert.That(_wordRepo.GetWord(ProjId, id).Result, Is.Not.Null);
186186
mergeWords.Children.Add(new MergeSourceWord { SrcWordId = id });
187187
}
188-
Assert.That(_wordRepo.GetFrontier(ProjId).Result, Has.Count.EqualTo(numberOfChildren));
188+
Assert.That(_wordRepo.GetAllFrontier(ProjId).Result, Has.Count.EqualTo(numberOfChildren));
189189

190190
var mergeWordsList = new List<MergeWords> { mergeWords };
191191
var newWords = _mergeService.Merge(ProjId, UserId, mergeWordsList).Result;
192192

193-
Assert.That(_wordRepo.GetFrontier(ProjId).Result, Has.Count.EqualTo(1));
193+
Assert.That(_wordRepo.GetAllFrontier(ProjId).Result, Has.Count.EqualTo(1));
194194

195195
var childIds = mergeWords.Children.Select(word => word.SrcWordId).ToList();
196196
var parentIds = new List<string> { newWords[0].Id };
197197
var mergedWord = new MergeUndoIds(parentIds, childIds);
198198
var undo = _mergeService.UndoMerge(ProjId, UserId, mergedWord).Result;
199199
Assert.That(undo, Is.True);
200200

201-
var frontierWords = _wordRepo.GetFrontier(ProjId).Result;
201+
var frontierWords = _wordRepo.GetAllFrontier(ProjId).Result;
202202
var frontierWordIds = frontierWords.Select(word => word.Id).ToList();
203203

204204
Assert.That(frontierWords, Has.Count.EqualTo(numberOfChildren));

Backend.Tests/Services/WordServiceTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public void TestCreateMultipleWords()
4343
{
4444
_ = _wordService.Create(UserId, [new() { ProjectId = ProjId }, new() { ProjectId = ProjId }]).Result;
4545
Assert.That(_wordRepo.GetAllWords(ProjId).Result, Has.Count.EqualTo(2));
46-
Assert.That(_wordRepo.GetFrontier(ProjId).Result, Has.Count.EqualTo(2));
46+
Assert.That(_wordRepo.GetAllFrontier(ProjId).Result, Has.Count.EqualTo(2));
4747
}
4848

4949
[Test]
@@ -93,7 +93,7 @@ public void TestUpdateReplacesFrontierWord()
9393
var oldId = word.Id;
9494
word.Vernacular = "NewVern";
9595
Assert.That(_wordService.Update(UserId, word).Result!.Guid, Is.EqualTo(word.Guid));
96-
var frontier = _wordRepo.GetFrontier(ProjId).Result;
96+
var frontier = _wordRepo.GetAllFrontier(ProjId).Result;
9797
Assert.That(frontier, Has.Count.EqualTo(1));
9898
var newWord = frontier.First();
9999
Assert.That(newWord.Id, Is.Not.EqualTo(oldId));
@@ -131,7 +131,7 @@ public void TestRestoreFrontierWordsFrontierWordFalse()
131131
{
132132
var wordNoFrontier = _wordRepo.Add(new Word { ProjectId = ProjId }).Result;
133133
var wordYesFrontier = _wordRepo.Create(new Word { ProjectId = ProjId }).Result;
134-
Assert.That(_wordRepo.GetFrontier(ProjId).Result, Has.Count.EqualTo(1));
134+
Assert.That(_wordRepo.GetAllFrontier(ProjId).Result, Has.Count.EqualTo(1));
135135
Assert.That(
136136
_wordService.RestoreFrontierWords(ProjId, [wordNoFrontier.Id, wordYesFrontier.Id]).Result, Is.False);
137137
}
@@ -141,9 +141,9 @@ public void TestRestoreFrontierWordsTrue()
141141
{
142142
var word1 = _wordRepo.Add(new Word { ProjectId = ProjId }).Result;
143143
var word2 = _wordRepo.Add(new Word { ProjectId = ProjId }).Result;
144-
Assert.That(_wordRepo.GetFrontier(ProjId).Result, Is.Empty);
144+
Assert.That(_wordRepo.GetAllFrontier(ProjId).Result, Is.Empty);
145145
Assert.That(_wordService.RestoreFrontierWords(ProjId, [word1.Id, word2.Id]).Result, Is.True);
146-
Assert.That(_wordRepo.GetFrontier(ProjId).Result, Has.Count.EqualTo(2));
146+
Assert.That(_wordRepo.GetAllFrontier(ProjId).Result, Has.Count.EqualTo(2));
147147
}
148148

149149
[Test]

Backend/Controllers/AudioController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public async Task<IActionResult> UploadAudioFile(
117117
return BadRequest("Empty File");
118118
}
119119

120-
var word = await _wordRepo.GetWord(projectId, wordId);
120+
var word = await _wordRepo.GetFrontier(projectId, wordId);
121121
if (word is null)
122122
{
123123
return NotFound($"wordId: {wordId}");

Backend/Controllers/WordController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public async Task<IActionResult> GetProjectFrontierWords(string projectId)
104104
return Forbid();
105105
}
106106

107-
return Ok(await _wordRepo.GetFrontier(projectId));
107+
return Ok(await _wordRepo.GetAllFrontier(projectId));
108108
}
109109

110110
/// <summary> Checks if Frontier has <see cref="Word"/> in specified <see cref="Project"/>. </summary>
@@ -187,7 +187,7 @@ public async Task<IActionResult> UpdateDuplicate(
187187
}
188188
word.ProjectId = projectId;
189189

190-
var duplicatedWord = await _wordRepo.GetWord(word.ProjectId, dupId);
190+
var duplicatedWord = await _wordRepo.GetFrontier(word.ProjectId, dupId);
191191
if (duplicatedWord is null)
192192
{
193193
return NotFound();

Backend/Interfaces/IWordRepository.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ public interface IWordRepository
1818
Task<bool> IsInFrontier(string projectId, string wordId);
1919
Task<bool> AreInFrontier(string projectId, List<string> wordIds, int count);
2020
Task<int> GetFrontierCount(string projectId);
21-
Task<List<Word>> GetFrontier(string projectId);
21+
Task<List<Word>> GetAllFrontier(string projectId);
22+
Task<Word?> GetFrontier(string projectId, string wordId, string? audioFileName = null);
2223
Task<List<Word>> GetFrontierWithVernacular(string projectId, string vernacular);
2324
Task<Word> AddFrontier(Word word);
2425
Task<List<Word>> AddFrontier(List<Word> words);

Backend/Repositories/WordRepository.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,25 @@ public async Task<int> GetFrontierCount(string projectId)
229229
}
230230

231231
/// <summary> Finds all <see cref="Word"/>s in the Frontier for specified <see cref="Project"/> </summary>
232-
public async Task<List<Word>> GetFrontier(string projectId)
232+
public async Task<List<Word>> GetAllFrontier(string projectId)
233233
{
234234
using var activity = OtelService.StartActivityWithTag(otelTagName, "getting all Frontier words");
235235

236236
return await _frontier.Find(GetAllProjectWordsFilter(projectId)).ToListAsync();
237237
}
238238

239+
/// <summary> Gets a specified <see cref="Word"/> from the Frontier </summary>
240+
/// <returns> The word, or null if not found. </returns>
241+
public async Task<Word?> GetFrontier(string projectId, string wordId, string? audioFileName = null)
242+
{
243+
using var activity = OtelService.StartActivityWithTag(otelTagName, "getting a word from Frontier");
244+
245+
return string.IsNullOrEmpty(audioFileName)
246+
? await _frontier.Find(GetProjectWordFilter(projectId, wordId)).FirstOrDefaultAsync()
247+
: await _frontier.Find(GetProjectWordWithAudioFilter(projectId, wordId, audioFileName))
248+
.FirstOrDefaultAsync();
249+
}
250+
239251
/// <summary> Finds all <see cref="Word"/>s in Frontier of specified project with specified vern </summary>
240252
public async Task<List<Word>> GetFrontierWithVernacular(string projectId, string vernacular)
241253
{

Backend/Services/LiftService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public async Task<string> LiftExport(string projectId, IProjectRepository projRe
282282

283283
// Get every word with all of its information.
284284
var allWords = await wordRepo.GetAllWords(projectId);
285-
var frontier = await wordRepo.GetFrontier(projectId);
285+
var frontier = await wordRepo.GetAllFrontier(projectId);
286286
var activeWords = frontier.Where(
287287
x => x.Senses.Any(s => s.Accessibility == Status.Active || s.Accessibility == Status.Protected)).ToList();
288288
var hasFlags = activeWords.Any(w => w.Flag.Active);

0 commit comments

Comments
 (0)