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 #1093

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 29 additions & 0 deletions db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,32 @@ class Actor(models.Model):

def __str__(self) -> str:
return f"{self.first_name} {self.last_name}"


class Movie(models.Model):
title = models.CharField(max_length=255, unique=True)
description = models.TextField()
actors = models.ManyToManyField(Actor)
gerne = models.ManyToManyField(Genre)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a typo here: gerne should be genre. This will cause an error when trying to access this field.


def __str__(self) -> str:
return self.title


class CinemaHall(models.Model):
name = models.CharField(max_length=255)
rows = models.IntegerField(null=False)
seats_in_row = models.IntegerField(null=False)

def __str__(self):
return self.name

@property
def capacity(self) -> int:
return self.rows * self.seats_in_row

class MovieSession(models.Model):
show_time = models.DateTimeField()
cinema_hall = models.ForeignKey(CinemaHall,
on_delete=models.CASCADE)
movie = models.ForeignKey(Movie, on_delete=models.CASCADE)
Empty file added services/__init__.py
Empty file.
14 changes: 14 additions & 0 deletions services/cinema_hall.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from db.models import CinemaHall
from django.db.models import QuerySet


def get_cinema_halls() -> QuerySet:
return CinemaHall.objects.all()


def create_cinema_hall(hall_name: str,
hall_rows: int,
hall_seats_in_row: int) -> CinemaHall:
return CinemaHall(hall_name,
hall_rows,
hall_seats_in_row)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The create_cinema_hall function creates a CinemaHall instance but does not save it to the database. You should call the save() method on the CinemaHall instance to persist it to the database.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The create_cinema_hall function should save the new CinemaHall instance to the database. You can do this by calling the save() method on the instance before returning it. This will ensure the cinema hall is persisted in the database.

29 changes: 29 additions & 0 deletions services/movie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from db.models import Movie
from django.db.models import QuerySet


def get_movies(genres_ids: list = None, /,
actors_ids: list = None) -> QuerySet:
movies = Movie.objects.all()
if genres_ids:
movies.filter(genres__in=genres_ids)
if actors_ids:
movies.filter(actors__in=actors_ids)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filter method is not being applied correctly. You need to assign the result of the filter method back to the movies variable, like movies = movies.filter(...), to actually apply the filters.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filter method is not being applied correctly in the get_movies function. You need to assign the result of the filter method back to the movies variable to actually apply the filters. For example: movies = movies.filter(genres__in=genres_ids).

return movies


def get_movie_by_id(movie_id) -> Movie:
return Movie.objects.filter(id=movie_id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The get_movie_by_id function should use get() instead of filter() to retrieve a single instance. Using filter() returns a QuerySet, not a single Movie instance.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The get_movie_by_id function should use .get() instead of .filter() to retrieve a single Movie instance. This will raise an exception if the movie is not found, which is typically the desired behavior when fetching a single object by ID.



def create_movie(movie_title: str, movie_description: str,
genres_ids: list = None,
actors_ids: list = None) -> Movie:
new_movie = Movie.objects.create(title=movie_title,
description=movie_description)
if genres_ids:
new_movie.gernes.set(genres_ids)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a typo here: gernes should be genres. This will cause an error when trying to set the genres for the movie.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a typo in the create_movie function. The method new_movie.gernes.set(genres_ids) should be new_movie.genres.set(genres_ids) to match the corrected field name in the Movie model.

if actors_ids:
new_movie.actors.set(actors_ids)

return new_movie
42 changes: 42 additions & 0 deletions services/movie_session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from db.models import MovieSession
from datetime import datetime
from django.db.models import QuerySet


def create_movie_session(movie_show_time: datetime,
movie_id: int,
cinema_hall_id: int) -> MovieSession:
return MovieSession.objects.create(
show_time=movie_show_time, movie_id=movie_id,
cinema_hall_id=cinema_hall_id)


def get_movies_sessions(session_date: datetime.date = None) -> QuerySet:
sessions_list = MovieSession.objecta.all()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a typo here: objecta should be objects. This will cause an error when trying to retrieve all movie sessions.

if session_date:
sessions_list.filter(show_time__date=session_date)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filter method is not being applied correctly. You need to assign the result of the filter method back to the sessions_list variable, like sessions_list = sessions_list.filter(...), to actually apply the filter.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filter method is not being applied correctly in the get_movies_sessions function. You need to assign the result of the filter method back to the sessions_list variable to actually apply the filter. For example: sessions_list = sessions_list.filter(show_time__date=session_date).

return sessions_list


def get_movie_session_by_id(movie_session_id: int) -> MovieSession:
return MovieSession.objects.get(id=movie_sessin_id)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a typo here: movie_sessin_id should be movie_session_id. This will cause an error when trying to retrieve a movie session by ID.



def update_movie_session(session_id: int,
show_time: datetime = None,
movie_id: int = None,
cinema_hall_id: int = None) -> MovieSession:
movie_session = MovieSession.objects.get(id=session_id)
if show_time:
movie_session.show_time = show_time
if movie_id:
movie_session.movie_id = movie_id
if cinema_hall_id:
movie_session.cinema_hall_id = cinema_hall_id

movie_session.save()
return movie_session


def delete_movie_session_by_id(session_id: int) -> None:
MovieSession.objects.get(id=session_id).delete()
Loading