Skip to content

Commit

Permalink
feat: set league group when creating an event
Browse files Browse the repository at this point in the history
  • Loading branch information
brownben committed Apr 12, 2024
1 parent 904db31 commit 86aba66
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 18 deletions.
3 changes: 2 additions & 1 deletion backend/src/database/leagues.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,8 @@ async def update(league: str, name: str, group: LeagueGroup) -> bool:
return False

for key, value in group.dict().items():
setattr(existing_group, key, value)
if key != "id":
setattr(existing_group, key, value)

await existing_group.save().run()

Expand Down
6 changes: 3 additions & 3 deletions backend/src/routes/tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ async def test_create_event(self) -> None:
"allow_user_submitted_results": False,
"league": "Sprintelope 2021",
"compulsory": False,
"league_group": "",
"league_group": None,
"overridden_scoring_method": "",
"expected_courses": {},
},
Expand Down Expand Up @@ -415,7 +415,7 @@ def test_create_event_already_exists(self) -> None:
"allow_user_submitted_results": False,
"league": "Sprintelope 2021",
"compulsory": False,
"league_group": "",
"league_group": None,
"overridden_scoring_method": "",
"expected_courses": {},
},
Expand Down Expand Up @@ -444,7 +444,7 @@ async def test_create_event_unknown_league(self) -> None:
"allow_user_submitted_results": False,
"league": "Unknown League",
"compulsory": False,
"league_group": "Edinburgh Summer 2021",
"league_group": 2,
"overridden_scoring_method": "",
"expected_courses": {},
},
Expand Down
14 changes: 9 additions & 5 deletions backend/src/routes/tests/test_leagues.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,18 +932,21 @@ async def test_update_league_groups(self) -> None:
"/leagues/Sprintelope 2021/groups",
headers={"Authorization": "Bearer SuperSecretTest"},
json={
"name": "Test",
"name": "Testing 123",
"league": "Sprintelope 2021",
"min": 0,
"max": 0,
},
)
group = await LeagueGroups.get_by_name("Sprintelope 2021", "Testing 123")
assert group is not None
self.assertEqual(group.max, 0)

response = self.client.put(
"/leagues/Sprintelope 2021/groups/Test",
"/leagues/Sprintelope 2021/groups/Testing 123",
headers={"Authorization": "Bearer SuperSecretTest"},
json={
"name": "Test",
"name": "Testing 123",
"league": "Sprintelope 2021",
"min": 0,
"max": 2,
Expand All @@ -953,10 +956,10 @@ async def test_update_league_groups(self) -> None:
self.assertEqual(response.status_code, 200)
self.assertDictEqual(
response.json(),
{"detail": "League group `Test` updated"},
{"detail": "League group `Testing 123` updated"},
)

group = await LeagueGroups.get_by_name("Sprintelope 2021", "Test")
group = await LeagueGroups.get_by_name("Sprintelope 2021", "Testing 123")
assert group is not None
self.assertEqual(group.max, 2)

Expand Down Expand Up @@ -1004,6 +1007,7 @@ def test_get_league_group(self) -> None:
self.assertEqual(
response.json(),
{
"id": 1,
"name": "Test",
"league": "Sprintelope 2021",
"min": 0,
Expand Down
3 changes: 2 additions & 1 deletion backend/src/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class League(BaseModel):


class LeagueGroup(BaseModel):
id: Optional[int] = None
name: str
league: str

Expand Down Expand Up @@ -67,7 +68,7 @@ class EventCreationRequest(BaseModel):
allow_user_submitted_results: bool = False
league: str
compulsory: bool = False
league_group: Optional[str] = None
league_group: Optional[int] = None
overridden_scoring_method: Optional[str] = None
expected_courses: Optional[dict[str, str]]

Expand Down
1 change: 1 addition & 0 deletions frontend/api-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface LeagueClass {
}

export interface LeagueGroup {
id: number
name: string
league: string

Expand Down
36 changes: 28 additions & 8 deletions frontend/pages/events/create.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
IsValidURL,
RequiredField,
} from '~/utils/validation'
import type { League } from '~/api-types'
import type { League, LeagueGroup } from '~/api-types'
requireLogin()
Expand All @@ -30,6 +30,18 @@ const form = reactive({
expected_courses: null,
})
const leagueGroups: Ref<LeagueGroup[]> = ref([])
const getLeague = async () => {
try {
if (form.league)
leagueGroups.value = await useGet(`leagues/${form.league}/groups`)
else leagueGroups.value = []
} catch {
leagueGroups.value = []
}
}
watch(() => form.name, getLeague)
const action = async () => {
try {
await usePost(`events/`, toRaw(form))
Expand Down Expand Up @@ -104,6 +116,21 @@ useTitle({
title="Scoring"
description="Adjust how the event is scored in the league"
/>
<InputDropdown
v-if="leagueGroups.length > 0"
v-model="form.league_group"
:list="leagueGroups.map((g) => ({ value: String(g.id), text: g.name }))"
label="League Group:"
include-blank
class="col-span-2"
/>
<InputDropdown
v-model="form.overridden_scoring_method"
:list="scoringOptions"
label="Override Scoring Method:"
include-blank
class="col-span-2"
/>
<InputSwitch
v-model="form.compulsory"
label="Event always included in total points?"
Expand All @@ -116,13 +143,6 @@ useTitle({
description="Let users add their times to the results"
class="col-span-2 py-2"
/>
<InputDropdown
v-model="form.overridden_scoring_method"
:list="scoringOptions"
label="Override Scoring Method:"
include-blank
class="col-span-2"
/>
</Form>
</div>
</template>

0 comments on commit 86aba66

Please sign in to comment.