-
Notifications
You must be signed in to change notification settings - Fork 58
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 #39
base: main
Are you sure you want to change the base?
solution #39
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
from typing import List | ||
|
||
from fastapi import HTTPException | ||
from sqlalchemy.exc import SQLAlchemyError | ||
from sqlalchemy.orm import Session | ||
|
||
from database.models import MovieModel, CountryModel, GenreModel, ActorModel, LanguageModel | ||
|
||
from schemas.movies import MovieCreateSchema, MovieUpdateSchema | ||
|
||
|
||
def get_movies(db: Session, offset, per_page): | ||
return db.query(MovieModel).order_by(MovieModel.id.desc()).offset(offset).limit(per_page).all() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider adding validation for the |
||
|
||
|
||
def get_movie(db: Session, movie_id): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
return db.query(MovieModel).filter(MovieModel.id == movie_id).first() | ||
|
||
|
||
def create_objects(data: List[str], model, db: Session): | ||
|
||
objects = [model(name=element) for element in data] | ||
db.add_all(objects) | ||
db.commit() | ||
|
||
for obj in objects: | ||
db.refresh(obj) | ||
|
||
return objects | ||
|
||
|
||
def create_movie(db: Session, movie: MovieCreateSchema): | ||
|
||
try: | ||
|
||
country_data = db.query(CountryModel).filter(CountryModel.code == movie.country).first() | ||
if not country_data: | ||
country_data = CountryModel(code=movie.country) | ||
db.add(country_data) | ||
db.commit() | ||
db.refresh(country_data) | ||
|
||
genres_data = db.query(GenreModel).filter(GenreModel.name.in_(movie.genres)).all() | ||
if not genres_data: | ||
genres_data = create_objects(movie.genres, GenreModel, db) | ||
|
||
actors_data = db.query(ActorModel).filter(ActorModel.name.in_(movie.actors)).all() | ||
if not actors_data: | ||
actors_data = create_objects(movie.actors, ActorModel, db) | ||
|
||
languages_data = db.query(LanguageModel).filter(LanguageModel.name.in_(movie.languages)).all() | ||
if not languages_data: | ||
languages_data = create_objects(movie.languages, LanguageModel, db) | ||
|
||
db_movie = MovieModel( | ||
name=movie.name, | ||
date=movie.date, | ||
score=movie.score, | ||
overview=movie.overview, | ||
status=movie.status, | ||
budget=movie.budget, | ||
revenue=movie.revenue, | ||
country=country_data, | ||
genres=genres_data, | ||
actors=actors_data, | ||
languages=languages_data | ||
) | ||
|
||
db.add(db_movie) | ||
db.commit() | ||
db.refresh(db_movie) | ||
return db_movie | ||
|
||
except SQLAlchemyError: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Catching |
||
db.rollback() | ||
raise HTTPException(status_code=400, detail="Invalid input data.") | ||
|
||
|
||
def delete_movie(db: Session, movie_id: int): | ||
db_movie = db.query(MovieModel).filter(MovieModel.id == movie_id).first() | ||
if not db_movie: | ||
raise HTTPException(status_code=404, detail="Movie with the given ID was not found.") | ||
db.delete(db_movie) | ||
db.commit() | ||
|
||
|
||
def update_movie(db: Session, movie_id: int, movie: MovieUpdateSchema): | ||
db_movie = db.query(MovieModel).filter(MovieModel.id == movie_id).first() | ||
if not db_movie: | ||
raise HTTPException(status_code=404, detail="Movie with the given ID was not found.") | ||
|
||
for key, value in movie.dict(exclude_unset=True).items(): | ||
setattr(db_movie, key, value) | ||
|
||
db.commit() | ||
db.refresh(db_movie) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,114 @@ | ||
"""initial migration | ||
|
||
Revision ID: ea3a65568bd9 | ||
Revises: | ||
Revises: | ||
Create Date: 2025-01-02 21:07:13.284837 | ||
|
||
""" | ||
|
||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = 'ea3a65568bd9' | ||
revision: str = "ea3a65568bd9" | ||
down_revision: Union[str, None] = None | ||
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.create_table('actors', | ||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), | ||
sa.Column('name', sa.String(length=255), nullable=False), | ||
sa.PrimaryKeyConstraint('id'), | ||
sa.UniqueConstraint('name') | ||
op.create_table( | ||
"actors", | ||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), | ||
sa.Column("name", sa.String(length=255), nullable=False), | ||
sa.PrimaryKeyConstraint("id"), | ||
sa.UniqueConstraint("name"), | ||
) | ||
op.create_table('countries', | ||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), | ||
sa.Column('code', sa.String(length=3), nullable=False), | ||
sa.Column('name', sa.String(length=255), nullable=True), | ||
sa.PrimaryKeyConstraint('id'), | ||
sa.UniqueConstraint('code') | ||
op.create_table( | ||
"countries", | ||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), | ||
sa.Column("code", sa.String(length=3), nullable=False), | ||
sa.Column("name", sa.String(length=255), nullable=True), | ||
sa.PrimaryKeyConstraint("id"), | ||
sa.UniqueConstraint("code"), | ||
) | ||
op.create_table('genres', | ||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), | ||
sa.Column('name', sa.String(length=255), nullable=False), | ||
sa.PrimaryKeyConstraint('id'), | ||
sa.UniqueConstraint('name') | ||
op.create_table( | ||
"genres", | ||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), | ||
sa.Column("name", sa.String(length=255), nullable=False), | ||
sa.PrimaryKeyConstraint("id"), | ||
sa.UniqueConstraint("name"), | ||
) | ||
op.create_table('languages', | ||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), | ||
sa.Column('name', sa.String(length=255), nullable=False), | ||
sa.PrimaryKeyConstraint('id'), | ||
sa.UniqueConstraint('name') | ||
op.create_table( | ||
"languages", | ||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), | ||
sa.Column("name", sa.String(length=255), nullable=False), | ||
sa.PrimaryKeyConstraint("id"), | ||
sa.UniqueConstraint("name"), | ||
) | ||
op.create_table('movies', | ||
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), | ||
sa.Column('name', sa.String(length=255), nullable=False), | ||
sa.Column('date', sa.Date(), nullable=False), | ||
sa.Column('score', sa.Float(), nullable=False), | ||
sa.Column('overview', sa.Text(), nullable=False), | ||
sa.Column('status', sa.Enum('RELEASED', 'POST_PRODUCTION', 'IN_PRODUCTION', name='moviestatusenum'), nullable=False), | ||
sa.Column('budget', sa.DECIMAL(precision=15, scale=2), nullable=False), | ||
sa.Column('revenue', sa.Float(), nullable=False), | ||
sa.Column('country_id', sa.Integer(), nullable=False), | ||
sa.ForeignKeyConstraint(['country_id'], ['countries.id'], ), | ||
sa.PrimaryKeyConstraint('id'), | ||
sa.UniqueConstraint('name', 'date', name='unique_movie_constraint') | ||
op.create_table( | ||
"movies", | ||
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), | ||
sa.Column("name", sa.String(length=255), nullable=False), | ||
sa.Column("date", sa.Date(), nullable=False), | ||
sa.Column("score", sa.Float(), nullable=False), | ||
sa.Column("overview", sa.Text(), nullable=False), | ||
sa.Column( | ||
"status", | ||
sa.Enum( | ||
"RELEASED", "POST_PRODUCTION", "IN_PRODUCTION", name="moviestatusenum" | ||
), | ||
nullable=False, | ||
), | ||
sa.Column("budget", sa.DECIMAL(precision=15, scale=2), nullable=False), | ||
sa.Column("revenue", sa.Float(), nullable=False), | ||
sa.Column("country_id", sa.Integer(), nullable=False), | ||
sa.ForeignKeyConstraint( | ||
["country_id"], | ||
["countries.id"], | ||
), | ||
sa.PrimaryKeyConstraint("id"), | ||
sa.UniqueConstraint("name", "date", name="unique_movie_constraint"), | ||
) | ||
op.create_table('actors_movies', | ||
sa.Column('movie_id', sa.Integer(), nullable=False), | ||
sa.Column('actor_id', sa.Integer(), nullable=False), | ||
sa.ForeignKeyConstraint(['actor_id'], ['actors.id'], ondelete='CASCADE'), | ||
sa.ForeignKeyConstraint(['movie_id'], ['movies.id'], ondelete='CASCADE'), | ||
sa.PrimaryKeyConstraint('movie_id', 'actor_id') | ||
op.create_table( | ||
"actors_movies", | ||
sa.Column("movie_id", sa.Integer(), nullable=False), | ||
sa.Column("actor_id", sa.Integer(), nullable=False), | ||
sa.ForeignKeyConstraint(["actor_id"], ["actors.id"], ondelete="CASCADE"), | ||
sa.ForeignKeyConstraint(["movie_id"], ["movies.id"], ondelete="CASCADE"), | ||
sa.PrimaryKeyConstraint("movie_id", "actor_id"), | ||
) | ||
op.create_table('movies_genres', | ||
sa.Column('movie_id', sa.Integer(), nullable=False), | ||
sa.Column('genre_id', sa.Integer(), nullable=False), | ||
sa.ForeignKeyConstraint(['genre_id'], ['genres.id'], ondelete='CASCADE'), | ||
sa.ForeignKeyConstraint(['movie_id'], ['movies.id'], ondelete='CASCADE'), | ||
sa.PrimaryKeyConstraint('movie_id', 'genre_id') | ||
op.create_table( | ||
"movies_genres", | ||
sa.Column("movie_id", sa.Integer(), nullable=False), | ||
sa.Column("genre_id", sa.Integer(), nullable=False), | ||
sa.ForeignKeyConstraint(["genre_id"], ["genres.id"], ondelete="CASCADE"), | ||
sa.ForeignKeyConstraint(["movie_id"], ["movies.id"], ondelete="CASCADE"), | ||
sa.PrimaryKeyConstraint("movie_id", "genre_id"), | ||
) | ||
op.create_table('movies_languages', | ||
sa.Column('movie_id', sa.Integer(), nullable=False), | ||
sa.Column('language_id', sa.Integer(), nullable=False), | ||
sa.ForeignKeyConstraint(['language_id'], ['languages.id'], ondelete='CASCADE'), | ||
sa.ForeignKeyConstraint(['movie_id'], ['movies.id'], ondelete='CASCADE'), | ||
sa.PrimaryKeyConstraint('movie_id', 'language_id') | ||
op.create_table( | ||
"movies_languages", | ||
sa.Column("movie_id", sa.Integer(), nullable=False), | ||
sa.Column("language_id", sa.Integer(), nullable=False), | ||
sa.ForeignKeyConstraint(["language_id"], ["languages.id"], ondelete="CASCADE"), | ||
sa.ForeignKeyConstraint(["movie_id"], ["movies.id"], ondelete="CASCADE"), | ||
sa.PrimaryKeyConstraint("movie_id", "language_id"), | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table('movies_languages') | ||
op.drop_table('movies_genres') | ||
op.drop_table('actors_movies') | ||
op.drop_table('movies') | ||
op.drop_table('languages') | ||
op.drop_table('genres') | ||
op.drop_table('countries') | ||
op.drop_table('actors') | ||
op.drop_table("movies_languages") | ||
op.drop_table("movies_genres") | ||
op.drop_table("actors_movies") | ||
op.drop_table("movies") | ||
op.drop_table("languages") | ||
op.drop_table("genres") | ||
op.drop_table("countries") | ||
op.drop_table("actors") | ||
# ### end Alembic commands ### |
There was a problem hiding this comment.
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 to5433:5432
. Ensure that port5433
is the intended external port for PostgreSQL, as the default PostgreSQL port is5432
. If you don't have a specific reason to change it, consider using5432:5432
.