44from sqlalchemy import desc , update
55from sqlalchemy .future import select
66
7- from api .models import CodeJam , CodeJamResponse
87from api .models .orm import Jam , Team , User
8+ from api .models .schemas .old import jam
99from api .settings import DBSession
1010
1111router = APIRouter (prefix = "/codejams" , tags = ["codejams" ])
1212
1313
1414@router .get ("/" )
15- async def get_codejams (session : DBSession ) -> list [CodeJamResponse ]:
15+ async def get_codejams (session : DBSession ) -> list [jam . CodeJam ]:
1616 """Get all the codejams stored in the database."""
17- codejams = await session .execute (select (Jam ).order_by (desc (Jam .id )))
17+ codejams = await session .execute (select (Jam ).order_by (desc (Jam .jam_id )))
1818 codejams .unique ()
1919
2020 return codejams .scalars ().all ()
@@ -24,7 +24,7 @@ async def get_codejams(session: DBSession) -> list[CodeJamResponse]:
2424 "/{codejam_id}" ,
2525 responses = {404 : {"description" : "CodeJam could not be found or there is no ongoing code jam." }},
2626)
27- async def get_codejam (codejam_id : int , session : DBSession ) -> CodeJamResponse :
27+ async def get_codejam (codejam_id : int , session : DBSession ) -> jam . CodeJam :
2828 """
2929 Get a specific codejam stored in the database by ID.
3030
@@ -39,7 +39,7 @@ async def get_codejam(codejam_id: int, session: DBSession) -> CodeJamResponse:
3939 # With the current implementation, there should only be one ongoing codejam.
4040 return ongoing_jams [0 ]
4141
42- jam_result = await session .execute (select (Jam ).where (Jam .id == codejam_id ))
42+ jam_result = await session .execute (select (Jam ).where (Jam .jam_id == codejam_id ))
4343 jam_result .unique ()
4444
4545 if not (jam := jam_result .scalars ().one_or_none ()):
@@ -54,23 +54,23 @@ async def modify_codejam(
5454 session : DBSession ,
5555 name : Optional [str ] = None ,
5656 ongoing : Optional [bool ] = None ,
57- ) -> CodeJamResponse :
57+ ) -> jam . CodeJam :
5858 """Modify the specified codejam to change its name and/or whether it's the ongoing code jam."""
59- codejam = await session .execute (select (Jam ).where (Jam .id == codejam_id ))
59+ codejam = await session .execute (select (Jam ).where (Jam .jam_id == codejam_id ))
6060 codejam .unique ()
6161
6262 if not codejam .scalars ().one_or_none ():
6363 raise HTTPException (status_code = 404 , detail = "Code Jam with specified ID does not exist." )
6464
6565 if name is not None :
66- await session .execute (update (Jam ).where (Jam .id == codejam_id ).values (name = name ))
66+ await session .execute (update (Jam ).where (Jam .jam_id == codejam_id ).values (name = name ))
6767
6868 if ongoing is not None :
6969 # Make sure no other Jams are ongoing, and set the specified codejam to ongoing.
7070 await session .execute (update (Jam ).where (Jam .ongoing == True ).values (ongoing = False ))
71- await session .execute (update (Jam ).where (Jam .id == codejam_id ).values (ongoing = True ))
71+ await session .execute (update (Jam ).where (Jam .jam_id == codejam_id ).values (ongoing = True ))
7272
73- jam_result = await session .execute (select (Jam ).where (Jam .id == codejam_id ))
73+ jam_result = await session .execute (select (Jam ).where (Jam .jam_id == codejam_id ))
7474 jam_result .unique ()
7575
7676 jam = jam_result .scalars ().one ()
@@ -79,7 +79,7 @@ async def modify_codejam(
7979
8080
8181@router .post ("/" )
82- async def create_codejam (codejam : CodeJam , session : DBSession ) -> CodeJamResponse :
82+ async def create_codejam (codejam : jam . CodeJamCreate , session : DBSession ) -> jam . CodeJam :
8383 """
8484 Create a new codejam and get back the one just created.
8585
@@ -94,34 +94,37 @@ async def create_codejam(codejam: CodeJam, session: DBSession) -> CodeJamRespons
9494 await session .flush ()
9595
9696 for raw_team in codejam .teams :
97- team = Team (
98- jam_id = jam .id ,
99- name = raw_team .name ,
100- discord_role_id = raw_team .discord_role_id ,
101- discord_channel_id = raw_team .discord_channel_id ,
102- )
103- session .add (team )
104- # Flush here to receive team ID
105- await session .flush ()
106-
97+ created_users = []
10798 for raw_user in raw_team .users :
99+ if raw_user .is_leader :
100+ team_leader_id = raw_user .user_id
108101 if (
109- not (await session .execute (select (User ).where (User .id == raw_user .user_id )))
102+ not (await session .execute (select (User ).where (User .user_id == raw_user .user_id )))
110103 .unique ()
111104 .scalars ()
112105 .one_or_none ()
113106 ):
114107 user = User (id = raw_user .user_id )
108+ created_users .append (user )
115109 session .add (user )
116110
117- team_user = TeamUser (team_id = team .id , user_id = raw_user .user_id , is_leader = raw_user .is_leader )
118- session .add (team_user )
111+ team = Team (
112+ jam_id = jam .jam_id ,
113+ name = raw_team .name ,
114+ discord_role_id = raw_team .discord_role_id ,
115+ discord_channel_id = raw_team .discord_channel_id ,
116+ team_leader_id = team_leader_id ,
117+ )
118+ team .users = created_users
119+ session .add (team )
120+ # Flush here to receive team ID
121+ await session .flush ()
119122
120123 await session .flush ()
121124
122125 # Pydantic, what is synchronous, may attempt to call async methods if current jam
123126 # object is returned. To avoid this, fetch all data here, in async context.
124- jam_result = await session .execute (select (Jam ).where (Jam .id == jam .id ))
127+ jam_result = await session .execute (select (Jam ).where (Jam .jam_id == jam .jam_id ))
125128 jam_result .unique ()
126129
127130 jam = jam_result .scalars ().one ()
0 commit comments