added deck update and delete

This commit is contained in:
Alexander Karpov 2022-06-07 23:54:26 +03:00
parent ba04ce8347
commit 56dc11c7b3
4 changed files with 46 additions and 18 deletions

View File

@ -56,7 +56,7 @@ class CreatePlayerSerializer(serializers.ModelSerializer):
fields = ("ton_wallet", "name")
class DeckCreateSerializer(serializers.ModelSerializer):
class CreateDeckSerializer(serializers.ModelSerializer):
hero_ids = serializers.ListSerializer(
child=serializers.UUIDField(), min_length=16, max_length=16
)
@ -66,14 +66,17 @@ class DeckCreateSerializer(serializers.ModelSerializer):
fields = ("hero_ids",)
def validate_hero_ids(self, value):
for x in value:
if not (hero := Hero.objects.filter(uuid=x)):
raise ValidationError(f"Hero with uuid {x} doesn't exist")
if self.context["request"].method == "POST":
for x in value:
if not (hero := Hero.objects.filter(uuid=x)):
raise ValidationError(f"Hero with uuid {x} doesn't exist")
if deck := HeroInDeck.objects.filter(hero=hero.first()):
raise ValidationError(
f"Hero with uuid {x} is already in deck with id {deck.first().deck.id}"
)
if deck := HeroInDeck.objects.filter(hero=hero.first()):
raise ValidationError(
f"Hero with uuid {x} is already in deck with id {deck.first().deck.id}"
)
elif self.context["request"].method in ["PUT", "PATCH"]:
print(value)
return value
def create(self, validated_data):
@ -82,13 +85,22 @@ class DeckCreateSerializer(serializers.ModelSerializer):
HeroInDeck.objects.create(hero_id=x, deck=deck)
return deck
def update(self, instance, validated_data):
print(instance, validated_data)
return instance
class GetPlayerSerializer(serializers.ModelSerializer):
class Meta:
model = Player
fields = ("id", "name")
class GetDeckSerializer(serializers.ModelSerializer):
player = GetPlayerSerializer()
heroes = ListHeroSerializer(many=True)
class Meta:
model = Deck
fields = ("player_uuid", "heroes")
def get_heroes(self, val):
print(val)
fields = ("player", "heroes")

View File

@ -11,13 +11,13 @@ from rest_framework.mixins import (
from rest_framework.response import Response
from game.authentication import PlayerAuthentication
from game.models import Hero
from game.models import Hero, Deck
from game.api.v1.serializers import (
CreateHeroSerializer,
GetHeroSerializer,
CreatePlayerSerializer,
ListHeroSerializer,
DeckCreateSerializer,
CreateDeckSerializer,
GetDeckSerializer,
)
from game.services.jwt import sign_jwt
@ -85,7 +85,7 @@ class PlayerCreateView(GenericAPIView, CreateModelMixin):
class DeckCreateView(GenericAPIView, CreateModelMixin):
serializer_class = DeckCreateSerializer
serializer_class = CreateDeckSerializer
authentication_classes = (PlayerAuthentication,)
def perform_create(self, serializer):
@ -102,11 +102,23 @@ class DeckCreateView(GenericAPIView, CreateModelMixin):
class RetireUpdateDeleteDeckView(
RetrieveHeroView, DestroyModelMixin, UpdateModelMixin, GenericAPIView
):
lookup_field = "pk"
lookup_field = "id"
queryset = Deck.objects.all()
def get_serializer_class(self):
if self.request.method == "GET":
return GetDeckSerializer
else:
return CreateDeckSerializer
def get(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)
def put(self, request, *args, **kwargs):
return self.update(request, *args, **kwargs)
def patch(self, request, *args, **kwargs):
return self.partial_update(request, *args, **kwargs)
def delete(self, request, *args, **kwargs):
return self.destroy(request, *args, **kwargs)

View File

@ -116,11 +116,15 @@ class Deck(models.Model):
)
def __str__(self):
return f"{self.player.name} deck"
return f"{self.player.name}'s deck"
def get_heroes(self):
return [x.hero for x in HeroInDeck.objects.filter(deck=self)]
def heroes(self):
# added for better DRF view
return self.get_heroes()
class Meta:
db_table = "deck"
verbose_name = "deck"

View File

@ -14,6 +14,6 @@ urlpatterns = [
path("v1/player/", PlayerCreateView.as_view(), name="player_create_api"),
path("v1/deck/", DeckCreateView.as_view(), name="deck_create_api"),
path(
"v1/deck/<int:pk>", RetireUpdateDeleteDeckView.as_view(), name="deck_retire_api"
"v1/deck/<int:id>", RetireUpdateDeleteDeckView.as_view(), name="deck_retire_api"
),
]