Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution #721

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions cinema/models.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
20 changes: 19 additions & 1 deletion cinema/serializers.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
20 changes: 14 additions & 6 deletions cinema/urls.py
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"
155 changes: 134 additions & 21 deletions cinema/views.py
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
9 changes: 9 additions & 0 deletions cinema_service/urls.py
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),
]
Loading