Skip to content

Commit

Permalink
feat: add get hints api
Browse files Browse the repository at this point in the history
  • Loading branch information
jsgalarraga committed Apr 20, 2024
1 parent f62da77 commit 6cf76f5
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 1 deletion.
31 changes: 31 additions & 0 deletions api/routes/game/hint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const _maxAllowedHints = 10;
Future<Response> onRequest(RequestContext context) async {
if (context.request.method == HttpMethod.post) {
return _onPost(context);
} else if (context.request.method == HttpMethod.get) {
return _onGet(context);
} else {
return Response(statusCode: HttpStatus.methodNotAllowed);
}
Expand Down Expand Up @@ -68,3 +70,32 @@ Future<Response> _onPost(RequestContext context) async {
);
}
}

Future<Response> _onGet(RequestContext context) async {
final hintRepository = context.read<HintRepository>();
final user = context.read<AuthenticatedUser>();

final wordId = context.request.uri.queryParameters['wordId'];

if (wordId == null) {
return Response(statusCode: HttpStatus.badRequest);
}

try {
final hints = await hintRepository.getPreviousHints(
userId: user.id,
wordId: wordId,
);

return Response.json(
body: {
'hints': hints.map((hint) => hint.toJson()).toList(),
},
);
} catch (e) {
return Response(
body: e.toString(),
statusCode: HttpStatus.internalServerError,
);
}
}
75 changes: 74 additions & 1 deletion api/test/routes/game/hint_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class _MockCrosswordRepository extends Mock implements CrosswordRepository {}

class _MockHintRepository extends Mock implements HintRepository {}

class _MockUri extends Mock implements Uri {}

void main() {
group('/game/hint', () {
late RequestContext requestContext;
Expand All @@ -45,7 +47,7 @@ void main() {
});

group('other http methods', () {
const allowedMethods = [HttpMethod.post];
const allowedMethods = [HttpMethod.post, HttpMethod.get];
final notAllowedMethods = HttpMethod.values.where(
(e) => !allowedMethods.contains(e),
);
Expand Down Expand Up @@ -241,5 +243,76 @@ void main() {
},
);
});

group('GET', () {
late Uri uri;

setUp(() {
uri = _MockUri();
when(() => request.method).thenReturn(HttpMethod.get);
when(() => request.uri).thenReturn(uri);
});

test(
'returns Response with a list of hints',
() async {
final hintList = [
Hint(question: 'question1', response: HintResponse.yes),
Hint(question: 'question2', response: HintResponse.notApplicable),
Hint(question: 'question3', response: HintResponse.no),
];
when(() => uri.queryParameters).thenReturn({'wordId': 'wordId'});
when(
() => hintRepository.getPreviousHints(
userId: 'userId',
wordId: 'wordId',
),
).thenAnswer((_) async => hintList);

final response = await route.onRequest(requestContext);

expect(response.statusCode, HttpStatus.ok);
expect(
await response.json(),
equals({
'hints': [
{'question': 'question1', 'response': 'yes'},
{'question': 'question2', 'response': 'notApplicable'},
{'question': 'question3', 'response': 'no'},
],
}),
);
},
);

test(
'returns internal server error response when getting hints fails',
() async {
when(() => uri.queryParameters).thenReturn({'wordId': 'wordId'});
when(
() => hintRepository.getPreviousHints(
userId: 'userId',
wordId: 'wordId',
),
).thenThrow(HintException('Oops', StackTrace.empty));

final response = await route.onRequest(requestContext);

expect(response.statusCode, HttpStatus.internalServerError);
expect(await response.body(), contains('Oops'));
},
);

test(
'returns bad request response when word id not provided',
() async {
when(() => uri.queryParameters).thenReturn({});

final response = await route.onRequest(requestContext);

expect(response.statusCode, HttpStatus.badRequest);
},
);
});
});
}

0 comments on commit 6cf76f5

Please sign in to comment.