Skip to content
Merged
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
17 changes: 17 additions & 0 deletions Backend.Tests/Mocks/WordRepositoryMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,23 @@ public Task<Word> Add(Word word)
return Task.FromResult(word);
}

public Task<Word> CreateAndDeleteFrontier(Word newWord, string oldWordId)
{
newWord.Id = Guid.NewGuid().ToString();
_words.Add(newWord.Clone());
_frontier.Add(newWord.Clone());
_frontier.RemoveAll(w => w.ProjectId == newWord.ProjectId && w.Id == oldWordId);
return Task.FromResult(newWord.Clone());
}

public Task<Word> AddAndDeleteFrontier(Word deletedWord, string wordId)
{
deletedWord.Id = Guid.NewGuid().ToString();
_words.Add(deletedWord.Clone());
_frontier.RemoveAll(w => w.ProjectId == deletedWord.ProjectId && w.Id == wordId);
return Task.FromResult(deletedWord.Clone());
}

public Task<int> CountFrontierWordsWithDomain(string projectId, string domainId)
{
var count = _frontier.Count(
Expand Down
2 changes: 2 additions & 0 deletions Backend/Interfaces/IWordRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public interface IWordRepository
Task<Word> Create(Word word);
Task<List<Word>> Create(List<Word> words);
Task<Word> Add(Word word);
Task<Word> CreateAndDeleteFrontier(Word newWord, string oldWordId);
Task<Word> AddAndDeleteFrontier(Word deletedWord, string wordId);
Task<bool> DeleteAllWords(string projectId);
Task<bool> DeleteAllFrontierWords(string projectId);
Task<bool> HasWords(string projectId);
Expand Down
39 changes: 39 additions & 0 deletions Backend/Repositories/WordRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,45 @@ public async Task<Word> Add(Word word)
return word;
}

/// <summary>
/// Adds a new <see cref="Word"/> to WordsCollection and Frontier, and removes the old word from Frontier.
/// </summary>
/// <remarks>
/// If the Created or Modified time fields are blank, they will be automatically calculated using the current
/// time. This allows services to set or clear the values before creation to control these fields.
/// </remarks>
/// <returns> The new word created. </returns>
public async Task<Word> CreateAndDeleteFrontier(Word newWord, string oldWordId)
{
using var activity = OtelService.StartActivityWithTag(
otelTagName, "creating word in WordsCollection and Frontier, deleting old word from Frontier");

PopulateBlankWordTimes(newWord);
await _words.InsertOneAsync(newWord);
await _frontier.InsertOneAsync(newWord);
await _frontier.FindOneAndDeleteAsync(GetProjectWordFilter(newWord.ProjectId, oldWordId));
return newWord;
}

/// <summary>
/// Adds a <see cref="Word"/> only to the WordsCollection and removes a word from the Frontier.
/// </summary>
/// <remarks>
/// If the Created or Modified time fields are blank, they will be automatically calculated using the current
/// time. This allows services to set or clear the values before creation to control these fields.
/// </remarks>
/// <returns> The word added. </returns>
public async Task<Word> AddAndDeleteFrontier(Word deletedWord, string wordId)
{
using var activity = OtelService.StartActivityWithTag(
otelTagName, "adding word to WordsCollection, deleting word from Frontier");

PopulateBlankWordTimes(deletedWord);
await _words.InsertOneAsync(deletedWord);
await _frontier.FindOneAndDeleteAsync(GetProjectWordFilter(deletedWord.ProjectId, wordId));
return deletedWord;
}

/// <summary> Checks if Words collection for specified <see cref="Project"/> has any words. </summary>
public async Task<bool> HasWords(string projectId)
{
Expand Down
19 changes: 2 additions & 17 deletions Backend/Services/WordService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,6 @@ public async Task<List<Word>> Create(string userId, List<Word> words)
return await _wordRepo.Create(words.Select(w => PrepEditedData(userId, w)).ToList());
}

/// <summary> Adds a new word with updated edited data. </summary>
/// <returns> The added word </returns>
private async Task<Word> Add(string userId, Word word)
{
return await _wordRepo.Add(PrepEditedData(userId, word));
}

/// <summary> Removes audio with specified fileName from a Frontier word </summary>
/// <returns> Updated word, or null if not found </returns>
public async Task<Word?> DeleteAudio(string projectId, string userId, string wordId, string fileName)
Expand Down Expand Up @@ -85,10 +78,7 @@ private async Task<Word> Add(string userId, Word word)
word.Accessibility = Status.Deleted;
word.History.Add(wordId);

var deletedWord = await Add(userId, word);

// Don't remove the Frontier word until the copy is successfully stored as deleted.
await _wordRepo.DeleteFrontier(projectId, wordId);
var deletedWord = await _wordRepo.AddAndDeleteFrontier(PrepEditedData(userId, word), wordId);

return deletedWord.Id;
}
Expand Down Expand Up @@ -156,12 +146,7 @@ public async Task<bool> RestoreFrontierWords(string projectId, List<string> word
// only keep UsingCitationForm true if the Vernacular hasn't changed.
word.UsingCitationForm &= word.Vernacular == oldWord.Vernacular;

var newWord = await Create(userId, word);

// Don't remove the old Frontier word until the new word is successfully created.
await _wordRepo.DeleteFrontier(word.ProjectId, oldWordId);

return newWord;
return await _wordRepo.CreateAndDeleteFrontier(PrepEditedData(userId, word), oldWordId);
}

/// <summary> Checks if a word being added is a duplicate of a preexisting word. </summary>
Expand Down