Basic game creating API (#2)
* Adding the base of our API * Little file and lint adjustments * Adding the lint command to Makefile * Adding the Minesweeper logic for game creation * Adding some tests for the Minesweeper algorithm * Adding some tools command to Makefile like pre-commit and pip-tools * Adding test help text to Makefile * all new user is_staff=True, for development for now * Now we can get the data from specific game Adding game status Adding game status Fixing game models
This commit is contained in:
0
api/__init__.py
Normal file
0
api/__init__.py
Normal file
5
api/apps.py
Normal file
5
api/apps.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ApiConfig(AppConfig):
|
||||
name = "api"
|
||||
0
api/resources/__init__.py
Normal file
0
api/resources/__init__.py
Normal file
30
api/resources/game.py
Normal file
30
api/resources/game.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from rest_framework import status
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
|
||||
from game.models import Game
|
||||
from ..serializers import GameSerializer
|
||||
|
||||
|
||||
class GameResource(APIView):
|
||||
def post(self, request):
|
||||
""" Creates a new game """
|
||||
|
||||
serializer = GameSerializer(data=request.data)
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
class GameSingleResource(APIView):
|
||||
def get(self, request, game_id):
|
||||
""" Returns a game serialized or not found """
|
||||
|
||||
try:
|
||||
game = Game.objects.get(pk=game_id)
|
||||
except Game.DoesNotExist:
|
||||
return Response(status=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
serializer = GameSerializer(game)
|
||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||
8
api/resources/main.py
Normal file
8
api/resources/main.py
Normal file
@@ -0,0 +1,8 @@
|
||||
from rest_framework import status
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
|
||||
|
||||
class MainResource(APIView):
|
||||
def get(self, request):
|
||||
return Response({"message": "Welcome to the game!"}, status=status.HTTP_200_OK)
|
||||
4
api/serializers/__init__.py
Normal file
4
api/serializers/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from .game import GameSerializer
|
||||
|
||||
|
||||
__all__ = ["GameSerializer"]
|
||||
9
api/serializers/game.py
Normal file
9
api/serializers/game.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from game.models import Game
|
||||
|
||||
|
||||
class GameSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Game
|
||||
fields = "__all__"
|
||||
0
api/tests.py
Normal file
0
api/tests.py
Normal file
13
api/urls.py
Normal file
13
api/urls.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from django.urls import path
|
||||
|
||||
from .resources.main import MainResource
|
||||
from .resources.game import GameResource, GameSingleResource
|
||||
|
||||
|
||||
app_name = "api"
|
||||
|
||||
urlpatterns = [
|
||||
path("games/<game_id>", GameSingleResource.as_view(), name="games_single"),
|
||||
path("games", GameResource.as_view(), name="games"),
|
||||
path("", MainResource.as_view(), name="main"),
|
||||
]
|
||||
Reference in New Issue
Block a user