diff --git a/Backend/Services/WordApiServices.cs b/Backend/Services/WordApiServices.cs index d4607ed366..cab82f5b7b 100644 --- a/Backend/Services/WordApiServices.cs +++ b/Backend/Services/WordApiServices.cs @@ -24,7 +24,7 @@ public async Task 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); @@ -34,6 +34,7 @@ public async Task Delete(string projectId, string wordId) } wordToDelete.Id = ""; + wordToDelete.Modified = ""; wordToDelete.History = new List { wordId }; wordToDelete.Accessibility = State.Deleted; @@ -65,6 +66,7 @@ public async Task 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. @@ -93,6 +95,7 @@ public async Task Delete(string projectId, string wordId) } word.Id = ""; + word.Modified = ""; word.ProjectId = projectId; word.Accessibility = State.Deleted; @@ -114,7 +117,7 @@ public async Task 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); @@ -138,7 +141,7 @@ public async Task> 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) { @@ -154,7 +157,7 @@ public async Task> 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"); @@ -169,9 +172,11 @@ public async Task> 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(); separateWord.Id = ""; @@ -179,7 +184,7 @@ public async Task> Merge(string projectId, MergeWords mergeWords) { 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)) @@ -187,7 +192,7 @@ public async Task> Merge(string projectId, MergeWords mergeWords) 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]); @@ -205,6 +210,7 @@ public async Task> Merge(string projectId, MergeWords mergeWords) if (separateWord.Senses.Count != 0) { separateWord.ProjectId = projectId; + separateWord.Modified = ""; var newSeparate = await _repo.Create(separateWord); newWordsList.Add(newSeparate); } @@ -212,6 +218,7 @@ public async Task> Merge(string projectId, MergeWords mergeWords) // 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; diff --git a/Backend/Services/WordRepository.cs b/Backend/Services/WordRepository.cs index 861e05865c..14812cf3f6 100644 --- a/Backend/Services/WordRepository.cs +++ b/Backend/Services/WordRepository.cs @@ -55,14 +55,11 @@ public async Task DeleteAllWords(string projectId) return deleted.DeletedCount != 0; } - /// Adds a to the WordsCollection and Frontier - /// The word created - public async Task Create(Word word) + /// + /// If the Created or Modified times are blank, fill them in the current time. + /// + 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. @@ -72,7 +69,18 @@ public async Task Create(Word word) { word.Modified = Time.UtcNowIso8601(); } + } + /// Adds a to the WordsCollection and Frontier + /// + /// 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. + /// + /// The word created + public async Task Create(Word word) + { + PopulateWordGuids(word); + PopulateBlankWordTimes(word); await _wordDatabase.Words.InsertOneAsync(word); await AddFrontier(word); return word; @@ -96,9 +104,14 @@ internal static void PopulateWordGuids(Word word) } /// Adds a only to the WordsCollection + /// + /// 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. + /// /// The word created public async Task Add(Word word) { + PopulateBlankWordTimes(word); await _wordDatabase.Words.InsertOneAsync(word); return word; }