|
1 | 1 | from sqlalchemy.sql import text |
2 | 2 | from sqlalchemy.ext.asyncio import AsyncSession |
| 3 | +from fastapi import HTTPException |
| 4 | + |
| 5 | +from ..utils.validators import validate_not_none |
| 6 | +from ..utils.sanitizer import sanitize_string |
3 | 7 |
|
4 | 8 |
|
5 | 9 |
|
@@ -35,6 +39,12 @@ async def get_parcel_meta_by_lat_lng(session: AsyncSession, lat: float, lng: flo |
35 | 39 |
|
36 | 40 |
|
37 | 41 | async def get_municipality_by_key(session: AsyncSession, key: str): |
| 42 | + try: |
| 43 | + validated_key = validate_not_none(key) |
| 44 | + validated_key = sanitize_string(validated_key) |
| 45 | + except ValueError as e: |
| 46 | + raise HTTPException(status_code=400, detail=str(e)) |
| 47 | + |
38 | 48 | stmt = text(''' |
39 | 49 | SELECT |
40 | 50 | mk.municipality_key AS municipality_key, |
@@ -68,7 +78,7 @@ async def get_municipality_by_key(session: AsyncSession, key: str): |
68 | 78 | LOWER(mk.municipality_key) = :key |
69 | 79 | ''') |
70 | 80 |
|
71 | | - sql = stmt.bindparams(key=key.lower()) |
| 81 | + sql = stmt.bindparams(key=validated_key.lower()) |
72 | 82 | result = await session.execute(sql) |
73 | 83 |
|
74 | 84 | return result.mappings().all() |
@@ -109,6 +119,12 @@ async def get_municipality_by_name(session: AsyncSession, name: str): |
109 | 119 |
|
110 | 120 |
|
111 | 121 | async def get_municipality_by_query(session: AsyncSession, query: str): |
| 122 | + try: |
| 123 | + validated_query = validate_not_none(query) |
| 124 | + sanitized_query = sanitize_string(validated_query) |
| 125 | + except ValueError as e: |
| 126 | + raise HTTPException(status_code=400, detail=str(e)) |
| 127 | + |
112 | 128 | stmt = text(''' |
113 | 129 | SELECT |
114 | 130 | gem.ags AS municipality_key, |
@@ -145,7 +161,7 @@ async def get_municipality_by_query(session: AsyncSession, query: str): |
145 | 161 | LIMIT 10 |
146 | 162 | ''') |
147 | 163 |
|
148 | | - sql = stmt.bindparams(q=query.lower()) |
| 164 | + sql = stmt.bindparams(q=sanitized_query.lower()) |
149 | 165 | result = await session.execute(sql) |
150 | 166 | rows = result.mappings().all() |
151 | 167 |
|
|
0 commit comments