Skip to content

Commit

Permalink
refactor: solved word count (#594)
Browse files Browse the repository at this point in the history
* 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 <jaimesanchez8@gmail.com>
Co-authored-by: Jaime <52668514+jsgalarraga@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 28, 2024
1 parent dc38086 commit 7eb09e7
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<BoardSection>> listAllSections() async {
Expand Down Expand Up @@ -203,27 +203,11 @@ class CrosswordRepository {
oldString.substring(index + 1);
}

/// Adds one to the solved words count in the crossword.
Future<void> 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<void> saveSolvedWord(String wordId) async {
await _dbClient.set(
_solvedWordCollection,
DbEntityRecord(id: wordId),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ void main() {
);
});

group('updateSolvedWordsCount', () {
group('saveSolvedWord', () {
late CrosswordRepository repository;

setUp(() {
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion api/routes/game/answer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Future<Response> _onPost(RequestContext context) async {
points = await leaderboardRepository.updateScore(user.id);

if (!preSolved) {
await crosswordRepository.updateSolvedWordsCount();
await crosswordRepository.saveSolvedWord(wordId);
}
}

Expand Down
8 changes: 5 additions & 3 deletions api/test/routes/game/answer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void main() {
final response = await route.onRequest(requestContext);

expect(response.statusCode, HttpStatus.ok);
verifyNever(() => crosswordRepository.updateSolvedWordsCount());
verifyNever(() => crosswordRepository.saveSolvedWord(any()));
},
);

Expand All @@ -153,7 +153,7 @@ void main() {
),
);
when(
() => crosswordRepository.updateSolvedWordsCount(),
() => crosswordRepository.saveSolvedWord('id'),
).thenAnswer((_) async {});
when(
() => crosswordRepository.answerWord(
Expand All @@ -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);
},
);

Expand Down

0 comments on commit 7eb09e7

Please sign in to comment.