Compare commits

...

2 Commits

Author SHA1 Message Date
bfc58e74a9 game: add: retrive view 2023-12-28 11:33:06 +01:00
4739afbaf0 block: fix: .exists() 2023-12-28 11:32:49 +01:00
6 changed files with 40 additions and 5 deletions

View File

@ -21,4 +21,5 @@ class GameSerializer(serializers.ModelSerializer):
return "waiting" return "waiting"
def get_players_id(self, instance: GameModel): 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

@ -12,7 +12,7 @@ class BlockView(APIView):
def get(self, request, pk): def get(self, request, pk):
block = BlockModel.objects.filter(pk=pk) block = BlockModel.objects.filter(pk=pk)
if (block): if (block.exists()):
return Response(serializers.serialize("json", block), status=status.HTTP_200_OK) return Response(serializers.serialize("json", block), status=status.HTTP_200_OK)
else: else:
return Response("Not Found", status=status.HTTP_404_NOT_FOUND) return Response("Not Found", status=status.HTTP_404_NOT_FOUND)

View File

@ -23,5 +23,6 @@ urlpatterns = [
path('api/accounts/', include('accounts.urls')), path('api/accounts/', include('accounts.urls')),
path('api/chat/', include('chat.urls')), path('api/chat/', include('chat.urls')),
path('api/tournaments/', include('tournament.urls')), path('api/tournaments/', include('tournament.urls')),
path('api/games/', include('games.urls')),
path('', include('frontend.urls')), path('', include('frontend.urls')),
] ]