Skip to content

Commit

Permalink
Fix error of empty string passed as parameter
Browse files Browse the repository at this point in the history
Co-authored-by: Caio Felipe <[email protected]>
  • Loading branch information
GabrielCastelo-31 and caio-felipee committed Nov 15, 2023
1 parent 43f202e commit 3e12ad7
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions api/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@
from rest_framework import status

MAXIMUM_RETURNED_DISCIPLINES = 5
ERROR_MESSAGE = "no valid argument found for 'search', 'year' or 'period'"


class Search(APIView):
def get(self, request: Request, *args, **kwargs) -> Response:
name = request.GET.get('search', None)
year = request.GET.get('year', None)
period = request.GET.get('period', None)

Check warning on line 16 in api/api/views.py

View check run for this annotation

Codecov / codecov/patch

api/api/views.py#L14-L16

Added lines #L14 - L16 were not covered by tests
if name is None or year is None or period is None:

if name is None or len(name) == 0 or year is None or len(year) == 0 or period is None or len(period) == 0:
return Response(

Check warning on line 19 in api/api/views.py

View check run for this annotation

Codecov / codecov/patch

api/api/views.py#L18-L19

Added lines #L18 - L19 were not covered by tests
{
"errors": "no valid argument found for 'search', 'year' or 'period'"
"errors": ERROR_MESSAGE
}, status.HTTP_400_BAD_REQUEST)

disciplines = filter_disciplines_by_name(name=name) | filter_disciplines_by_code(code=name)
filtered_disciplines = filter_disciplines_by_year_and_period(year=year, period=period, disciplines=disciplines)
disciplines = filter_disciplines_by_name(

Check warning on line 24 in api/api/views.py

View check run for this annotation

Codecov / codecov/patch

api/api/views.py#L24

Added line #L24 was not covered by tests
name=name) | filter_disciplines_by_code(code=name)
filtered_disciplines = filter_disciplines_by_year_and_period(

Check warning on line 26 in api/api/views.py

View check run for this annotation

Codecov / codecov/patch

api/api/views.py#L26

Added line #L26 was not covered by tests
year=year, period=period, disciplines=disciplines)
data = DisciplineSerializer(filtered_disciplines, many=True).data

Check warning on line 28 in api/api/views.py

View check run for this annotation

Codecov / codecov/patch

api/api/views.py#L28

Added line #L28 was not covered by tests

return Response(data[:MAXIMUM_RETURNED_DISCIPLINES], status.HTTP_200_OK)

Check warning on line 30 in api/api/views.py

View check run for this annotation

Codecov / codecov/patch

api/api/views.py#L30

Added line #L30 was not covered by tests

0 comments on commit 3e12ad7

Please sign in to comment.