Skip to content

Commit

Permalink
Solution with fixed bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
obohatov committed Oct 14, 2023
1 parent e6b0377 commit c366fd4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 41 deletions.
3 changes: 0 additions & 3 deletions cinema/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ def __str__(self):
class Genre(models.Model):
name = models.CharField(max_length=63, unique=True)

def __str__(self):
return f"{self.name}"


class CinemaHall(models.Model):
name = models.CharField(max_length=63)
Expand Down
58 changes: 20 additions & 38 deletions cinema/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,67 +28,49 @@ def get(
serializer.data,
status=status.HTTP_200_OK)

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

if serializer.is_valid(raise_exception=True):
if serializer.is_valid():
serializer.save()

return Response(
serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
return Response(serializer.data, status=status.HTTP_201_CREATED)


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

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

def put(
self, request,
pk):
def put(self, request, pk):
genre = self.get_object(pk)
serializer = GenreSerializer(
genre,
data=request.data)
if serializer.is_valid(raise_exception=True):
serializer.save()
serializer = GenreSerializer(genre, data=request.data)

return Response(
serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
if serializer.is_valid():
serializer.save()
return Response(
serializer.data,
status=status.HTTP_200_OK)

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

return Response(
serializer.errors,
status=status.HTTP_400_BAD_REQUEST)
if serializer.is_valid():
serializer.save()
return Response(
serializer.data,
status=status.HTTP_200_OK)

def delete(
self, request,
pk):
def delete(self, request, pk):
genre = self.get_object(pk)
genre.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
Expand Down

0 comments on commit c366fd4

Please sign in to comment.