Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
FursykIvan committed Dec 16, 2024
1 parent 3db5b6f commit 0ba6e7b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 44 deletions.
18 changes: 10 additions & 8 deletions cinema/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
from rest_framework.routers import DefaultRouter

from cinema.views import (
GenreAPIView,
ActorGenericAPIView,
GenreList,
GenreDetail,
ActorList,
ActorDetail,
CinemaHallViewSet,
MovieModelViewSet
MovieViewSet
)

app_name = "cinema"

router = DefaultRouter()
router.register("movies", MovieModelViewSet, basename="movies")
router.register("movies", MovieViewSet, basename="movies")

cinema_hall_list = CinemaHallViewSet.as_view(
actions={"get": "list",
Expand All @@ -27,11 +29,11 @@

urlpatterns = [
path("", include(router.urls)),
path("genres/", GenreAPIView.as_view(), name="genre-list"),
path("genres/<int:pk>/", GenreAPIView.as_view(), name="genre-detail"),
path("actors/", ActorGenericAPIView.as_view(), name="actor-list"),
path("genres/", GenreList.as_view(), name="genre-list"),
path("genres/<int:pk>/", GenreDetail.as_view(), name="genre-detail"),
path("actors/", ActorList.as_view(), name="actor-list"),
path("actors/<int:pk>/",
ActorGenericAPIView.as_view(),
ActorDetail.as_view(),
name="actor-detail"),
path("cinema_halls/",
cinema_hall_list,
Expand Down
69 changes: 33 additions & 36 deletions cinema/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
CinemaHallSerializer)


class GenreAPIView(APIView):
class GenreList(APIView):
def get(self, request):
genres = Genre.objects.all()
serializer = GenreSerializer(genres, many=True)
Expand All @@ -25,9 +25,16 @@ def post(self, request):
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class GenreDetail(APIView):
def get_object(self, pk):
return get_object_or_404(Genre, pk=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):
genre = self.get_object(pk)
serializer = GenreSerializer(genre, data=request.data)
Expand All @@ -50,47 +57,37 @@ def delete(self, request, pk):
return Response(status=status.HTTP_204_NO_CONTENT)


class ActorGenericAPIView(generics.GenericAPIView):
class ActorList(mixins.ListModelMixin,
mixins.CreateModelMixin,
generics.GenericAPIView):
queryset = Actor.objects.all()
serializer_class = ActorSerializer

def get(self, request, pk=None):
if pk is None:
actors = self.get_queryset()
serializer = self.get_serializer(actors, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
else:
actor = self.get_object()
serializer = self.get_serializer(actor)
return Response(serializer.data, status=status.HTTP_200_OK)
def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)

def post(self, request):
serializer = self.get_serializer(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)
def post(self, request, *args, **kwargs):
return self.create(request, *args, **kwargs)

def put(self, request):
actor = self.get_object()
serializer = self.get_serializer(actor, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

def patch(self, request):
actor = self.get_object()
serializer = self.get_serializer(actor, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
class ActorDetail(mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
generics.GenericAPIView):
queryset = Actor.objects.all()
serializer_class = ActorSerializer

def delete(self, request):
actor = self.get_object()
actor.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
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 All @@ -105,6 +102,6 @@ class CinemaHallViewSet(
serializer_class = CinemaHallSerializer


class MovieModelViewSet(viewsets.ModelViewSet):
class MovieViewSet(viewsets.ModelViewSet):
queryset = Movie.objects.all()
serializer_class = MovieSerializer

0 comments on commit 0ba6e7b

Please sign in to comment.