Skip to content

Commit

Permalink
[NewEntry] If duplicate, add note to existing (#1615)
Browse files Browse the repository at this point in the history
  • Loading branch information
imnasnainaec authored Mar 18, 2022
1 parent e29f156 commit 555605e
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 7 deletions.
54 changes: 54 additions & 0 deletions Backend.Tests/Models/WordTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,60 @@ public void TestIsBlank()
Assert.That(new Note { Language = Language }.IsBlank());
Assert.IsFalse(new Note { Text = Text }.IsBlank());
}

[Test]
public void TestAppendBlank()
{
var note = new Note(Language, Text);

var blankNote = new Note();
var newNote = note.Clone();
blankNote.Append(newNote);
Assert.That(blankNote.Equals(note));

blankNote = new Note();
var oldNote = note.Clone();
oldNote.Append(blankNote);
Assert.That(oldNote.Equals(note));
}

[Test]
public void TestAppendIdentical()
{
var note = new Note(Language, Text);
var oldNote = note.Clone();
var newNote = note.Clone();
oldNote.Append(newNote);
Assert.That(oldNote.Equals(note));
}

[Test]
public void TestAppendSameLanguage()
{
var note = new Note(Language, Text);
var oldNote = note.Clone();
var newNote = note.Clone();
const string newText = "sameLangNewText";
newNote.Text = newText;
oldNote.Append(newNote);
var expectedNote = note.Clone();
expectedNote.Text += $"; {newText}";
Assert.That(oldNote.Equals(expectedNote));
}

[Test]
public void TestAppendDiffLanguage()
{
var note = new Note(Language, Text);
var oldNote = note.Clone();
var newNote = note.Clone();
const string newLanguage = "diffLang";
newNote.Language = newLanguage;
oldNote.Append(newNote);
var expectedNote = note.Clone();
expectedNote.Text += $"; [{newLanguage}] {newNote.Text}";
Assert.That(oldNote.Equals(expectedNote));
}
}

public class SenseTests
Expand Down
23 changes: 23 additions & 0 deletions Backend.Tests/Services/WordServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,29 @@ public void WordIsUniqueFalseAddsDomain()
Assert.That(frontierSemDom, Is.Not.Null);
}

[Test]
public void WordIsUniqueFalseAddsNote()
{
var oldWord = Util.RandomWord(ProjId);
oldWord.Note.Text = "";
oldWord = _wordRepo.Create(oldWord).Result;

// Make the new word a duplicate of the old word, but with a note added.
var newWord = Util.RandomWord(ProjId);
newWord.Vernacular = oldWord.Vernacular;
newWord.Senses = new List<Sense> { oldWord.Senses.First().Clone() };
var newNote = new Note("lang", "text");
newWord.Note = newNote.Clone();

var isUnique = _wordService.WordIsUnique(newWord).Result;
Assert.That(isUnique, Is.False);

// The newNote should be added to the old word.
var frontier = _wordRepo.GetFrontier(ProjId).Result;
Assert.That(frontier, Has.Count.EqualTo(1));
Assert.That(frontier.First().Note, Is.EqualTo(newNote));
}

[Test]
public void WordIsUniqueSameVernNoDefNoGlossTrue()
{
Expand Down
20 changes: 20 additions & 0 deletions Backend/Models/Word.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,26 @@ public bool IsBlank()
return Text == "";
}

/// <summary> Append another note to the existing note. </summary>
public void Append(Note note)
{
if (note.IsBlank() || Equals(note))
{
return;
}

if (IsBlank())
{
Language = note.Language;
Text = note.Text;
}
else
{
var langTag = Language == note.Language ? "" : $"[{note.Language}] ";
Text += $"; {langTag}{note.Text}";
}
}

public override bool Equals(object? obj)
{
if (obj is not Note other || GetType() != obj.GetType())
Expand Down
16 changes: 9 additions & 7 deletions Backend/Services/WordService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ public async Task<bool> Update(string projectId, string wordId, Word word)
return wordIsInFrontier;
}

/// <summary> Checks if a word being added is an exact duplicate of a preexisting word </summary>
/// <summary>
/// Checks if a word being added is a duplicate of a preexisting word.
/// If a duplicate, updates the existing word with any new domains or note.
/// </summary>
public async Task<bool> WordIsUnique(Word word)
{
// Get all words from frontier
Expand Down Expand Up @@ -178,11 +181,6 @@ public async Task<bool> WordIsUnique(Word word)
{
foundDuplicateSense = true;

// Add edited by and remove duplicates
matchingVern.EditedBy.AddRange(word.EditedBy);
matchingVern.EditedBy = matchingVern.EditedBy.Distinct().ToList();

// Add semantic domains and remove duplicates
oldSense.SemanticDomains.AddRange(newSense.SemanticDomains);
oldSense.SemanticDomains = oldSense.SemanticDomains.Distinct().ToList();
}
Expand All @@ -195,10 +193,14 @@ public async Task<bool> WordIsUnique(Word word)
}
}

// Update the word only if all the senses were duplicates
// Update the existing word only if all the senses were duplicates
if (foundDuplicateSense)
{
isUniqueWord = false;

matchingVern.Note.Append(word.Note);
matchingVern.EditedBy.AddRange(word.EditedBy);
matchingVern.EditedBy = matchingVern.EditedBy.Distinct().ToList();
await Update(matchingVern.ProjectId, matchingVern.Id, matchingVern);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/components/DataEntry/DataEntryTable/DataEntryTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ export class DataEntryTable extends React.Component<
);
return;
}
// TODO: add the audio even if the word is a duplicate
const wordId = await this.addAudiosToBackend(addedWord.id, audioURLs);
const word = await backend.getWord(wordId);
await this.updateExisting();
Expand Down

0 comments on commit 555605e

Please sign in to comment.