From 54bb6548b80796811536feaaea02a73575d689b9 Mon Sep 17 00:00:00 2001 From: "K. Petrychenko" Date: Wed, 20 Nov 2024 02:56:22 +0200 Subject: [PATCH] Solution --- cinema/models.py | 24 +++++++ cinema/serializers.py | 20 +++++- cinema/urls.py | 20 ++++-- cinema/views.py | 155 +++++++++++++++++++++++++++++++++++------ cinema_service/urls.py | 9 +++ 5 files changed, 200 insertions(+), 28 deletions(-) diff --git a/cinema/models.py b/cinema/models.py index cc477513f..6c757bc11 100644 --- a/cinema/models.py +++ b/cinema/models.py @@ -1,6 +1,30 @@ from django.db import models +class Actor(models.Model): + first_name = models.CharField(max_length=100) + last_name = models.CharField(max_length=100) + + def __str__(self): + return f"{self.first_name} {self.last_name}" + + +class Genre(models.Model): + name = models.CharField(max_length=50, unique=True) + + def __str__(self): + return self.name + + +class CinemaHall(models.Model): + name = models.CharField(max_length=100) + rows = models.PositiveIntegerField() + seats_in_row = models.PositiveIntegerField() + + def __str__(self): + return self.name + + class Movie(models.Model): title = models.CharField(max_length=255) description = models.TextField() diff --git a/cinema/serializers.py b/cinema/serializers.py index 050db5771..ccb2f5b9b 100644 --- a/cinema/serializers.py +++ b/cinema/serializers.py @@ -1,6 +1,24 @@ from rest_framework import serializers -from cinema.models import Movie +from cinema.models import Actor, Genre, CinemaHall, Movie + + +class ActorSerializer(serializers.ModelSerializer): + class Meta: + model = Actor + fields = "__all__" + + +class GenreSerializer(serializers.ModelSerializer): + class Meta: + model = Genre + fields = "__all__" + + +class CinemaHallSerializer(serializers.ModelSerializer): + class Meta: + model = CinemaHall + fields = "__all__" class MovieSerializer(serializers.Serializer): diff --git a/cinema/urls.py b/cinema/urls.py index 1ae7d5cb0..41c5d495e 100644 --- a/cinema/urls.py +++ b/cinema/urls.py @@ -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//", movie_detail, name="movie-detail"), + path("genres/", GenreList.as_view(), name="genre-list"), + path("genres//", GenreDetail.as_view(), name="genre-detail"), + path("actors/", ActorList.as_view(), name="actor-list"), + path("actors//", ActorDetail.as_view(), name="actor-detail"), + path("", include(router.urls)), ] - -app_name = "cinema" diff --git a/cinema/views.py b/cinema/views.py index 78ba8a79c..794dfb869 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -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 diff --git a/cinema_service/urls.py b/cinema_service/urls.py index 229767ed9..089bdea9c 100644 --- a/cinema_service/urls.py +++ b/cinema_service/urls.py @@ -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), ]