From 7eb09e7124d8db45b735adb00eb6f36e2cb02b91 Mon Sep 17 00:00:00 2001 From: Hugo Walbecq Date: Fri, 28 Jun 2024 16:29:09 +0200 Subject: [PATCH] refactor: solved word count (#594) * refactor: updateSolvedWordsCount method to add document in solvedWord collection * refactor: getSolvedWordsCount to return solvedWord collection size * chore: unused collection * chore: update doc * fix: address PR comments * refactor: keep same count reading logic * refactor: remove unused * refactor: rename method --------- Co-authored-by: Jaime Sanchez Co-authored-by: Jaime <52668514+jsgalarraga@users.noreply.github.com> --- .../lib/src/crossword_repository.dart | 28 ++++--------------- .../test/src/crossword_repository_test.dart | 19 ++++--------- api/routes/game/answer.dart | 2 +- api/test/routes/game/answer_test.dart | 8 ++++-- 4 files changed, 18 insertions(+), 39 deletions(-) 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); }, );