55from sqlalchemy .orm import selectinload
66
77from ..db import get_db_session
8+ from ..redis import r
89from .models import City , Song , Tag
910from .schemas import CityCreate , CityRead , SongCreate , SongRead , TagCreate , TagRead
1011
1415
1516
1617@router .get ("/songs" , response_model = list [SongRead ])
17- async def get_songs (
18- session : AsyncSession = Depends (get_db_session )):
19- result = await session .scalars (select (Song ).options (selectinload (Song .tags ), selectinload (Song .city )))
18+ async def get_songs (session : AsyncSession = Depends (get_db_session )):
19+ result = await session .scalars (
20+ select (Song ).options (selectinload (Song .tags ), selectinload (Song .city ))
21+ )
2022 songs = result .all ()
2123 return songs
2224
@@ -35,7 +37,9 @@ async def add_song(song: SongCreate, session: AsyncSession = Depends(get_db_sess
3537
3638
3739@router .post ("/city" , response_model = CityRead )
38- async def create_city (* , session : AsyncSession = Depends (get_db_session ), city : CityCreate ):
40+ async def create_city (
41+ * , session : AsyncSession = Depends (get_db_session ), city : CityCreate
42+ ):
3943 # new_city = City(**city.dict())
4044 new_city = City (name = city .name )
4145 session .add (new_city )
@@ -46,11 +50,13 @@ async def create_city(*, session: AsyncSession = Depends(get_db_session), city:
4650
4751@router .post ("/connect_city_with_song" )
4852async def connect_city_with_song (
49- city_title : str , song_title : str , session : AsyncSession = Depends (get_db_session )
53+ city_title : str , song_title : str , session : AsyncSession = Depends (get_db_session )
5054):
5155 city_in_db = await session .scalars (select (City ).where (City .name == city_title ))
5256 city = city_in_db .first ()
53- song_in_db = await session .scalars (select (Song ).where (Song .name .like (f"%{ song_title } %" )))
57+ song_in_db = await session .scalars (
58+ select (Song ).where (Song .name .like (f"%{ song_title } %" ))
59+ )
5460 song = song_in_db .first ()
5561 song .city = city
5662 session .add (song )
@@ -60,7 +66,9 @@ async def connect_city_with_song(
6066
6167
6268@router .post ("/tags" , response_model = TagRead )
63- async def create_tag (* , session : AsyncSession = Depends (get_db_session ), tag : TagCreate ):
69+ async def create_tag (
70+ * , session : AsyncSession = Depends (get_db_session ), tag : TagCreate
71+ ):
6472 input_tag = TagCreate .model_validate (tag )
6573 new_tag = Tag (** input_tag .dict ())
6674 session .add (new_tag )
@@ -71,14 +79,17 @@ async def create_tag(*, session: AsyncSession = Depends(get_db_session), tag: Ta
7179
7280@router .post ("/attach-tags" )
7381async def attach_tag_to_song (
74- tag_title : str , song_name : str , session : AsyncSession = Depends (get_db_session )
82+ tag_title : str , song_name : str , session : AsyncSession = Depends (get_db_session )
7583):
7684 tag_in_db = await session .scalars (
7785 select (Tag ).where (Tag .title .like (f"%{ tag_title } %" ))
7886 )
7987 tag = tag_in_db .first ()
8088 song_in_db = await session .scalars (
81- select (Song ).options (selectinload (Song .tags )).where (Song .name .like (f"%{ song_name } %" )))
89+ select (Song )
90+ .options (selectinload (Song .tags ))
91+ .where (Song .name .like (f"%{ song_name } %" ))
92+ )
8293 song = song_in_db .first ()
8394 song .tags .append (tag )
8495 session .add (tag )
0 commit comments