diff --git a/Backend/Repositories/WordRepository.cs b/Backend/Repositories/WordRepository.cs index 8a6a9f2a7b..18fcdf2803 100644 --- a/Backend/Repositories/WordRepository.cs +++ b/Backend/Repositories/WordRepository.cs @@ -20,21 +20,43 @@ public WordRepository(IWordContext collectionSettings) _wordDatabase = collectionSettings; } - /// Finds all s with specified projectId + /// + /// Creates a mongo filter for all words in a specified project (and optionally with specified vernacular). + /// Since a variant in FLEx can export as an entry without any senses, filters out 0-sense words. + /// + private static FilterDefinition GetAllProjectWordsFilter(string projectId, string? vernacular = null) + { + var filterDef = new FilterDefinitionBuilder(); + return (vernacular is null) + ? filterDef.And(filterDef.Eq(w => w.ProjectId, projectId), filterDef.SizeGt(w => w.Senses, 0)) + : filterDef.And(filterDef.Eq(w => w.ProjectId, projectId), filterDef.SizeGt(w => w.Senses, 0), + filterDef.Eq(w => w.Vernacular, vernacular)); + } + + /// Creates a mongo filter for words in a specified project with specified wordId. + private static FilterDefinition GetProjectWordFilter(string projectId, string wordId) + { + var filterDef = new FilterDefinitionBuilder(); + return filterDef.And(filterDef.Eq(w => w.ProjectId, projectId), filterDef.Eq(w => w.Id, wordId)); + } + + /// Creates a mongo filter for words in a specified project with specified wordIds. + private static FilterDefinition GetProjectWordsFilter(string projectId, List wordIds) + { + var filterDef = new FilterDefinitionBuilder(); + return filterDef.And(filterDef.Eq(w => w.ProjectId, projectId), filterDef.In(w => w.Id, wordIds)); + } + + /// Finds all s with specified projectId public async Task> GetAllWords(string projectId) { - return await _wordDatabase.Words.Find(w => w.ProjectId == projectId).ToListAsync(); + return await _wordDatabase.Words.Find(GetAllProjectWordsFilter(projectId)).ToListAsync(); } /// Finds with specified wordId and projectId public async Task GetWord(string projectId, string wordId) { - var filterDef = new FilterDefinitionBuilder(); - var filter = filterDef.And( - filterDef.Eq(x => x.ProjectId, projectId), - filterDef.Eq(x => x.Id, wordId)); - - var wordList = await _wordDatabase.Words.FindAsync(filter); + var wordList = await _wordDatabase.Words.FindAsync(GetProjectWordFilter(projectId, wordId)); try { return await wordList.FirstAsync(); @@ -127,29 +149,26 @@ public async Task Add(Word word) /// Checks if Frontier is nonempty for specified public async Task IsFrontierNonempty(string projectId) { - var word = await _wordDatabase.Frontier.Find(w => w.ProjectId == projectId).FirstOrDefaultAsync(); + var word = await _wordDatabase.Frontier.Find(GetAllProjectWordsFilter(projectId)).FirstOrDefaultAsync(); return word is not null; } /// Checks if specified word is in Frontier for specified public async Task IsInFrontier(string projectId, string wordId) { - var word = await _wordDatabase.Frontier - .Find(w => w.ProjectId == projectId && w.Id == wordId).FirstOrDefaultAsync(); - return word is not null; + return (await _wordDatabase.Frontier.CountDocumentsAsync(GetProjectWordFilter(projectId, wordId))) > 0; } /// Finds all s in the Frontier for specified public async Task> GetFrontier(string projectId) { - return await _wordDatabase.Frontier.Find(w => w.ProjectId == projectId).ToListAsync(); + return await _wordDatabase.Frontier.Find(GetAllProjectWordsFilter(projectId)).ToListAsync(); } /// Finds all s in Frontier of specified project with specified vern public async Task> GetFrontierWithVernacular(string projectId, string vernacular) { - return await _wordDatabase.Frontier.Find( - w => w.ProjectId == projectId && w.Vernacular == vernacular).ToListAsync(); + return await _wordDatabase.Frontier.Find(GetAllProjectWordsFilter(projectId, vernacular)).ToListAsync(); } /// Adds a only to the Frontier @@ -174,12 +193,7 @@ public async Task> AddFrontier(List words) /// A bool: success of operation public async Task DeleteFrontier(string projectId, string wordId) { - var filterDef = new FilterDefinitionBuilder(); - var filter = filterDef.And( - filterDef.Eq(x => x.ProjectId, projectId), - filterDef.Eq(x => x.Id, wordId)); - - var deleted = await _wordDatabase.Frontier.DeleteOneAsync(filter); + var deleted = await _wordDatabase.Frontier.DeleteOneAsync(GetProjectWordFilter(projectId, wordId)); return deleted.DeletedCount > 0; } @@ -187,29 +201,8 @@ public async Task DeleteFrontier(string projectId, string wordId) /// Number of words deleted public async Task DeleteFrontier(string projectId, List wordIds) { - var filterDef = new FilterDefinitionBuilder(); - var filter = filterDef.And( - filterDef.Eq(x => x.ProjectId, projectId), - filterDef.In(x => x.Id, wordIds)); - var deleted = await _wordDatabase.Frontier.DeleteManyAsync(filter); + var deleted = await _wordDatabase.Frontier.DeleteManyAsync(GetProjectWordsFilter(projectId, wordIds)); return deleted.DeletedCount; } - - /// Updates in the Frontier collection with same wordId and projectId - /// A bool: success of operation - public async Task UpdateFrontier(Word word) - { - var filterDef = new FilterDefinitionBuilder(); - var filter = filterDef.And( - filterDef.Eq(x => x.ProjectId, word.ProjectId), - filterDef.Eq(x => x.Id, word.Id)); - - var deleted = (await _wordDatabase.Frontier.DeleteOneAsync(filter)).DeletedCount > 0; - if (deleted) - { - await AddFrontier(word); - } - return deleted; - } } }