diff --git a/games/serializers.py b/games/serializers.py index 5bd2fc6..0c19b0b 100644 --- a/games/serializers.py +++ b/games/serializers.py @@ -21,4 +21,5 @@ class GameSerializer(serializers.ModelSerializer): return "waiting" def get_players_id(self, instance: GameModel): - players_id = [player_game.member_id for player_game in GameMembersModel.objects.filter(game_id=instance.pk)] \ No newline at end of file + players_id = [player_game.player_id for player_game in GameMembersModel.objects.filter(game_id = instance.pk)] + return players_id \ No newline at end of file diff --git a/games/urls.py b/games/urls.py new file mode 100644 index 0000000..96d61a8 --- /dev/null +++ b/games/urls.py @@ -0,0 +1,9 @@ +from django.urls import path, re_path +from django.conf import settings +from django.conf.urls.static import static + +from .viewset import GameViewSet + +urlpatterns = [ + path("", GameViewSet.as_view({"get": "retrieve"}), name="game_page"), +] \ No newline at end of file diff --git a/games/views.py b/games/views.py deleted file mode 100644 index 91ea44a..0000000 --- a/games/views.py +++ /dev/null @@ -1,3 +0,0 @@ -from django.shortcuts import render - -# Create your views here. diff --git a/games/viewset.py b/games/viewset.py new file mode 100644 index 0000000..622cdf1 --- /dev/null +++ b/games/viewset.py @@ -0,0 +1,27 @@ +from rest_framework import viewsets +from rest_framework.response import Response +from rest_framework import permissions, status +from rest_framework.authentication import SessionAuthentication + +from django.http import HttpRequest +from django.db.models import QuerySet + +from .models import GameModel +from .serializers import GameSerializer + +# Create your views here. +class GameViewSet(viewsets.ModelViewSet): + + queryset = GameModel.objects + serializer_class = GameSerializer + permission_classes = (permissions.IsAuthenticated,) + authentication_classes = (SessionAuthentication,) + + def retrieve(self, request: HttpRequest, pk): + + if (not self.queryset.filter(pk = pk).exists()): + return Response({"detail": "Game not found."}, status=status.HTTP_404_NOT_FOUND) + + game = self.queryset.get(pk=pk) + + return Response(self.serializer_class(game).data, status=status.HTTP_200_OK) \ No newline at end of file diff --git a/transcendence/urls.py b/transcendence/urls.py index 38eb6d0..d0d06ad 100644 --- a/transcendence/urls.py +++ b/transcendence/urls.py @@ -23,5 +23,6 @@ urlpatterns = [ path('api/accounts/', include('accounts.urls')), path('api/chat/', include('chat.urls')), path('api/tournaments/', include('tournament.urls')), + path('api/games/', include('games.urls')), path('', include('frontend.urls')), ]