From 234513e0f5cef5974759e4b8d992d668ed19e85c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=BChler=20Ma=C3=ABlys?= Date: Sat, 30 Mar 2024 12:40:57 +0100 Subject: [PATCH] add rank routes --- api/masteriq/urls.py | 1 + api/masteriqapp/tests/test_routes.py | 27 ++++++++++++++ api/masteriqapp/views/RankView.py | 54 ++++++++++++++++++++++++++++ api/masteriqapp/views/__init__.py | 3 +- 4 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 api/masteriqapp/views/RankView.py diff --git a/api/masteriq/urls.py b/api/masteriq/urls.py index 5a55a85..2a968fa 100644 --- a/api/masteriq/urls.py +++ b/api/masteriq/urls.py @@ -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), diff --git a/api/masteriqapp/tests/test_routes.py b/api/masteriqapp/tests/test_routes.py index c1bb5d1..59a2c0f 100644 --- a/api/masteriqapp/tests/test_routes.py +++ b/api/masteriqapp/tests/test_routes.py @@ -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 + + + + diff --git a/api/masteriqapp/views/RankView.py b/api/masteriqapp/views/RankView.py new file mode 100644 index 0000000..76b4764 --- /dev/null +++ b/api/masteriqapp/views/RankView.py @@ -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) diff --git a/api/masteriqapp/views/__init__.py b/api/masteriqapp/views/__init__.py index 4604a74..69ea03e 100644 --- a/api/masteriqapp/views/__init__.py +++ b/api/masteriqapp/views/__init__.py @@ -1,2 +1,3 @@ from .IQView import IQView -from .QuestionView import QuestionView \ No newline at end of file +from .QuestionView import QuestionView +from .RankView import RankView