1
|
|
|
""" |
2
|
|
|
byceps.services.tourney.tourney_service |
3
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
:Copyright: 2006-2018 Jochen Kupperschmidt |
6
|
|
|
:License: Modified BSD, see LICENSE for details. |
7
|
|
|
""" |
8
|
|
|
|
9
|
1 |
|
from typing import List, Optional |
10
|
|
|
|
11
|
1 |
|
from ...database import db |
12
|
1 |
|
from ...typing import PartyID, UserID |
13
|
|
|
|
14
|
1 |
|
from .models.participant import Participant as DbParticipant |
15
|
1 |
|
from .models.tourney import Tourney as DbTourney |
16
|
1 |
|
from .models.tourney_category import TourneyCategory as DbTourneyCategory |
17
|
1 |
|
from .transfer.models import Tourney |
18
|
|
|
|
19
|
|
|
|
20
|
1 |
|
def find_tourney(tourney_id: int) -> Optional[Tourney]: |
21
|
|
|
"""Return the tourney with that id, or `None` if not found.""" |
22
|
|
|
tourney = DbTourney.query.get(tourney_id) |
23
|
|
|
|
24
|
|
|
if tourney is None: |
25
|
|
|
return None |
26
|
|
|
|
27
|
|
|
return _db_entity_to_tourney(tourney) |
28
|
|
|
|
29
|
|
|
|
30
|
1 |
|
def get_tourneys_for_party(party_id: PartyID) -> List[Tourney]: |
31
|
|
|
"""Return the tourneys for that party.""" |
32
|
|
|
rows = db.session \ |
33
|
|
|
.query(DbTourney, db.func.count(DbParticipant.id)) \ |
34
|
|
|
.join(DbTourneyCategory) \ |
35
|
|
|
.join(DbParticipant, isouter=True) \ |
36
|
|
|
.filter(DbTourneyCategory.party_id == party_id) \ |
37
|
|
|
.group_by(DbTourney.id) \ |
38
|
|
|
.all() |
39
|
|
|
|
40
|
|
|
return [_db_entity_to_tourney(row[0], row[1]) for row in rows] |
41
|
|
|
|
42
|
|
|
|
43
|
1 |
|
def _db_entity_to_tourney( |
44
|
|
|
tourney: DbTourney, current_participant_count: int = -1 |
45
|
|
|
) -> Tourney: |
46
|
|
|
return Tourney( |
47
|
|
|
tourney.id, |
48
|
|
|
tourney.category_id, |
49
|
|
|
tourney.title, |
50
|
|
|
tourney.subtitle, |
51
|
|
|
tourney.logo_url, |
52
|
|
|
current_participant_count, |
53
|
|
|
tourney.max_participant_count, |
54
|
|
|
tourney.starts_at, |
55
|
|
|
) |
56
|
|
|
|