Skip to content

Commit

Permalink
add endpoint to get archaeological monument by id
Browse files Browse the repository at this point in the history
  • Loading branch information
p3t3r67x0 committed Feb 14, 2025
1 parent b47a159 commit 64feb4b
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 6 deletions.
19 changes: 17 additions & 2 deletions app/api/monument.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from typing import List

from ..dependencies import get_session
from ..services.monument import get_monument_by_id, get_monument_by_slug, get_monument_geometries_by_bbox
from ..services.monument import get_monument_by_id, get_monument_by_slug, get_monument_geometries_by_bbox, get_archaeological_monument_by_id
from ..schemas.monument import ArchaeologicalMonumentResponse


route_monument = APIRouter(prefix='/monument/v1')
Expand Down Expand Up @@ -63,8 +64,22 @@ async def fetch_monument_geometries_by_bbox(xmin: float, ymin: float, xmax: floa

for row in rows
]

geojson_data = FeatureCollection(features)
response = jsonable_encoder(geojson_data)

return JSONResponse(content=response)




@route_monument.get(
'/archaeological/detail',
response_model=ArchaeologicalMonumentResponse,
tags=['Denkmalliste']
)
async def fetch_archaeological_monument_by_id(monument_id: int, session: AsyncSession = Depends(get_session)):
rows = await get_archaeological_monument_by_id(session, monument_id)
response = jsonable_encoder(rows)

return JSONResponse(content=response)
25 changes: 23 additions & 2 deletions app/models/monument.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from sqlalchemy import Column, Integer, String
from sqlalchemy import Column, Integer, String, Text, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.sql import func
from geoalchemy2 import Geometry

Base = declarative_base()



class Monument(Base):
__tablename__ = 'sh_monuments'
__tablename__ = 'sh_monument'

id = Column(Integer, primary_key=True)
object_id = Column(String)
Expand All @@ -20,3 +21,23 @@ class Monument(Base):
postal_code = Column(String)
place_name = Column(String)
wkb_geometry = Column(Geometry)


class ArchaeologicalMonument(Base):
__tablename__ = 'sh_archaeological_monument'

id = Column(Integer, primary_key=True, index=True)
object_name = Column(String, nullable=False)
proper_name = Column(String, nullable=True)
object_number = Column(String(50), unique=True, nullable=False)
district_name = Column(String, nullable=True)
municipality_name = Column(String, nullable=True)
object_description = Column(Text, nullable=True)
object_significance = Column(Text, nullable=True)
protection_scope = Column(Text, nullable=True)
date_registered = Column(DateTime(timezone=True), server_default=func.now())
date_modified = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now())
status = Column(String, nullable=True)
heritage_authority = Column(String, nullable=True)
municipality_key = Column(String(8), nullable=True)
wkb_geometry = Column(Geometry('MULTIPOLYGON', srid=4326), nullable=False)
21 changes: 21 additions & 0 deletions app/schemas/monument.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from pydantic import BaseModel
from typing import Optional



class ArchaeologicalMonumentResponse(BaseModel):
monument_id: int
object_name: str
proper_name: Optional[str] = None
object_number: str
district_name: Optional[str] = None
municipality_name: Optional[str] = None
object_description: Optional[str] = None
object_significance: Optional[str] = None
protection_scope: Optional[str] = None
date_registered: Optional[str] = None
date_modified: Optional[str] = None
status: Optional[str] = None
heritage_authority: Optional[str] = None
municipality_key: Optional[str] = None
geojson: Optional[dict] = None
31 changes: 29 additions & 2 deletions app/services/monument.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from sqlalchemy.sql import text
from sqlalchemy.sql import func, text
from sqlalchemy.types import JSON
from sqlalchemy.future import select
from sqlalchemy.sql.expression import cast
from sqlalchemy.ext.asyncio import AsyncSession
from fastapi import HTTPException

from ..utils.validators import validate_positive_int32
from ..utils.validators import validate_positive_int32, validate_not_none
from ..schemas.monument import ArchaeologicalMonumentResponse
from ..models.monument import ArchaeologicalMonument



Expand Down Expand Up @@ -157,3 +162,25 @@ async def get_monument_by_id(session: AsyncSession, monument_id: int):
rows = result.mappings().all()

return [dict(row) for row in rows]


async def get_archaeological_monument_by_id(session: AsyncSession, monument_id: int):
stmt = select(
ArchaeologicalMonument.object_name,
ArchaeologicalMonument.proper_name,
ArchaeologicalMonument.object_number,
ArchaeologicalMonument.district_name,
ArchaeologicalMonument.municipality_name,
ArchaeologicalMonument.object_description,
ArchaeologicalMonument.object_significance,
ArchaeologicalMonument.protection_scope,
func.to_char(ArchaeologicalMonument.date_registered, 'DD.MM.YYYY').label('date_registered'),
func.to_char(ArchaeologicalMonument.date_modified, 'DD.MM.YYYY').label('date_modified'),
ArchaeologicalMonument.status,
ArchaeologicalMonument.heritage_authority,
ArchaeologicalMonument.municipality_key,
cast(func.ST_AsGeoJSON(ArchaeologicalMonument.wkb_geometry, 15), JSON).label('geojson'),
).where(ArchaeologicalMonument.id == monument_id)

result = await session.execute(stmt)
return result.mappings().all()

0 comments on commit 64feb4b

Please sign in to comment.