4
4
from sqlalchemy import desc , update
5
5
from sqlalchemy .future import select
6
6
7
- from api .models import CodeJam , CodeJamResponse
8
7
from api .models .orm import Jam , Team , User
8
+ from api .models .schemas .v1 import jam
9
9
from api .settings import DBSession
10
10
11
11
router = APIRouter (prefix = "/codejams" , tags = ["codejams" ])
12
12
13
13
14
14
@router .get ("/" )
15
- async def get_codejams (session : DBSession ) -> list [CodeJamResponse ]:
15
+ async def get_codejams (session : DBSession ) -> list [jam . CodeJam ]:
16
16
"""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 )))
18
18
codejams .unique ()
19
19
20
20
return codejams .scalars ().all ()
@@ -24,7 +24,7 @@ async def get_codejams(session: DBSession) -> list[CodeJamResponse]:
24
24
"/{codejam_id}" ,
25
25
responses = {404 : {"description" : "CodeJam could not be found or there is no ongoing code jam." }},
26
26
)
27
- async def get_codejam (codejam_id : int , session : DBSession ) -> CodeJamResponse :
27
+ async def get_codejam (codejam_id : int , session : DBSession ) -> jam . CodeJam :
28
28
"""
29
29
Get a specific codejam stored in the database by ID.
30
30
@@ -39,7 +39,7 @@ async def get_codejam(codejam_id: int, session: DBSession) -> CodeJamResponse:
39
39
# With the current implementation, there should only be one ongoing codejam.
40
40
return ongoing_jams [0 ]
41
41
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 ))
43
43
jam_result .unique ()
44
44
45
45
if not (jam := jam_result .scalars ().one_or_none ()):
@@ -54,23 +54,23 @@ async def modify_codejam(
54
54
session : DBSession ,
55
55
name : Optional [str ] = None ,
56
56
ongoing : Optional [bool ] = None ,
57
- ) -> CodeJamResponse :
57
+ ) -> jam . CodeJam :
58
58
"""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 ))
60
60
codejam .unique ()
61
61
62
62
if not codejam .scalars ().one_or_none ():
63
63
raise HTTPException (status_code = 404 , detail = "Code Jam with specified ID does not exist." )
64
64
65
65
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 ))
67
67
68
68
if ongoing is not None :
69
69
# Make sure no other Jams are ongoing, and set the specified codejam to ongoing.
70
70
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 ))
72
72
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 ))
74
74
jam_result .unique ()
75
75
76
76
jam = jam_result .scalars ().one ()
@@ -79,7 +79,7 @@ async def modify_codejam(
79
79
80
80
81
81
@router .post ("/" )
82
- async def create_codejam (codejam : CodeJam , session : DBSession ) -> CodeJamResponse :
82
+ async def create_codejam (codejam : jam . CodeJamCreate , session : DBSession ) -> jam . CodeJam :
83
83
"""
84
84
Create a new codejam and get back the one just created.
85
85
@@ -94,34 +94,37 @@ async def create_codejam(codejam: CodeJam, session: DBSession) -> CodeJamRespons
94
94
await session .flush ()
95
95
96
96
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 = []
107
98
for raw_user in raw_team .users :
99
+ if raw_user .is_leader :
100
+ team_leader_id = raw_user .user_id
108
101
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 )))
110
103
.unique ()
111
104
.scalars ()
112
105
.one_or_none ()
113
106
):
114
107
user = User (id = raw_user .user_id )
108
+ created_users .append (user )
115
109
session .add (user )
116
110
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 ()
119
122
120
123
await session .flush ()
121
124
122
125
# Pydantic, what is synchronous, may attempt to call async methods if current jam
123
126
# 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 ))
125
128
jam_result .unique ()
126
129
127
130
jam = jam_result .scalars ().one ()
0 commit comments