Skip to content

Commit

Permalink
Solution with new suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
obohatov committed Oct 18, 2023
1 parent c366fd4 commit e3653c4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 101 deletions.
44 changes: 8 additions & 36 deletions cinema/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,10 @@ class MovieSerializer(serializers.Serializer):
description = serializers.CharField()
duration = serializers.IntegerField()

def create(
self,
validated_data
):
def create(self, validated_data):
return Movie.objects.create(**validated_data)

def update(
self,
instance,
validated_data
):
def update(self, instance, validated_data):
instance.title = validated_data.get(
"title",
instance.title
Expand All @@ -43,17 +36,10 @@ class ActorSerializer(serializers.Serializer):
first_name = serializers.CharField()
last_name = serializers.CharField()

def create(
self,
validated_data
):
def create(self, validated_data):
return Actor.objects.create(**validated_data)

def update(
self,
instance,
validated_data
):
def update(self, instance, validated_data):
instance.first_name = validated_data.get(
"first_name",
instance.first_name
Expand All @@ -71,17 +57,10 @@ class GenreSerializer(serializers.Serializer):
id = serializers.IntegerField(read_only=True)
name = serializers.CharField()

def create(
self,
validated_data
):
def create(self, validated_data):
return Genre.objects.create(**validated_data)

def update(
self,
instance,
validated_data
):
def update(self, instance, validated_data):
instance.name = validated_data.get(
"name",
instance.name
Expand All @@ -97,17 +76,10 @@ class CinemaHallSerializer(serializers.Serializer):
rows = serializers.IntegerField()
seats_in_row = serializers.IntegerField()

def create(
self,
validated_data
):
def create(self, validated_data):
return CinemaHall.objects.create(**validated_data)

def update(
self,
instance,
validated_data
):
def update(self, instance, validated_data):
instance.name = validated_data.get(
"name",
instance.name
Expand Down
94 changes: 29 additions & 65 deletions cinema/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,56 +19,44 @@


class GenreList(APIView):
def get(
self, request):
def get(self, request):
genres = Genre.objects.all()
serializer = GenreSerializer(
genres, many=True)
return Response(
serializer.data,
status=status.HTTP_200_OK)
serializer = GenreSerializer(genres, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)

def post(self, request):
serializer = GenreSerializer(data=request.data)

if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(
serializer.data, status=status.HTTP_201_CREATED
)


class GenreDetail(APIView):
def get_object(self, pk):
genre = get_object_or_404(
Genre.objects.all(),
pk=pk)
genre = get_object_or_404(Genre.objects.all(), pk=pk)
return genre

def get(self, request, pk):
genre = self.get_object(pk)
serializer = GenreSerializer(genre)
return Response(
serializer.data,
status=status.HTTP_200_OK)
return Response(serializer.data, status=status.HTTP_200_OK)

def put(self, request, pk):
genre = self.get_object(pk)
serializer = GenreSerializer(genre, data=request.data)

if serializer.is_valid():
serializer.save()
return Response(
serializer.data,
status=status.HTTP_200_OK)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)

def patch(self, request, pk):
genre = self.get_object(pk)
serializer = GenreSerializer(genre, data=request.data, partial=True)

if serializer.is_valid():
serializer.save()
return Response(
serializer.data,
status=status.HTTP_200_OK)
serializer.is_valid(raise_exception=True)
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)

def delete(self, request, pk):
genre = self.get_object(pk)
Expand All @@ -84,19 +72,11 @@ class ActorList(
queryset = Actor.objects.all()
serializer_class = ActorSerializer

def get(
self, request,
*args, **kwargs):
return self.list(
request, *args,
**kwargs)
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)

def post(
self, request,
*args, **kwargs):
return self.create(
request, *args,
**kwargs)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)


class ActorDetail(
Expand All @@ -108,33 +88,17 @@ class ActorDetail(
queryset = Actor.objects.all()
serializer_class = ActorSerializer

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


class CinemaHallViewSet(
Expand Down

0 comments on commit e3653c4

Please sign in to comment.