Skip to content

Commit

Permalink
Update Word.Modified timestamp on merge or delete (#994)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnthagen authored Feb 5, 2021
1 parent 5f4387a commit 19ad181
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
21 changes: 14 additions & 7 deletions Backend/Services/WordApiServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public async Task<bool> Delete(string projectId, string wordId)
{
var wordIsInFrontier = await _repo.DeleteFrontier(projectId, wordId);

// We only want to add the deleted word if the word started in the frontier
// We only want to add the deleted word if the word started in the frontier.
if (wordIsInFrontier)
{
var wordToDelete = await _repo.GetWord(projectId, wordId);
Expand All @@ -34,6 +34,7 @@ public async Task<bool> Delete(string projectId, string wordId)
}

wordToDelete.Id = "";
wordToDelete.Modified = "";
wordToDelete.History = new List<string> { wordId };
wordToDelete.Accessibility = State.Deleted;

Expand Down Expand Up @@ -65,6 +66,7 @@ public async Task<bool> Delete(string projectId, string wordId)
{
wordWithAudioToDelete.Audio.Remove(fileName);
wordWithAudioToDelete.Id = "";
wordWithAudioToDelete.Modified = "";
wordWithAudioToDelete.ProjectId = projectId;

// Keep track of the old word, adding it to the history.
Expand Down Expand Up @@ -93,6 +95,7 @@ public async Task<bool> Delete(string projectId, string wordId)
}

word.Id = "";
word.Modified = "";
word.ProjectId = projectId;
word.Accessibility = State.Deleted;

Expand All @@ -114,7 +117,7 @@ public async Task<bool> Update(string projectId, string wordId, Word word)
{
word.Id = "";
word.ProjectId = projectId;
word.Modified = Time.UtcNowIso8601();
word.Modified = "";

// Keep track of the old word, adding it to the history.
word.History.Add(wordId);
Expand All @@ -138,7 +141,7 @@ public async Task<List<Word>> Merge(string projectId, MergeWords mergeWords)
// Generate new child words from ChildrenWords.
foreach (var newChildWordState in mergeWords.ChildrenWords)
{
// Get child word
// Get child word.
var currentChildWord = await _repo.GetWord(projectId, newChildWordState.SrcWordId);
if (currentChildWord is null)
{
Expand All @@ -154,7 +157,7 @@ public async Task<List<Word>> Merge(string projectId, MergeWords mergeWords)
// Remove child from frontier.
await _repo.DeleteFrontier(projectId, currentChildWord.Id);

// Iterate through senses of that word and change to corresponding state in mergewords.
// Iterate through senses of that word and change to corresponding state in merged words.
if (currentChildWord.Senses.Count != newChildWordState.SenseStates.Count)
{
throw new FormatException("Sense counts don't match");
Expand All @@ -169,25 +172,27 @@ public async Task<List<Word>> Merge(string projectId, MergeWords mergeWords)

// Add child word to the database
currentChildWord.Id = "";
// Erase time old Modified date timestamp so the are recalculated.
currentChildWord.Modified = "";
await _repo.Add(currentChildWord);

// Handle different states
// Handle different states.
var separateWord = currentChildWord.Clone();
separateWord.Senses = new List<Sense>();
separateWord.Id = "";
for (var i = 0; i < currentChildWord.Senses.Count; i++)
{
switch (newChildWordState.SenseStates[i])
{
// Add the word to the parent's history
// Add the word to the parent's history.
case State.Sense:
case State.Duplicate:
if (!addParent.History.Contains(currentChildWord.Id))
{
addParent.History.Add(currentChildWord.Id);
}
break;
// Add the sense to a separate word and the word to its history
// Add the sense to a separate word and the word to its history.
case State.Separate:
currentChildWord.Senses[i].Accessibility = State.Active;
separateWord.Senses.Add(currentChildWord.Senses[i]);
Expand All @@ -205,13 +210,15 @@ public async Task<List<Word>> Merge(string projectId, MergeWords mergeWords)
if (separateWord.Senses.Count != 0)
{
separateWord.ProjectId = projectId;
separateWord.Modified = "";
var newSeparate = await _repo.Create(separateWord);
newWordsList.Add(newSeparate);
}
}

// Add parent with child history to the database
addParent.ProjectId = projectId;
addParent.Modified = "";
var newParent = await _repo.Create(addParent);
newWordsList.Insert(0, newParent);
return newWordsList;
Expand Down
27 changes: 20 additions & 7 deletions Backend/Services/WordRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,11 @@ public async Task<bool> DeleteAllWords(string projectId)
return deleted.DeletedCount != 0;
}

/// <summary> Adds a <see cref="Word"/> to the WordsCollection and Frontier </summary>
/// <returns> The word created </returns>
public async Task<Word> Create(Word word)
/// <summary>
/// If the <see cref="Word"/> Created or Modified times are blank, fill them in the current time.
/// </summary>
private static void PopulateBlankWordTimes(Word word)
{
PopulateWordGuids(word);

// Only update date time stamps if they are blank to allow services such as LiftApiService to set before
// creation.
if (word.Created.Length == 0)
{
// Use Roundtrip-suitable ISO 8601 format.
Expand All @@ -72,7 +69,18 @@ public async Task<Word> Create(Word word)
{
word.Modified = Time.UtcNowIso8601();
}
}

/// <summary> Adds a <see cref="Word"/> to the WordsCollection and Frontier </summary>
/// <remarks>
/// 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>
/// <returns> The word created </returns>
public async Task<Word> Create(Word word)
{
PopulateWordGuids(word);
PopulateBlankWordTimes(word);
await _wordDatabase.Words.InsertOneAsync(word);
await AddFrontier(word);
return word;
Expand All @@ -96,9 +104,14 @@ internal static void PopulateWordGuids(Word word)
}

/// <summary> Adds a <see cref="Word"/> only to the WordsCollection </summary>
/// <remarks>
/// 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>
/// <returns> The word created </returns>
public async Task<Word> Add(Word word)
{
PopulateBlankWordTimes(word);
await _wordDatabase.Words.InsertOneAsync(word);
return word;
}
Expand Down

0 comments on commit 19ad181

Please sign in to comment.