diff --git a/api/packages/crossword_repository/lib/src/crossword_repository.dart b/api/packages/crossword_repository/lib/src/crossword_repository.dart index 8ef59efa..39a1ef94 100644 --- a/api/packages/crossword_repository/lib/src/crossword_repository.dart +++ b/api/packages/crossword_repository/lib/src/crossword_repository.dart @@ -17,7 +17,7 @@ class CrosswordRepository { static const _sectionsCollection = 'boardChunks'; static const _answersCollection = 'answers'; - static const _boardInfoCollection = 'boardInfo'; + static const _solvedWordCollection = 'solvedWords'; /// Fetches all sections from the board. Future> listAllSections() async { @@ -203,27 +203,11 @@ class CrosswordRepository { oldString.substring(index + 1); } - /// Adds one to the solved words count in the crossword. - Future updateSolvedWordsCount() async { - final snapshot = await _dbClient.find( - _boardInfoCollection, - { - 'type': 'solved_words_count', - }, - ); - - final document = snapshot.first; - final solvedWordsCount = (document.data['value'] as num).toInt(); - final newValue = solvedWordsCount + 1; - - await _dbClient.update( - _boardInfoCollection, - DbEntityRecord( - id: document.id, - data: { - 'value': newValue, - }, - ), + /// Creates a new document for a solved word + Future saveSolvedWord(String wordId) async { + await _dbClient.set( + _solvedWordCollection, + DbEntityRecord(id: wordId), ); } } diff --git a/api/packages/crossword_repository/test/src/crossword_repository_test.dart b/api/packages/crossword_repository/test/src/crossword_repository_test.dart index 7fceefb1..71e4be77 100644 --- a/api/packages/crossword_repository/test/src/crossword_repository_test.dart +++ b/api/packages/crossword_repository/test/src/crossword_repository_test.dart @@ -518,7 +518,7 @@ void main() { ); }); - group('updateSolvedWordsCount', () { + group('saveSolvedWord', () { late CrosswordRepository repository; setUp(() { @@ -532,25 +532,18 @@ void main() { ).thenAnswer((_) async {}); }); - test('updates the document in the database', () async { - final record = _MockDbEntityRecord(); - when(() => record.id).thenReturn('id'); - when(() => record.data).thenReturn({'value': 80}); - when( - () => dbClient.find('boardInfo', {'type': 'solved_words_count'}), - ).thenAnswer((_) async => [record]); + test('creates a new document in the database', () async { when( - () => dbClient.update('boardInfo', any()), + () => dbClient.set('solvedWords', any()), ).thenAnswer((_) async {}); - await repository.updateSolvedWordsCount(); + await repository.saveSolvedWord('id'); verify( - () => dbClient.update( - 'boardInfo', + () => dbClient.set( + 'solvedWords', DbEntityRecord( id: 'id', - data: {'value': 81}, ), ), ).called(1); diff --git a/api/routes/game/answer.dart b/api/routes/game/answer.dart index 0154e5bc..cbd2eec9 100644 --- a/api/routes/game/answer.dart +++ b/api/routes/game/answer.dart @@ -48,7 +48,7 @@ Future _onPost(RequestContext context) async { points = await leaderboardRepository.updateScore(user.id); if (!preSolved) { - await crosswordRepository.updateSolvedWordsCount(); + await crosswordRepository.saveSolvedWord(wordId); } } diff --git a/api/test/routes/game/answer_test.dart b/api/test/routes/game/answer_test.dart index 3d096390..67ead548 100644 --- a/api/test/routes/game/answer_test.dart +++ b/api/test/routes/game/answer_test.dart @@ -137,7 +137,7 @@ void main() { final response = await route.onRequest(requestContext); expect(response.statusCode, HttpStatus.ok); - verifyNever(() => crosswordRepository.updateSolvedWordsCount()); + verifyNever(() => crosswordRepository.saveSolvedWord(any())); }, ); @@ -153,7 +153,7 @@ void main() { ), ); when( - () => crosswordRepository.updateSolvedWordsCount(), + () => crosswordRepository.saveSolvedWord('id'), ).thenAnswer((_) async {}); when( () => crosswordRepository.answerWord( @@ -176,7 +176,9 @@ void main() { final response = await route.onRequest(requestContext); expect(response.statusCode, HttpStatus.ok); - verify(() => crosswordRepository.updateSolvedWordsCount()).called(1); + verify( + () => crosswordRepository.saveSolvedWord('id'), + ).called(1); }, );