Add FastAPI backend with PostGIS-backed nearby search
FastAPI service backed by PostgreSQL+PostGIS: cities/places/place_content schema (normalized per-language, per-length content for easy future re-ingestion), Alembic migration, an iterative-radius-expansion nearby search endpoint, a name-search fallback for denied-location flows, and a YAML seed loader CLI. Verified end-to-end against a live Docker Postgres+PostGIS instance (migration, city seed, place/content CRUD, and nearby radius expansion all confirmed working via curl). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
2acc5dfa2e
commit
d2ab424e7e
43 changed files with 1058 additions and 0 deletions
5
backend/app/models/__init__.py
Normal file
5
backend/app/models/__init__.py
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
from app.models.city import City
|
||||
from app.models.place import Place
|
||||
from app.models.place_content import PlaceContent
|
||||
|
||||
__all__ = ["City", "Place", "PlaceContent"]
|
||||
23
backend/app/models/city.py
Normal file
23
backend/app/models/city.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
from datetime import datetime
|
||||
|
||||
from geoalchemy2 import Geography
|
||||
from sqlalchemy import DateTime, String, func
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.db.base import Base
|
||||
|
||||
|
||||
class City(Base):
|
||||
__tablename__ = "cities"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
slug: Mapped[str] = mapped_column(String(64), unique=True, nullable=False)
|
||||
name_ru: Mapped[str] = mapped_column(String(128), nullable=False)
|
||||
name_en: Mapped[str] = mapped_column(String(128), nullable=False)
|
||||
country_code: Mapped[str] = mapped_column(String(2), nullable=False, default="RU")
|
||||
center_point = mapped_column(
|
||||
Geography(geometry_type="POINT", srid=4326, spatial_index=False), nullable=True
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||
|
||||
places: Mapped[list["Place"]] = relationship(back_populates="city", cascade="all, delete-orphan")
|
||||
25
backend/app/models/enums.py
Normal file
25
backend/app/models/enums.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import enum
|
||||
|
||||
|
||||
class PlaceCategory(str, enum.Enum):
|
||||
building = "building"
|
||||
bridge = "bridge"
|
||||
monument = "monument"
|
||||
park = "park"
|
||||
district = "district"
|
||||
skyscraper = "skyscraper"
|
||||
cathedral = "cathedral"
|
||||
museum = "museum"
|
||||
gate = "gate"
|
||||
square = "square"
|
||||
|
||||
|
||||
class ContentLanguage(str, enum.Enum):
|
||||
ru = "ru"
|
||||
en = "en"
|
||||
|
||||
|
||||
class ContentLength(str, enum.Enum):
|
||||
short = "short"
|
||||
medium = "medium"
|
||||
long = "long"
|
||||
38
backend/app/models/place.py
Normal file
38
backend/app/models/place.py
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
from datetime import datetime
|
||||
|
||||
from geoalchemy2 import Geography
|
||||
from sqlalchemy import Boolean, DateTime, Enum, ForeignKey, String, Text, func
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.db.base import Base
|
||||
from app.models.enums import PlaceCategory
|
||||
|
||||
|
||||
class Place(Base):
|
||||
__tablename__ = "places"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
city_id: Mapped[int] = mapped_column(ForeignKey("cities.id", ondelete="CASCADE"), nullable=False)
|
||||
slug: Mapped[str] = mapped_column(String(128), unique=True, nullable=False)
|
||||
category: Mapped[PlaceCategory] = mapped_column(
|
||||
Enum(PlaceCategory, name="place_category", native_enum=True), nullable=False
|
||||
)
|
||||
location = mapped_column(
|
||||
Geography(geometry_type="POINT", srid=4326, spatial_index=False), nullable=False
|
||||
)
|
||||
address: Mapped[str | None] = mapped_column(String(256))
|
||||
district: Mapped[str | None] = mapped_column(String(128))
|
||||
built_year: Mapped[str | None] = mapped_column(String(32))
|
||||
architect_builder: Mapped[str | None] = mapped_column(String(256))
|
||||
architectural_style: Mapped[str | None] = mapped_column(String(128))
|
||||
notable_people: Mapped[str | None] = mapped_column(Text)
|
||||
is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), server_default=func.now())
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True), server_default=func.now(), onupdate=func.now()
|
||||
)
|
||||
|
||||
city: Mapped["City"] = relationship(back_populates="places")
|
||||
contents: Mapped[list["PlaceContent"]] = relationship(
|
||||
back_populates="place", cascade="all, delete-orphan"
|
||||
)
|
||||
23
backend/app/models/place_content.py
Normal file
23
backend/app/models/place_content.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
from sqlalchemy import Enum, ForeignKey, String, Text, UniqueConstraint
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.db.base import Base
|
||||
from app.models.enums import ContentLanguage, ContentLength
|
||||
|
||||
|
||||
class PlaceContent(Base):
|
||||
__tablename__ = "place_content"
|
||||
__table_args__ = (UniqueConstraint("place_id", "language", "length", name="uq_place_content_variant"),)
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
place_id: Mapped[int] = mapped_column(ForeignKey("places.id", ondelete="CASCADE"), nullable=False)
|
||||
language: Mapped[ContentLanguage] = mapped_column(
|
||||
Enum(ContentLanguage, name="content_language", native_enum=True), nullable=False
|
||||
)
|
||||
length: Mapped[ContentLength] = mapped_column(
|
||||
Enum(ContentLength, name="content_length", native_enum=True), nullable=False
|
||||
)
|
||||
title: Mapped[str] = mapped_column(String(256), nullable=False)
|
||||
body: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
|
||||
place: Mapped["Place"] = relationship(back_populates="contents")
|
||||
Loading…
Add table
Add a link
Reference in a new issue