File size: 2,069 Bytes
55d0d9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from sqlalchemy import select, cast, Integer, func
from sqlalchemy.ext.asyncio import AsyncSession

from app.models.models import CommitteeDesignation, CodesTable, Definitions
import logging


class CommitteeDesignationService:
    def __init__(self):
        self.logger = logging.getLogger(__name__)

    async def get_committee_designations(self, session: AsyncSession):
        query = select(CommitteeDesignation).order_by(CommitteeDesignation.letter_tag.asc())
        result = await session.execute(query)
        return result.scalars().all()

    async def get_committee_designations_by_code(self, session: AsyncSession, code: str):
        query = (
            select(CommitteeDesignation)
            .join(CodesTable, CommitteeDesignation.letter_tag == CodesTable.letter_tag)
            .where(CodesTable.code == code)
            .order_by(CommitteeDesignation.letter_tag.asc())
        )
        result = await session.execute(query)
        return result.scalars().all()

    async def get_codes_by_designation(self, session: AsyncSession, designation: str, limit: int):
        query = (
            select(CodesTable)
            .join(CommitteeDesignation, CodesTable.letter_tag == CommitteeDesignation.letter_tag)
            .where(func.lower(CommitteeDesignation.letter_tag) == func.lower(designation))
            .order_by(CodesTable.sort_index.asc())
            .limit(limit)
        )
        result = await session.execute(query)
        return result.scalars().all()
        

    async def get_definitions_by_designation(self, session: AsyncSession, designation: str, limit: int):
        query = (
            select(Definitions)
            .join(CommitteeDesignation, Definitions.letter_tag == CommitteeDesignation.letter_tag)
            .where(func.lower(CommitteeDesignation.letter_tag) == func.lower(designation))
            .order_by(Definitions.term.asc())
            .limit(limit)
        )
        result = await session.execute(query)
        return result.all()
committee_designation_service = CommitteeDesignationService()