Skip to content

Commit

Permalink
Create models and services
Browse files Browse the repository at this point in the history
  • Loading branch information
vmyronets committed Jan 14, 2025
1 parent 99c3ab0 commit 47e9dac
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ venv/
**__pycache__/
*.pyc
**db.sqlite3
.gitignore
42 changes: 42 additions & 0 deletions db/migrations/0002_cinemahall_movie_moviesession.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Generated by Django 4.0.2 on 2025-01-14 11:59

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('db', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='CinemaHall',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=255)),
('rows', models.IntegerField()),
('seats_in_row', models.IntegerField()),
],
),
migrations.CreateModel(
name='Movie',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('title', models.CharField(max_length=255)),
('description', models.TextField()),
('actors', models.ManyToManyField(related_name='movies', to='db.Actor')),
('genres', models.ManyToManyField(related_name='movies', to='db.Genre')),
],
),
migrations.CreateModel(
name='MovieSession',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('show_time', models.DateTimeField()),
('cinema_hall', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='db.cinemahall')),
('movie', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='db.movie')),
],
),
]
32 changes: 32 additions & 0 deletions db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,35 @@ 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)
description = models.TextField()
actors = models.ManyToManyField(Actor, related_name="movies")
genres = models.ManyToManyField(Genre, related_name="movies")

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


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

def __str__(self) -> str:
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)

def __str__(self) -> str:
return f"{self.movie.title} {self.show_time}"
Empty file added services/__init__.py
Empty file.
19 changes: 19 additions & 0 deletions services/cinema_hall.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.db.models import QuerySet

from db.models import CinemaHall


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


def create_cinema_hall(
hall_name: str,
hall_rows: int,
hall_seats_in_row: int
) -> None:
CinemaHall.objects.create(
name=hall_name,
rows=hall_rows,
seats_in_row=hall_seats_in_row
)
36 changes: 36 additions & 0 deletions services/movie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from django.db.models import QuerySet

from db.models import Movie


def get_movies(
genres_ids: list[int] = None,
actors_ids: list[int] = None
) -> QuerySet:
movies = Movie.objects.all()
if genres_ids:
movies = movies.filter(genres__id__in=genres_ids)
if actors_ids:
movies = movies.filter(actors__id__in=actors_ids)
return movies


def get_movie_by_id(movie_id: int) -> Movie:
return Movie.objects.get(pk=movie_id)


def create_movie(
movie_title: str,
movie_description: str,
genres_ids: list[int] = None,
actors_ids: list[int] = None
) -> None:
movie = Movie.objects.create(
title=movie_title,
description=movie_description
)
if genres_ids:
movie.genres.set(genres_ids)

if actors_ids:
movie.actors.set(actors_ids)
51 changes: 51 additions & 0 deletions services/movie_session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from datetime import datetime

from django.db.models import QuerySet

from db.models import MovieSession


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


def get_movies_sessions(session_date: datetime = None) -> QuerySet:
movies_sessions = MovieSession.objects.all()
if session_date is not None:
movies_sessions = movies_sessions.filter(show_time__date=session_date)
return movies_sessions


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


def update_movie_session(
session_id: int,
show_time: datetime = None,
movie_id: int = None,
cinema_hall_id: int = None
) -> None:
movie_session = get_movie_session_by_id(session_id)

if show_time is not None:
movie_session.show_time = show_time

if movie_id is not None:
movie_session.movie_id = movie_id

if cinema_hall_id is not None:
movie_session.cinema_hall_id = cinema_hall_id
movie_session.save()


def delete_movie_session_by_id(session_id: int) -> None:
get_movie_session_by_id(session_id).delete()

0 comments on commit 47e9dac

Please sign in to comment.