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

Open
wants to merge 2 commits into
base: main
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
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
env_file:
- .env
ports:
- "5432:5432"
- "5433:5432"

Choose a reason for hiding this comment

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

The port mapping for the db service is set to 5433:5432. Ensure that port 5433 is not already in use on your host machine to avoid conflicts. If it is, consider using a different host port.

Choose a reason for hiding this comment

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

The db service is configured to use port 5433 on the host. Ensure this port is not already in use to avoid conflicts. If it is, consider changing the host port.

volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
- postgres_theater_data:/var/lib/postgresql/data/
Expand Down Expand Up @@ -45,7 +45,7 @@ services:
- PYTHONPATH=/usr/src/fastapi
- WATCHFILES_FORCE_POLLING=true
ports:
- "8000:8000"
- "8080:8000"
depends_on:
db:
condition: service_healthy
Expand Down
2 changes: 1 addition & 1 deletion src/database/migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from alembic import context

from database import models # noqa: F401
from database import models # noqa: F401
from database.models import Base
from database.session_postgresql import postgresql_engine

Expand Down
34 changes: 34 additions & 0 deletions src/database/migrations/versions/d0c53c351d00_temp_migration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""temp_migration

Revision ID: d0c53c351d00
Revises: ea3a65568bd9
Create Date: 2025-02-17 16:28:57.022412

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'd0c53c351d00'
down_revision: Union[str, None] = 'ea3a65568bd9'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('countries', 'name',
existing_type=sa.VARCHAR(length=255),
nullable=False)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('countries', 'name',
existing_type=sa.VARCHAR(length=255),
nullable=True)
# ### end Alembic commands ###
87 changes: 67 additions & 20 deletions src/database/models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
import datetime
from enum import Enum
from typing import Optional

from sqlalchemy import String, Float, Text, DECIMAL, UniqueConstraint, Date, ForeignKey, Table, Column
from sqlalchemy.orm import DeclarativeBase, mapped_column, Mapped, relationship
from sqlalchemy import (
String,
Float,
Text,
DECIMAL,
UniqueConstraint,
Date,
ForeignKey,
Table,
Column
)
from sqlalchemy.orm import (
DeclarativeBase,
mapped_column,
Mapped,
relationship
)
from sqlalchemy import Enum as SQLAlchemyEnum


Expand All @@ -24,36 +38,55 @@ class MovieStatusEnum(str, Enum):
Base.metadata,
Column(
"movie_id",
ForeignKey("movies.id", ondelete="CASCADE"), primary_key=True, nullable=False),
ForeignKey("movies.id", ondelete="CASCADE"),
primary_key=True,
nullable=False
),
Column(
"genre_id",
ForeignKey("genres.id", ondelete="CASCADE"), primary_key=True, nullable=False),
ForeignKey("genres.id", ondelete="CASCADE"),
primary_key=True,
nullable=False
),
)

ActorsMoviesModel = Table(
"actors_movies",
Base.metadata,
Column(
"movie_id",
ForeignKey("movies.id", ondelete="CASCADE"), primary_key=True, nullable=False),
ForeignKey("movies.id", ondelete="CASCADE"),
primary_key=True,
nullable=False
),
Column(
"actor_id",
ForeignKey("actors.id", ondelete="CASCADE"), primary_key=True, nullable=False),
ForeignKey("actors.id", ondelete="CASCADE"),
primary_key=True,
nullable=False
),
)

MoviesLanguagesModel = Table(
"movies_languages",
Base.metadata,
Column("movie_id", ForeignKey("movies.id", ondelete="CASCADE"), primary_key=True),
Column("language_id", ForeignKey("languages.id", ondelete="CASCADE"), primary_key=True),
Column("movie_id",
ForeignKey("movies.id", ondelete="CASCADE"),
primary_key=True),
Column("language_id",
ForeignKey("languages.id", ondelete="CASCADE"),
primary_key=True),
)


class GenreModel(Base):
__tablename__ = "genres"

id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
name: Mapped[str] = mapped_column(String(255), unique=True, nullable=False)
id: Mapped[int] = mapped_column(primary_key=True,
autoincrement=True)
name: Mapped[str] = mapped_column(String(255),
unique=True,
nullable=False)

movies: Mapped[list["MovieModel"]] = relationship(
"MovieModel",
Expand All @@ -69,7 +102,9 @@ class ActorModel(Base):
__tablename__ = "actors"

id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
name: Mapped[str] = mapped_column(String(255), unique=True, nullable=False)
name: Mapped[str] = mapped_column(String(255),
unique=True,
nullable=False)

movies: Mapped[list["MovieModel"]] = relationship(
"MovieModel",
Expand All @@ -86,9 +121,12 @@ class CountryModel(Base):

id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
code: Mapped[str] = mapped_column(String(3), unique=True, nullable=False)
name: Mapped[Optional[str]] = mapped_column(String(255), nullable=True)
name: Mapped[str] = mapped_column(String(255), nullable=False)

movies: Mapped[list["MovieModel"]] = relationship("MovieModel", back_populates="country")
movies: Mapped[list["MovieModel"]] = relationship(
"MovieModel",
back_populates="country"
)

def __repr__(self):
return f"<Country(code='{self.code}', name='{self.name}')>"
Expand All @@ -98,7 +136,9 @@ class LanguageModel(Base):
__tablename__ = "languages"

id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
name: Mapped[str] = mapped_column(String(255), unique=True, nullable=False)
name: Mapped[str] = mapped_column(String(255),
unique=True,
nullable=False)

movies: Mapped[list["MovieModel"]] = relationship(
"MovieModel",
Expand All @@ -121,11 +161,14 @@ class MovieModel(Base):
status: Mapped[MovieStatusEnum] = mapped_column(
SQLAlchemyEnum(MovieStatusEnum), nullable=False
)
budget: Mapped[float] = mapped_column(DECIMAL(15, 2), nullable=False)
budget: Mapped[float] = mapped_column(DECIMAL(15, 2),
nullable=False)
revenue: Mapped[float] = mapped_column(Float, nullable=False)

country_id: Mapped[int] = mapped_column(ForeignKey("countries.id"), nullable=False)
country: Mapped["CountryModel"] = relationship("CountryModel", back_populates="movies")
country_id: Mapped[int] = mapped_column(ForeignKey("countries.id"),
nullable=False)
country: Mapped["CountryModel"] = relationship("CountryModel",
back_populates="movies")

genres: Mapped[list["GenreModel"]] = relationship(
"GenreModel",
Expand All @@ -146,12 +189,16 @@ class MovieModel(Base):
)

__table_args__ = (
UniqueConstraint("name", "date", name="unique_movie_constraint"),
UniqueConstraint("name",
"date",
name="unique_movie_constraint"),
)

@classmethod
def default_order_by(cls):
return [cls.id.desc()]

def __repr__(self):
return f"<Movie(name='{self.name}', release_date='{self.date}', score={self.score})>"
return (f"<Movie(name='{self.name}', "
f"release_date='{self.date}', "
f"score={self.score})>")
10 changes: 9 additions & 1 deletion src/database/populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@

from config import get_settings
from database import MovieModel, get_db_contextmanager
from database.models import CountryModel, GenreModel, ActorModel, MoviesGenresModel, ActorsMoviesModel, LanguageModel, \
from database.models import (
CountryModel,
GenreModel,
ActorModel,
MoviesGenresModel,
ActorsMoviesModel,
LanguageModel,
MoviesLanguagesModel
)


class CSVDatabaseSeeder:
Expand Down Expand Up @@ -139,6 +146,7 @@ def seed(self):
print(f"Unexpected error: {e}")
raise


def main():
settings = get_settings()
with get_db_contextmanager() as db_session:
Expand Down
7 changes: 4 additions & 3 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from fastapi import FastAPI

from routes import movie_router
from routes.movies import router as movie_router

app = FastAPI(
title="Movies homework",
Expand All @@ -9,4 +8,6 @@

api_version_prefix = "/api/v1"

app.include_router(movie_router, prefix=f"{api_version_prefix}/theater", tags=["theater"])
app.include_router(movie_router,
prefix=f"{api_version_prefix}/theater",
tags=["theater"])
Loading
Loading