Skip to content

Commit

Permalink
Merge pull request #51 from HE-Arc/mbu-40-routes-leaderboard
Browse files Browse the repository at this point in the history
add rank routes
  • Loading branch information
maelys-buhler authored Mar 30, 2024
2 parents cfc5a50 + 234513e commit 1ea2fec
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions api/masteriq/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

router.register("category", views.IQView, basename="category")
router.register("question", views.QuestionView, basename="question")
router.register("rank", views.RankView, basename="rank")

urlpatterns = [
path('admin/', admin.site.urls),
Expand Down
27 changes: 27 additions & 0 deletions api/masteriqapp/tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,30 @@ def test_route(self):
assert response.json()['options'] is not None
assert len(response.json()['options']) >= 2

response = c.get("/api/rank/1/leaderboard/")
assert response.status_code == 200
assert len(response.json()) > 0
assert response.json()[0]['user_id'] is not None
assert response.json()[0]['user_name'] is not None
assert response.json()[0]['user_iq'] is not None

response = c.get("/api/rank/global_leaderboard/")
assert response.status_code == 200
assert len(response.json()) > 0
assert response.json()[0]['user_id'] is not None
assert response.json()[0]['user_name'] is not None
assert response.json()[0]['user_iq'] is not None

response = c.get("/api/rank/1/user/")
assert response.status_code == 200
assert response.json()['user_rank'] is not None
assert response.json()['user_iq'] is not None

response = c.get("/api/rank/global_user/")
assert response.status_code == 200
assert response.json()['user_rank'] is not None
assert response.json()['user_iq'] is not None




54 changes: 54 additions & 0 deletions api/masteriqapp/views/RankView.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import random

from django.apps import apps
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework import status

masteriq = apps.get_app_config("masteriqapp")


class RankView(viewsets.ViewSet):
category_model = masteriq.get_model("Category")
question_model = masteriq.get_model("Question")
queryset = category_model.objects.all()

@action(detail=True, methods=["GET"])
def leaderboard(self, request, pk):
# TODO: get data from database when users are implemented
data_to_send = []
for i in range(1, 11):
data_to_send.append({
"user_id": random.randint(1, 1000),
"user_name": f"player_number_{i}",
"user_iq": 150 - (i * 10)
})

return Response(data=data_to_send, status=status.HTTP_200_OK)

@action(detail=False, methods=["GET"])
def global_leaderboard(self, request):
# TODO: get data from database when users are implemented
data_to_send = []
for i in range(1, 11):
data_to_send.append({
"user_id": random.randint(1, 1000),
"user_name": f"player_number_{i}",
"user_iq": 50 + (i * 10)
})

return Response(data=data_to_send, status=status.HTTP_200_OK)

@action(detail=True, methods=["GET"])
def user(self, request, pk):
# TODO: get data from database when users are implemented

data_to_send = {"user_rank": random.randint(1, 1000), "user_iq": random.randint(1, 200)}
return Response(data=data_to_send, status=status.HTTP_200_OK)

@action(detail=False, methods=["GET"])
def global_user(self, request):
# TODO: get data from database when users are implemented
data_to_send = {"user_rank": random.randint(1, 1000), "user_iq": random.randint(1, 200)}
return Response(data=data_to_send, status=status.HTTP_200_OK)
3 changes: 2 additions & 1 deletion api/masteriqapp/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .IQView import IQView
from .QuestionView import QuestionView
from .QuestionView import QuestionView
from .RankView import RankView

0 comments on commit 1ea2fec

Please sign in to comment.