Skip to content

Commit

Permalink
Merge pull request #110 from unb-mds/task/api/yearperiod-endpoint
Browse files Browse the repository at this point in the history
task(year/period): cria rota para retornar ano e período disponíveis no backend
  • Loading branch information
mateusvrs authored Nov 27, 2023
2 parents 5fac118 + b4dac77 commit 3a603cc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
25 changes: 25 additions & 0 deletions api/api/tests/test_year_period_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from rest_framework.test import APITestCase
from utils import sessions as sns


class TestYearPeriodAPI(APITestCase):

def test_year_period(self):
"""
Testa se a API retorna o ano/periodo atual e o próximo ano/periodo
Testes:
- Status code (200 OK)
- Dados retornados (ano/periodo atual e próximo ano/periodo)
"""

response = self.client.get('/courses/year-period/')
year, period = sns.get_current_year_and_period()
next_year, next_period = sns.get_next_period()

expected_data = {
'year/period': [f'{year}/{period}', f'{next_year}/{next_period}'],
}

self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, expected_data)
5 changes: 3 additions & 2 deletions api/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
app_name = 'api'

urlpatterns = [
path('', views.Search.as_view(), name="search")
]
path('', views.Search.as_view(), name="search"),
path('year-period/', views.YearPeriod.as_view(), name="year-period")
]
17 changes: 15 additions & 2 deletions api/api/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from utils.db_handler import filter_disciplines_by_name, filter_disciplines_by_code, filter_disciplines_by_year_and_period
from rest_framework.decorators import APIView
from .serializers import DisciplineSerializer
from utils.sessions import get_current_year_and_period, get_next_period
from rest_framework.response import Response
from rest_framework.request import Request
from rest_framework import status
Expand All @@ -15,7 +16,7 @@ def treat_string(self, string: str | None) -> str | None:
string = string.strip()

return string

def get(self, request: Request, *args, **kwargs) -> Response:
name = self.treat_string(request.GET.get('search', None))
year = self.treat_string(request.GET.get('year', None))
Expand All @@ -38,9 +39,21 @@ def get(self, request: Request, *args, **kwargs) -> Response:

for term in name[1:]:
disciplines &= filter_disciplines_by_code(code=term)

filtered_disciplines = filter_disciplines_by_year_and_period(
year=year, period=period, disciplines=disciplines)
data = DisciplineSerializer(filtered_disciplines, many=True).data

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


class YearPeriod(APIView):
def get(self, request: Request, *args, **kwargs) -> Response:
year, period = get_current_year_and_period()
next_year, next_period = get_next_period()

data = {
'year/period': [f'{year}/{period}', f'{next_year}/{next_period}'],
}

return Response(data, status.HTTP_200_OK)

0 comments on commit 3a603cc

Please sign in to comment.