Skip to content

Commit

Permalink
refactor use monument id instead object id
Browse files Browse the repository at this point in the history
  • Loading branch information
p3t3r67x0 committed Feb 2, 2025
1 parent 7080e93 commit 025663d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
8 changes: 4 additions & 4 deletions app/api/monument.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from typing import List

from ..dependencies import get_session
from ..services.monument import get_monument_by_object_id, get_monument_geometries_by_bbox
from ..services.monument import get_monument_by_id, get_monument_geometries_by_bbox


route_monument = APIRouter(prefix='/monument/v1')
Expand All @@ -19,12 +19,12 @@
response_model=List,
tags=['Denkmalliste']
)
async def fetch_monument_by_object_id(object_id: int, session: AsyncSession = Depends(get_session)):
rows = await get_monument_by_object_id(session, object_id)
async def fetch_monument_by_monument_id(monument_id: int, session: AsyncSession = Depends(get_session)):
rows = await get_monument_by_id(session, monument_id)
response = jsonable_encoder(rows)

if len(response) == 0:
raise HTTPException(status_code=404, detail=f'No matches found for object id {object_id}')
raise HTTPException(status_code=404, detail=f'No matches found for monument id {monument_id}')

return JSONResponse(content=response)

Expand Down
28 changes: 23 additions & 5 deletions app/services/monument.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ async def get_monument_geometries_by_bbox(session: AsyncSession, xmin: float, ym



async def get_monument_by_object_id(session: AsyncSession, object_id: int):
async def get_monument_by_id(session: AsyncSession, monument_id: int):
try:
validated_object_id = validate_positive_int32(object_id, 'query', 'object_id')
validated_monument_id = validate_positive_int32(monument_id, 'query', 'monument_id')
except ValueError as e:
raise HTTPException(status_code=422, detail=str(e))

Expand All @@ -42,6 +42,19 @@ async def get_monument_by_object_id(session: AsyncSession, object_id: int):
mxr.reason_id = mr.id
GROUP BY
mxr.monument_id
),
monument_scopes AS (
SELECT
mxs.monument_id,
string_agg(ms.label, ', ') AS scope_labels
FROM
sh_monument_x_scope AS mxs
JOIN
sh_monument_scope AS ms
ON
mxs.scope_id = ms.id
GROUP BY
mxs.monument_id
)
SELECT
ST_AsGeoJSON(m.wkb_geometry, 15)::jsonb AS geojson,
Expand All @@ -54,18 +67,23 @@ async def get_monument_by_object_id(session: AsyncSession, object_id: int):
m.designation,
m.description,
m.monument_type,
r.reason_labels AS monument_reason
r.reason_labels AS monument_reason,
s.scope_labels AS monument_scope
FROM
sh_monument AS m
LEFT JOIN
monument_reasons AS r
ON
m.id = r.monument_id
LEFT JOIN
monument_scopes AS s
ON
m.id = s.monument_id
WHERE
m.id = :q
m.id = :monument_id
''')

sql = stmt.bindparams(q=validated_object_id)
sql = stmt.bindparams(monument_id=validated_monument_id)
result = await session.execute(sql)
rows = result.mappings().all()

Expand Down

0 comments on commit 025663d

Please sign in to comment.