Complete genres and actors

This commit is contained in:
2024-07-06 20:08:30 +04:00
parent b9e22fcc4c
commit 4c0bdcbd6a
9 changed files with 33 additions and 36 deletions

View File

@@ -21,14 +21,18 @@ class AudiobooksCRUD(EntityCRUD[mdl.Audiobook]):
async def change_genres(db: AsyncSession, audiobook: mdl.Audiobook, info: sch.AudiobookCreate): async def change_genres(db: AsyncSession, audiobook: mdl.Audiobook, info: sch.AudiobookCreate):
audiobook_genres = await AudiobookGenresCRUD.get_all(db) audiobook_genres = await AudiobookGenresCRUD.get_all(db)
if (info.genres): if (info.genres):
genres_id = [genre.id for genre in info.genres]
audiobook.genres = [ audiobook.genres = [
genre for genre in audiobook_genres if genre.id in info.genres] genre for genre in audiobook_genres if genre.id in genres_id]
@staticmethod @staticmethod
async def add(db: AsyncSession, async def add(db: AsyncSession,
info: sch.AudiobookCreate, info: sch.AudiobookCreate,
owner_id: int): owner_id: int):
audiobook = mdl.Audiobook(**info.model_dump(), audiobook_data_db = \
{k: v for k, v in info.model_dump().items()
if not k in ["genres", "update_date"]}
audiobook = mdl.Audiobook(**audiobook_data_db,
update_date=strftime("%Y-%m-%d %H:%M:%S"), update_date=strftime("%Y-%m-%d %H:%M:%S"),
owner_id=owner_id) owner_id=owner_id)
await AudiobooksCRUD.change_genres(db, audiobook, info) await AudiobooksCRUD.change_genres(db, audiobook, info)

View File

@@ -21,14 +21,18 @@ class GamesCRUD(EntityCRUD[mdl.Game]):
async def change_genres(db: AsyncSession, game: mdl.Game, info: sch.GameCreate): async def change_genres(db: AsyncSession, game: mdl.Game, info: sch.GameCreate):
game_genres = await GameGenresCRUD.get_all(db) game_genres = await GameGenresCRUD.get_all(db)
if (info.genres): if (info.genres):
genres_id = [genre.id for genre in info.genres]
game.genres = [ game.genres = [
genre for genre in game_genres if genre.id in info.genres] genre for genre in game_genres if genre.id in genres_id]
@staticmethod @staticmethod
async def add(db: AsyncSession, async def add(db: AsyncSession,
info: sch.GameCreate, info: sch.GameCreate,
owner_id: int): owner_id: int):
game = mdl.Game(**info.model_dump(), game_data_db = \
{k: v for k, v in info.model_dump().items()
if not k in ["genres", "update_date"]}
game = mdl.Game(**game_data_db,
update_date=strftime("%Y-%m-%d %H:%M:%S"), update_date=strftime("%Y-%m-%d %H:%M:%S"),
owner_id=owner_id) owner_id=owner_id)
await GamesCRUD.change_genres(db, game, info) await GamesCRUD.change_genres(db, game, info)

View File

@@ -22,21 +22,26 @@ class MoviesCRUD(EntityCRUD[mdl.Movie]):
async def change_actors(db: AsyncSession, movie: mdl.Movie, info: sch.MovieCreate): async def change_actors(db: AsyncSession, movie: mdl.Movie, info: sch.MovieCreate):
movie_actors = await MovieActorsCRUD.get_all(db) movie_actors = await MovieActorsCRUD.get_all(db)
if (info.actors): if (info.actors):
actors_id = [actor.id for actor in info.actors]
movie.actors = [ movie.actors = [
actor for actor in movie_actors if actor.id in info.actors] actor for actor in movie_actors if actor.id in actors_id]
@staticmethod @staticmethod
async def change_genres(db: AsyncSession, movie: mdl.Movie, info: sch.MovieCreate): async def change_genres(db: AsyncSession, movie: mdl.Movie, info: sch.MovieCreate):
movie_genres = await MovieGenresCRUD.get_all(db) movie_genres = await MovieGenresCRUD.get_all(db)
if (info.genres): if (info.genres):
genres_id = [genre.id for genre in info.genres]
movie.genres = [ movie.genres = [
genre for genre in movie_genres if genre.id in info.genres] genre for genre in movie_genres if genre.id in genres_id]
@staticmethod @staticmethod
async def add(db: AsyncSession, async def add(db: AsyncSession,
info: sch.MovieCreate, info: sch.MovieCreate,
owner_id: int): owner_id: int):
movie = mdl.Movie(**info.model_dump(), movie_data_db = \
{k: v for k, v in info.model_dump().items()
if not k in ["actors", "genres", "update_date"]}
movie = mdl.Movie(**movie_data_db,
update_date=strftime("%Y-%m-%d %H:%M:%S"), update_date=strftime("%Y-%m-%d %H:%M:%S"),
owner_id=owner_id) owner_id=owner_id)
await MoviesCRUD.change_genres(db, movie, info) await MoviesCRUD.change_genres(db, movie, info)

View File

@@ -40,17 +40,17 @@ class AudiobookBase(AudiobookCardBase):
duration: Optional[str] = Field(default=None, examples=["12:38"]) duration: Optional[str] = Field(default=None, examples=["12:38"])
reader: Optional[str] = Field(default=None, examples=["Дмитрий Хазанович"]) reader: Optional[str] = Field(default=None, examples=["Дмитрий Хазанович"])
genres: Optional[list[AudiobookGenre]] = Field()
class AudiobookCreate(AudiobookBase): class AudiobookCreate(AudiobookBase):
genres: Optional[list[int]] = Field(default=None, examples=[[1, 2]]) ...
class Audiobook(AudiobookBase): class Audiobook(AudiobookBase):
id: int = Field(examples=[1]) id: int = Field(examples=[1])
update_date: str = Field(examples=["2024-06-14 12:00:00"]) update_date: str = Field(examples=["2024-06-14 12:00:00"])
genres: list[AudiobookGenre] = Field()
owner: User = Field() owner: User = Field()
model_config = ConfigDict(from_attributes=True) model_config = ConfigDict(from_attributes=True)

View File

@@ -41,17 +41,17 @@ class GameBase(GameCardBase):
release_date: Optional[str] = Field(default=None, examples=["2014"]) release_date: Optional[str] = Field(default=None, examples=["2014"])
download_size: Optional[str] = Field(default=None, examples=["80Mb"]) download_size: Optional[str] = Field(default=None, examples=["80Mb"])
genres: Optional[list[GameGenre]] = Field()
class GameCreate(GameBase): class GameCreate(GameBase):
genres: Optional[list[int]] = Field(default=None, examples=[[1, 2]]) ...
class Game(GameBase): class Game(GameBase):
id: int = Field(examples=[1]) id: int = Field(examples=[1])
update_date: str = Field(examples=["2024-05-13 12:00:00"]) update_date: str = Field(examples=["2024-05-13 12:00:00"])
genres: list[GameGenre] = Field()
owner: User = Field() owner: User = Field()
model_config = ConfigDict(from_attributes=True) model_config = ConfigDict(from_attributes=True)

View File

@@ -45,19 +45,18 @@ class MovieBase(MovieCardBase):
country: Optional[str] = \ country: Optional[str] = \
Field(default=None, examples=["США, Великобритания, Канада"]) Field(default=None, examples=["США, Великобритания, Канада"])
genres: Optional[list[MovieGenre]] = Field()
actors: Optional[list[MovieActor]] = Field()
class MovieCreate(MovieBase): class MovieCreate(MovieBase):
genres: Optional[list[int]] = Field(default=None, examples=[[1, 2]]) ...
actors: Optional[list[int]] = Field(default=None, examples=[[1, 2]])
class Movie(MovieBase): class Movie(MovieBase):
id: int = Field(examples=[1]) id: int = Field(examples=[1])
update_date: str = Field(examples=["2024-06-11 12:00:00"]) update_date: str = Field(examples=["2024-06-11 12:00:00"])
genres: list[MovieGenre] = Field()
actors: list[MovieActor] = Field()
owner: User = Field() owner: User = Field()
model_config = ConfigDict(from_attributes=True) model_config = ConfigDict(from_attributes=True)

View File

@@ -13,16 +13,11 @@ async def get_audiobook(audiobook_id: int, db_session: AsyncSession = Depends(Da
return await AudiobooksCRUD.get(db_session, audiobook_id) return await AudiobooksCRUD.get(db_session, audiobook_id)
@audiobooks_router.get("/cards", response_model=list[AudiobookCard]) @audiobooks_router.get("", response_model=list[AudiobookCard])
async def get_audiobooks_cards(db_session: AsyncSession = Depends(Database.get_session)): async def get_audiobooks_cards(db_session: AsyncSession = Depends(Database.get_session)):
return await AudiobooksCRUD.get_all(db_session) return await AudiobooksCRUD.get_all(db_session)
@audiobooks_router.get("", response_model=list[Audiobook])
async def get_audiobooks(db_session: AsyncSession = Depends(Database.get_session)):
return await AudiobooksCRUD.get_all(db_session)
@audiobooks_router.post("", response_model=Audiobook) @audiobooks_router.post("", response_model=Audiobook)
async def add_audiobook(audiobook: AudiobookCreate, async def add_audiobook(audiobook: AudiobookCreate,
user: User = Depends(get_user), user: User = Depends(get_user),

View File

@@ -14,16 +14,11 @@ async def get_game(game_id: int, db_session: AsyncSession = Depends(Database.get
return await GamesCRUD.get(db_session, game_id) return await GamesCRUD.get(db_session, game_id)
@games_router.get("/cards", response_model=list[GameCard]) @games_router.get("", response_model=list[GameCard])
async def get_games_cards(db_session: AsyncSession = Depends(Database.get_session)): async def get_games_cards(db_session: AsyncSession = Depends(Database.get_session)):
return await GamesCRUD.get_all(db_session) return await GamesCRUD.get_all(db_session)
@games_router.get("", response_model=list[Game])
async def get_games(db_session: AsyncSession = Depends(Database.get_session)):
return await GamesCRUD.get_all(db_session)
@games_router.post("", response_model=Game) @games_router.post("", response_model=Game)
async def add_game(game: GameCreate, async def add_game(game: GameCreate,
user: User = Depends(get_user), user: User = Depends(get_user),

View File

@@ -13,16 +13,11 @@ async def get_movie(movie_id: int, db_session: AsyncSession = Depends(Database.g
return await MoviesCRUD.get(db_session, movie_id) return await MoviesCRUD.get(db_session, movie_id)
@movies_router.get("/cards", response_model=list[MovieCard]) @movies_router.get("", response_model=list[MovieCard])
async def get_movies_cards(db_session: AsyncSession = Depends(Database.get_session)): async def get_movies_cards(db_session: AsyncSession = Depends(Database.get_session)):
return await MoviesCRUD.get_all(db_session) return await MoviesCRUD.get_all(db_session)
@movies_router.get("", response_model=list[Movie])
async def get_movies(db_session: AsyncSession = Depends(Database.get_session)):
return await MoviesCRUD.get_all(db_session)
@movies_router.post("", response_model=Movie) @movies_router.post("", response_model=Movie)
async def add_movie(movie: MovieCreate, async def add_movie(movie: MovieCreate,
user: User = Depends(get_user), user: User = Depends(get_user),