Passed
Branch main (854eb5)
by Jochen
04:24
created

byceps.services.board.category_query_service   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Test Coverage

Coverage 89.66%

Importance

Changes 0
Metric Value
eloc 76
dl 0
loc 125
ccs 26
cts 29
cp 0.8966
rs 10
c 0
b 0
f 0
wmc 10

8 Functions

Rating   Name   Duplication   Size   Complexity  
A _db_entity_to_category() 0 11 1
A find_category_by_id() 0 8 2
A get_categories_excluding() 0 11 1
A count_categories_for_board() 0 5 1
A find_category_by_slug() 0 11 2
A get_categories() 0 8 1
A _db_entity_to_category_with_last_update() 0 15 1
A get_categories_with_last_updates() 0 18 1
1
"""
2
byceps.services.board.category_query_service
3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5
:Copyright: 2006-2021 Jochen Kupperschmidt
6
:License: Revised BSD (see `LICENSE` file for details)
7
"""
8
9 1
from typing import Optional, Sequence
10
11 1
from ...database import db
12
13 1
from .dbmodels.category import Category as DbCategory
14 1
from .transfer.models import (
15
    BoardID,
16
    Category,
17
    CategoryID,
18
    CategoryWithLastUpdate,
19
)
20
21
22 1
def count_categories_for_board(board_id: BoardID) -> int:
23
    """Return the number of categories for that board."""
24 1
    return DbCategory.query \
25
        .for_board(board_id) \
26
        .count()
27
28
29 1
def find_category_by_id(category_id: CategoryID) -> Optional[Category]:
30
    """Return the category with that id, or `None` if not found."""
31 1
    category = db.session.query(DbCategory).get(category_id)
32
33 1
    if category is None:
34
        return None
35
36 1
    return _db_entity_to_category(category)
37
38
39 1
def find_category_by_slug(board_id: BoardID, slug: str) -> Optional[Category]:
40
    """Return the category for that board and slug, or `None` if not found."""
41 1
    category = DbCategory.query \
42
        .for_board(board_id) \
43
        .filter_by(slug=slug) \
44
        .first()
45
46 1
    if category is None:
47 1
        return None
48
49 1
    return _db_entity_to_category(category)
50
51
52 1
def get_categories(board_id: BoardID) -> Sequence[Category]:
53
    """Return all categories for that board, ordered by position."""
54 1
    categories = DbCategory.query \
55
        .for_board(board_id) \
56
        .order_by(DbCategory.position) \
57
        .all()
58
59 1
    return [_db_entity_to_category(category) for category in categories]
60
61
62 1
def get_categories_excluding(
63
    board_id: BoardID, category_id: CategoryID
64
) -> Sequence[Category]:
65
    """Return all categories for that board except for the specified one."""
66
    categories = DbCategory.query \
67
        .for_board(board_id) \
68
        .filter(DbCategory.id != category_id) \
69
        .order_by(DbCategory.position) \
70
        .all()
71
72
    return [_db_entity_to_category(category) for category in categories]
73
74
75 1
def get_categories_with_last_updates(
76
    board_id: BoardID,
77
) -> Sequence[CategoryWithLastUpdate]:
78
    """Return the categories for that board.
79
80
    Include the creator of the last posting in each category.
81
    """
82 1
    categories_with_last_update = DbCategory.query \
83
        .for_board(board_id) \
84
        .filter_by(hidden=False) \
85
        .options(
86
            db.joinedload(DbCategory.last_posting_updated_by),
87
        ) \
88
        .all()
89
90 1
    return [
91
        _db_entity_to_category_with_last_update(category)
92
        for category in categories_with_last_update
93
    ]
94
95
96 1
def _db_entity_to_category(category: DbCategory) -> Category:
97 1
    return Category(
98
        category.id,
99
        category.board_id,
100
        category.position,
101
        category.slug,
102
        category.title,
103
        category.description,
104
        category.topic_count,
105
        category.posting_count,
106
        category.hidden,
107
    )
108
109
110 1
def _db_entity_to_category_with_last_update(
111
    category: DbCategory,
112
) -> CategoryWithLastUpdate:
113 1
    return CategoryWithLastUpdate(
114
        category.id,
115
        category.board_id,
116
        category.position,
117
        category.slug,
118
        category.title,
119
        category.description,
120
        category.topic_count,
121
        category.posting_count,
122
        category.hidden,
123
        category.last_posting_updated_at,
124
        category.last_posting_updated_by,
125
    )
126