Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Backend.Tests/Mocks/WordRepositoryMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,20 @@ public Task<List<Word>> GetAllWords(string projectId)
{
try
{
var foundWord = _words.Single(word => word.Id == wordId);
return Task.FromResult<Word?>(foundWord.Clone());
return Task.FromResult<Word?>(_words.Single(w => w.ProjectId == projectId && w.Id == wordId).Clone());
}
catch (InvalidOperationException)
{
return Task.FromResult<Word?>(null);
}
}

public Task<List<Word>> GetWords(string projectId, List<string> wordIds)
{
return Task.FromResult(
_words.Where(w => w.ProjectId == projectId && wordIds.Contains(w.Id)).Select(w => w.Clone()).ToList());
}

public Task<Word> Create(Word word)
{
word.Id = Guid.NewGuid().ToString();
Expand Down
1 change: 1 addition & 0 deletions Backend/Interfaces/IWordRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public interface IWordRepository
{
Task<List<Word>> GetAllWords(string projectId);
Task<Word?> GetWord(string projectId, string wordId);
Task<List<Word>> GetWords(string projectId, List<string> wordIds);
Task<Word> Create(Word word);
Task<List<Word>> Create(List<Word> words);
Task<Word> Add(Word word);
Expand Down
15 changes: 11 additions & 4 deletions Backend/Repositories/WordRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,17 @@ public async Task<List<Word>> GetAllWords(string projectId)
}
}

/// <summary> Removes all <see cref="Word"/>s from the WordsCollection and Frontier for specified
/// <see cref="Project"/> </summary>
/// <summary> Finds project <see cref="Word"/>s with specified ids </summary>
public async Task<List<Word>> GetWords(string projectId, List<string> wordIds)
{
using var activity = OtelService.StartActivityWithTag(otelTagName, "getting words");

return await _words.Find(GetProjectWordsFilter(projectId, wordIds)).ToListAsync();
}

/// <summary>
/// Removes all <see cref="Word"/>s from the WordsCollection and Frontier for specified <see cref="Project"/>
/// </summary>
/// <returns> A bool: success of operation </returns>
public async Task<bool> DeleteAllWords(string projectId)
{
Expand Down Expand Up @@ -130,7 +139,6 @@ private static void PopulateBlankWordTimes(Word word)
/// If the Created or Modified time fields are blank, they will automatically calculated using the current
/// time. This allows services to set or clear the values before creation to control these fields.
/// </remarks>
/// <param name="word"></param>
/// <returns> The word created </returns>
public async Task<Word> Create(Word word)
{
Expand All @@ -148,7 +156,6 @@ public async Task<Word> Create(Word word)
/// If the Created or Modified time fields are blank, they will automatically calculated using the current
/// time. This allows services to set or clear the values before creation to control these fields.
/// </remarks>
/// <param name="words"></param>
/// <returns> The words created </returns>
public async Task<List<Word>> Create(List<Word> words)
{
Expand Down
37 changes: 27 additions & 10 deletions Backend/Services/WordService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,22 +92,39 @@ private async Task<Word> Add(string userId, Word word)
return (await Add(userId, word)).Id;
}

/// <summary> Restores words to the Frontier </summary>
/// <returns> A bool: true if successful, false if any don't exist or are already in the Frontier. </returns>
/// <summary> Restores words to the Frontier that aren't in the Frontier </summary>
/// <returns> A bool: true if all successfully restored </returns>
public async Task<bool> RestoreFrontierWords(string projectId, List<string> wordIds)
{
using var activity = OtelService.StartActivityWithTag(otelTagName, "restoring words to Frontier");

var words = new List<Word>();
foreach (var id in wordIds)
// Allow calls that don't specify any wordIds, but don't do any work.
if (wordIds.Count == 0)
{
var word = await _wordRepo.GetWord(projectId, id);
if (word is null || await _wordRepo.IsInFrontier(projectId, id))
{
return false;
}
words.Add(word);
return true;
}

wordIds = wordIds.Distinct().ToList();

// Make sure none of the words are in the Frontier.
if (await _wordRepo.AreInFrontier(projectId, wordIds, 1))
{
return false;
}

// Make sure all the words exist and are valid.
var words = await _wordRepo.GetWords(projectId, wordIds);
if (words.Count != wordIds.Count)
{
return false;
}
if (words.Any(w => w.Accessibility == Status.Deleted))
{
// We should be restoring words that were removed from the Frontier,
// and not their "Deleted" copies in the words collection.
return false;
}

await _wordRepo.AddFrontier(words);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
return true;
}
Expand Down
Loading