diff --git a/cinema/models.py b/cinema/models.py index c42d2a3d..724a6e8c 100644 --- a/cinema/models.py +++ b/cinema/models.py @@ -27,8 +27,12 @@ class Actor(models.Model): first_name = models.CharField(max_length=255) last_name = models.CharField(max_length=255) + @property + def full_name(self): + return self.__str__() + def __str__(self): - return self.first_name + " " + self.last_name + return f"{self.first_name} {self.last_name}" class Movie(models.Model): diff --git a/cinema/serializers.py b/cinema/serializers.py index 612ca7e2..769837a4 100644 --- a/cinema/serializers.py +++ b/cinema/serializers.py @@ -1 +1,67 @@ -# write serializers here +from rest_framework import serializers +from .models import Actor, CinemaHall, Genre, Movie, MovieSession, Order, \ + Ticket + + +class ActorSerializer(serializers.ModelSerializer): + class Meta: + model = Actor + fields = ["id", "first_name", "last_name", "full_name"] + + +class GenreSerializer(serializers.ModelSerializer): + class Meta: + model = Genre + fields = ["id", "name"] + + +class MovieSerializer(serializers.ModelSerializer): + class Meta: + model = Movie + fields = ["id", "title", "description", "duration", "genres", "actors"] + + +class MovieListSerializer(MovieSerializer): + genres = serializers.SlugRelatedField( + many=True, read_only=True, slug_field="name" + ) + actors = serializers.SlugRelatedField( + many=True, read_only=True, slug_field="full_name" + ) + + +class MovieDetailSerializer(MovieSerializer): + genres = GenreSerializer(many=True, read_only=True) + actors = ActorSerializer(many=True, read_only=True) + + +class CinemaHallSerializer(serializers.ModelSerializer): + class Meta: + model = CinemaHall + fields = ["id", "name", "rows", "seats_in_row", "capacity"] + + +class MovieSessionSerializer(serializers.ModelSerializer): + class Meta: + model = MovieSession + fields = ["id", "show_time", "movie", "cinema_hall"] + + +class MovieSessionListSerializer(MovieSessionSerializer): + movie_title = serializers.CharField(source="movie.title", read_only=True) + cinema_hall_name = serializers.CharField( + source="cinema_hall.name", read_only=True + ) + cinema_hall_capacity = serializers.IntegerField( + source="cinema_hall.capacity", read_only=True + ) + + class Meta: + model = MovieSession + fields = ["id", "show_time", "movie_title", "cinema_hall_name", + "cinema_hall_capacity"] + + +class MovieSessionDetailSerializer(MovieSessionSerializer): + movie = MovieListSerializer(read_only=True) + cinema_hall = CinemaHallSerializer(read_only=True) diff --git a/cinema/urls.py b/cinema/urls.py index 420f8e8c..675aa451 100644 --- a/cinema/urls.py +++ b/cinema/urls.py @@ -1 +1,16 @@ -# write urls here +from django.urls import path, include +from rest_framework import routers + +from cinema.views import GenreViewSet, ActorViewSet, CinemaHallViewSet, \ + MovieViewSet, MovieSessionViewSet + +router = routers.DefaultRouter() +router.register("genres", GenreViewSet) +router.register("actors", ActorViewSet) +router.register("cinema_halls", CinemaHallViewSet) +router.register("movies", MovieViewSet) +router.register("movie_sessions", MovieSessionViewSet) + +urlpatterns = [ + path("", include(router.urls)), +] diff --git a/cinema/views.py b/cinema/views.py index ae87bfde..0fefaa69 100644 --- a/cinema/views.py +++ b/cinema/views.py @@ -1 +1,43 @@ -# write views here +from rest_framework import viewsets +from cinema.models import Genre, Actor, CinemaHall, Movie, MovieSession +from cinema.serializers import GenreSerializer, ActorSerializer, \ + CinemaHallSerializer, MovieSerializer, MovieSessionSerializer, \ + MovieListSerializer, MovieDetailSerializer, MovieSessionListSerializer, \ + MovieSessionDetailSerializer + + +class GenreViewSet(viewsets.ModelViewSet): + queryset = Genre.objects.all() + serializer_class = GenreSerializer + + +class ActorViewSet(viewsets.ModelViewSet): + queryset = Actor.objects.all() + serializer_class = ActorSerializer + + +class CinemaHallViewSet(viewsets.ModelViewSet): + queryset = CinemaHall.objects.all() + serializer_class = CinemaHallSerializer + + +class MovieViewSet(viewsets.ModelViewSet): + queryset = Movie.objects.prefetch_related("actors", "genres") + + def get_serializer_class(self): + if self.action == "list": + return MovieListSerializer + elif self.action == "retrieve": + return MovieDetailSerializer + return MovieSerializer + + +class MovieSessionViewSet(viewsets.ModelViewSet): + queryset = MovieSession.objects.select_related("movie", "cinema_hall") + + def get_serializer_class(self): + if self.action == "list": + return MovieSessionListSerializer + elif self.action == "retrieve": + return MovieSessionDetailSerializer + return MovieSessionSerializer diff --git a/cinema_service/urls.py b/cinema_service/urls.py index 083932c6..fda5a995 100644 --- a/cinema_service/urls.py +++ b/cinema_service/urls.py @@ -1,6 +1,7 @@ from django.contrib import admin -from django.urls import path +from django.urls import path, include urlpatterns = [ path("admin/", admin.site.urls), + path("api/cinema/", include("cinema.urls")), ]