-
Notifications
You must be signed in to change notification settings - Fork 729
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
K. Petrychenko
committed
Nov 20, 2024
1 parent
e170514
commit 54bb654
Showing
5 changed files
with
200 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,18 @@ | ||
from django.urls import path | ||
from django.urls import path, include | ||
from rest_framework.routers import DefaultRouter | ||
from cinema.views import GenreList, GenreDetail, \ | ||
ActorList, ActorDetail, CinemaHallViewSet, MovieViewSet | ||
|
||
from cinema.views import movie_list, movie_detail | ||
app_name = "cinema" | ||
|
||
router = DefaultRouter() | ||
router.register(r"cinema_halls", CinemaHallViewSet, basename="cinema_hall") | ||
router.register(r"movies", MovieViewSet, basename="movie") | ||
|
||
urlpatterns = [ | ||
path("movies/", movie_list, name="movie-list"), | ||
path("movies/<int:pk>/", movie_detail, name="movie-detail"), | ||
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>/", ActorDetail.as_view(), name="actor-detail"), | ||
path("", include(router.urls)), | ||
] | ||
|
||
app_name = "cinema" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,158 @@ | ||
from rest_framework.decorators import api_view | ||
from rest_framework.views import APIView | ||
from rest_framework.generics import RetrieveUpdateDestroyAPIView, \ | ||
ListCreateAPIView, GenericAPIView | ||
from rest_framework.viewsets import ModelViewSet, GenericViewSet | ||
from rest_framework.response import Response | ||
from rest_framework import status | ||
|
||
from django.shortcuts import get_object_or_404 | ||
|
||
from cinema.models import Movie | ||
from cinema.serializers import MovieSerializer | ||
from cinema.models import Actor, Genre, CinemaHall, Movie | ||
from cinema.serializers import ActorSerializer, GenreSerializer, \ | ||
CinemaHallSerializer, MovieSerializer | ||
|
||
|
||
class GenreList(APIView): | ||
""" | ||
Handles listing all genres and creating a new genre. | ||
""" | ||
def get(self, request): | ||
genres = Genre.objects.all() | ||
serializer = GenreSerializer(genres, many=True) | ||
return Response(serializer.data, status=status.HTTP_200_OK) | ||
|
||
def post(self, request): | ||
serializer = GenreSerializer(data=request.data) | ||
serializer.is_valid(raise_exception=True) | ||
serializer.save() | ||
return Response(serializer.data, status=status.HTTP_201_CREATED) | ||
|
||
|
||
@api_view(["GET", "POST"]) | ||
def movie_list(request): | ||
if request.method == "GET": | ||
movies = Movie.objects.all() | ||
serializer = MovieSerializer(movies, many=True) | ||
class GenreDetail(APIView): | ||
""" | ||
Handles retrieving, updating, or deleting a single genre. | ||
""" | ||
def get(self, request, pk): | ||
genre = get_object_or_404(Genre, pk=pk) | ||
serializer = GenreSerializer(genre) | ||
return Response(serializer.data, status=status.HTTP_200_OK) | ||
|
||
if request.method == "POST": | ||
serializer = MovieSerializer(data=request.data) | ||
def put(self, request, pk): | ||
genre = get_object_or_404(Genre, pk=pk) | ||
serializer = GenreSerializer(genre, data=request.data) | ||
serializer.is_valid(raise_exception=True) | ||
serializer.save() | ||
return Response(serializer.data, status=status.HTTP_200_OK) | ||
|
||
def patch(self, request, pk): | ||
""" | ||
Handle partial update for Genre. | ||
""" | ||
genre = get_object_or_404(Genre, pk=pk) | ||
serializer = GenreSerializer(genre, data=request.data, partial=True) | ||
if serializer.is_valid(): | ||
serializer.save() | ||
return Response(serializer.data, status=status.HTTP_201_CREATED) | ||
return Response(serializer.data, status=status.HTTP_200_OK) | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
def delete(self, request, pk): | ||
genre = get_object_or_404(Genre, pk=pk) | ||
genre.delete() | ||
return Response(status=status.HTTP_204_NO_CONTENT) | ||
|
||
|
||
class ActorList(ListCreateAPIView): | ||
""" | ||
Handles listing all actors and creating a new actor. | ||
""" | ||
queryset = Actor.objects.all() | ||
serializer_class = ActorSerializer | ||
|
||
|
||
class ActorDetail(RetrieveUpdateDestroyAPIView): | ||
""" | ||
Handles retrieving, updating, or deleting a single actor. | ||
""" | ||
queryset = Actor.objects.all() | ||
serializer_class = ActorSerializer | ||
|
||
|
||
class MovieViewSet(ModelViewSet): | ||
""" | ||
Handles CRUD operations for the Movie model. | ||
""" | ||
queryset = Movie.objects.all() | ||
serializer_class = MovieSerializer | ||
|
||
|
||
class ActorGenericAPIView(GenericAPIView): | ||
""" | ||
Handles CRUD operations for the Actor model. | ||
""" | ||
queryset = Actor.objects.all() | ||
serializer_class = ActorSerializer | ||
|
||
def get(self, request): | ||
actors = self.get_queryset() | ||
serializer = self.get_serializer(actors, many=True) | ||
return Response(serializer.data, status=status.HTTP_200_OK) | ||
|
||
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) | ||
|
||
|
||
@api_view(["GET", "PUT", "DELETE"]) | ||
def movie_detail(request, pk): | ||
movie = get_object_or_404(Movie, pk=pk) | ||
class CinemaHallViewSet(GenericViewSet): | ||
""" | ||
Handles CRUD operations for the CinemaHall model. | ||
""" | ||
queryset = CinemaHall.objects.all() | ||
serializer_class = CinemaHallSerializer | ||
|
||
def list(self, request): | ||
halls = self.get_queryset() | ||
serializer = self.get_serializer(halls, many=True) | ||
return Response(serializer.data, status=status.HTTP_200_OK) | ||
|
||
if request.method == "GET": | ||
serializer = MovieSerializer(movie) | ||
def retrieve(self, request, pk=None): | ||
hall = get_object_or_404(CinemaHall, pk=pk) | ||
serializer = self.get_serializer(hall) | ||
return Response(serializer.data, status=status.HTTP_200_OK) | ||
|
||
if request.method == "PUT": | ||
serializer = MovieSerializer(movie, data=request.data) | ||
def create(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 update(self, request, pk=None): | ||
hall = get_object_or_404(CinemaHall, pk=pk) | ||
serializer = self.get_serializer(hall, 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 partial_update(self, request, pk=None): | ||
hall = get_object_or_404(CinemaHall, pk=pk) | ||
serializer = self.get_serializer(hall, data=request.data, partial=True) | ||
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) | ||
|
||
if request.method == "DELETE": | ||
movie.delete() | ||
def destroy(self, request, pk=None): | ||
hall = get_object_or_404(CinemaHall, pk=pk) | ||
hall.delete() | ||
return Response(status=status.HTTP_204_NO_CONTENT) | ||
|
||
|
||
class MovieModelViewSet(ModelViewSet): | ||
""" | ||
Handles CRUD operations for the Movie model. | ||
""" | ||
queryset = Movie.objects.all() | ||
serializer_class = MovieSerializer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,16 @@ | ||
from django.contrib import admin | ||
from django.urls import path, include | ||
from django.http import HttpResponse | ||
|
||
|
||
def root_view(request): | ||
return HttpResponse( | ||
"Welcome to the Cinema API! Use /api/cinema/ to access the endpoints.", | ||
content_type="text/plain") | ||
|
||
|
||
urlpatterns = [ | ||
path("admin/", admin.site.urls), | ||
path("api/cinema/", include("cinema.urls", namespace="cinema")), | ||
path("", root_view), | ||
] |