game: add: retrive view

This commit is contained in:
starnakin 2023-12-28 11:33:06 +01:00
parent 4739afbaf0
commit bfc58e74a9
5 changed files with 39 additions and 4 deletions

View File

@ -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)]
players_id = [player_game.player_id for player_game in GameMembersModel.objects.filter(game_id = instance.pk)]
return players_id

9
games/urls.py Normal file
View File

@ -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("<int:pk>", GameViewSet.as_view({"get": "retrieve"}), name="game_page"),
]

View File

@ -1,3 +0,0 @@
from django.shortcuts import render
# Create your views here.

27
games/viewset.py Normal file
View File

@ -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)

View File

@ -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')),
]