Completed
Push — main ( 64aa50...375f7e )
by Jochen
76:12
created

get_tourneys_for_party()   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1.2963

Importance

Changes 0
Metric Value
cc 1
eloc 9
nop 1
dl 0
loc 11
ccs 1
cts 3
cp 0.3333
crap 1.2963
rs 9.95
c 0
b 0
f 0
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