11from datetime import date
22from typing import Any , Sequence
33
4- from sqlalchemy import select , RowMapping , and_
4+ from sqlalchemy import select , RowMapping , and_ , Select
55from sqlalchemy .ext .asyncio import AsyncSession
66from sqlalchemy .orm import selectinload
77
8+ from src .api .endpoints .data_source ._shared .build import build_data_source_get_query
9+ from src .api .endpoints .data_source ._shared .process import process_data_source_get_mapping
810from src .api .endpoints .data_source .get .response import DataSourceGetOuterResponse , DataSourceGetResponse
911from src .core .enums import RecordType
1012from src .db .models .impl .flag .url_validated .enums import URLType
1820from src .db .queries .base .builder import QueryBuilderBase
1921
2022
21- class GetDataSourceQueryBuilder (QueryBuilderBase ):
23+ class GetDataSourcesQueryBuilder (QueryBuilderBase ):
2224
2325 def __init__ (
2426 self ,
@@ -28,59 +30,9 @@ def __init__(
2830 self .page = page
2931
3032 async def run (self , session : AsyncSession ) -> DataSourceGetOuterResponse :
33+ query : Select = build_data_source_get_query ()
3134 query = (
32- select (
33- URL ,
34- URL .id ,
35- URL .url ,
36-
37- # Required Attributes
38- URL .name ,
39- URLRecordType .record_type ,
40-
41- # Optional Attributes
42- URL .description ,
43- LinkBatchURL .batch_id ,
44- URLOptionalDataSourceMetadata .record_formats ,
45- URLOptionalDataSourceMetadata .data_portal_type ,
46- URLOptionalDataSourceMetadata .supplying_entity ,
47- URLOptionalDataSourceMetadata .coverage_start ,
48- URLOptionalDataSourceMetadata .coverage_end ,
49- URLOptionalDataSourceMetadata .agency_supplied ,
50- URLOptionalDataSourceMetadata .agency_aggregation ,
51- URLOptionalDataSourceMetadata .agency_described_not_in_database ,
52- URLOptionalDataSourceMetadata .agency_originated ,
53- URLOptionalDataSourceMetadata .update_method ,
54- URLOptionalDataSourceMetadata .readme_url ,
55- URLOptionalDataSourceMetadata .originating_entity ,
56- URLOptionalDataSourceMetadata .retention_schedule ,
57- URLOptionalDataSourceMetadata .scraper_url ,
58- URLOptionalDataSourceMetadata .submission_notes ,
59- URLOptionalDataSourceMetadata .access_notes ,
60- URLOptionalDataSourceMetadata .access_types
61- )
62- .join (
63- URLRecordType ,
64- URLRecordType .url_id == URL .id
65- )
66- .join (
67- FlagURLValidated ,
68- and_ (
69- FlagURLValidated .url_id == URL .id ,
70- FlagURLValidated .type == URLType .DATA_SOURCE
71- )
72- )
73- .outerjoin (
74- LinkBatchURL ,
75- LinkBatchURL .url_id == URL .id
76- )
77- .outerjoin (
78- URLOptionalDataSourceMetadata ,
79- URLOptionalDataSourceMetadata .url_id == URL .id
80- )
81- .options (
82- selectinload (URL .confirmed_agencies ),
83- )
35+ query
8436 .limit (100 )
8537 .offset ((self .page - 1 ) * 100 )
8638 )
@@ -89,64 +41,8 @@ async def run(self, session: AsyncSession) -> DataSourceGetOuterResponse:
8941 responses : list [DataSourceGetResponse ] = []
9042
9143 for mapping in mappings :
92- url : URL = mapping [URL ]
93- url_id : int = mapping [URL .id ]
94- url_url : str = mapping [URL .url ]
95- url_name : str = mapping [URL .name ]
96- url_record_type : RecordType = mapping [URLRecordType .record_type ]
97-
98- url_agency_ids : list [int ] = []
99- for agency in url .confirmed_agencies :
100- url_agency_ids .append (agency .id )
101-
102- url_description : str | None = mapping [URL .description ]
103- link_batch_url_batch_id : int | None = mapping [LinkBatchURL .batch_id ]
104- url_record_formats : list [str ] = mapping [URLOptionalDataSourceMetadata .record_formats ] or []
105- url_data_portal_type : str | None = mapping [URLOptionalDataSourceMetadata .data_portal_type ]
106- url_supplying_entity : str | None = mapping [URLOptionalDataSourceMetadata .supplying_entity ]
107- url_coverage_start : date | None = mapping [URLOptionalDataSourceMetadata .coverage_start ]
108- url_coverage_end : date | None = mapping [URLOptionalDataSourceMetadata .coverage_end ]
109- url_agency_supplied : bool | None = mapping [URLOptionalDataSourceMetadata .agency_supplied ]
110- url_agency_aggregation : AgencyAggregationEnum | None = mapping [URLOptionalDataSourceMetadata .agency_aggregation ]
111- url_agency_originated : bool | None = mapping [URLOptionalDataSourceMetadata .agency_originated ]
112- url_agency_described_not_in_database : bool | None = mapping [URLOptionalDataSourceMetadata .agency_described_not_in_database ]
113- url_update_method : UpdateMethodEnum | None = mapping [URLOptionalDataSourceMetadata .update_method ]
114- url_readme_url : str | None = mapping [URLOptionalDataSourceMetadata .readme_url ]
115- url_originating_entity : str | None = mapping [URLOptionalDataSourceMetadata .originating_entity ]
116- url_retention_schedule : RetentionScheduleEnum | None = mapping [URLOptionalDataSourceMetadata .retention_schedule ]
117- url_scraper_url : str | None = mapping [URLOptionalDataSourceMetadata .scraper_url ]
118- url_submission_notes : str | None = mapping [URLOptionalDataSourceMetadata .submission_notes ]
119- url_access_notes : str | None = mapping [URLOptionalDataSourceMetadata .access_notes ]
120- url_access_types : list [AccessTypeEnum ] = mapping [URLOptionalDataSourceMetadata .access_types ] or []
121-
122- responses .append (
123- DataSourceGetResponse (
124- url_id = url_id ,
125- url = url_url ,
126- name = url_name ,
127- record_type = url_record_type ,
128- agency_ids = url_agency_ids ,
129- description = url_description ,
130- batch_id = link_batch_url_batch_id ,
131- record_formats = url_record_formats ,
132- data_portal_type = url_data_portal_type ,
133- supplying_entity = url_supplying_entity ,
134- coverage_start = url_coverage_start ,
135- coverage_end = url_coverage_end ,
136- agency_supplied = url_agency_supplied ,
137- agency_aggregation = url_agency_aggregation ,
138- agency_originated = url_agency_originated ,
139- agency_described_not_in_database = url_agency_described_not_in_database ,
140- update_method = url_update_method ,
141- readme_url = url_readme_url ,
142- originating_entity = url_originating_entity ,
143- retention_schedule = url_retention_schedule ,
144- scraper_url = url_scraper_url ,
145- submission_notes = url_submission_notes ,
146- access_notes = url_access_notes ,
147- access_types = url_access_types
148- )
149- )
44+ response : DataSourceGetResponse = process_data_source_get_mapping (mapping )
45+ responses .append (response )
15046
15147 return DataSourceGetOuterResponse (
15248 results = responses ,
0 commit comments