refactor: improve code formatting and organization across multiple files
This commit is contained in:
@@ -25,17 +25,25 @@ async def login(credentials: LoginRequest) -> AuthResponse:
|
||||
return issue_token_for_user(user)
|
||||
|
||||
|
||||
@router.post("/register", response_model=AuthResponse, status_code=status.HTTP_201_CREATED)
|
||||
@router.post(
|
||||
"/register", response_model=AuthResponse, status_code=status.HTTP_201_CREATED
|
||||
)
|
||||
async def register(payload: RegisterRequest) -> AuthResponse:
|
||||
try:
|
||||
user = register_user(payload.username, payload.password, payload.full_name)
|
||||
except ValueError as exc:
|
||||
message = str(exc)
|
||||
status_code = status.HTTP_409_CONFLICT if "exists" in message else status.HTTP_400_BAD_REQUEST
|
||||
status_code = (
|
||||
status.HTTP_409_CONFLICT
|
||||
if "exists" in message
|
||||
else status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
raise HTTPException(status_code=status_code, detail=message) from exc
|
||||
return issue_token_for_user(user)
|
||||
|
||||
|
||||
@router.get("/me", response_model=UserPublic)
|
||||
async def read_current_user(current_user: UserPublic = Depends(get_current_user)) -> UserPublic:
|
||||
async def read_current_user(
|
||||
current_user: UserPublic = Depends(get_current_user),
|
||||
) -> UserPublic:
|
||||
return current_user
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
from functools import lru_cache
|
||||
from typing import Optional
|
||||
|
||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||
|
||||
|
||||
from typing import Optional
|
||||
|
||||
|
||||
class Settings(BaseSettings):
|
||||
project_name: str = "Rail Game API"
|
||||
version: str = "0.1.0"
|
||||
|
||||
@@ -25,12 +25,18 @@ def create_access_token(subject: str, expires_delta: timedelta | None = None) ->
|
||||
expires_delta or timedelta(minutes=settings.access_token_expire_minutes)
|
||||
)
|
||||
to_encode: Dict[str, Any] = {"sub": subject, "exp": expire}
|
||||
return jwt.encode(to_encode, settings.jwt_secret_key, algorithm=settings.jwt_algorithm)
|
||||
return jwt.encode(
|
||||
to_encode, settings.jwt_secret_key, algorithm=settings.jwt_algorithm
|
||||
)
|
||||
|
||||
|
||||
def decode_access_token(token: str) -> Dict[str, Any]:
|
||||
settings = get_settings()
|
||||
try:
|
||||
return jwt.decode(token, settings.jwt_secret_key, algorithms=[settings.jwt_algorithm])
|
||||
except JWTError as exc: # pragma: no cover - specific error mapping handled by caller
|
||||
return jwt.decode(
|
||||
token, settings.jwt_secret_key, algorithms=[settings.jwt_algorithm]
|
||||
)
|
||||
except (
|
||||
JWTError
|
||||
) as exc: # pragma: no cover - specific error mapping handled by caller
|
||||
raise ValueError("Invalid token") from exc
|
||||
|
||||
@@ -3,7 +3,17 @@ from __future__ import annotations
|
||||
import uuid
|
||||
|
||||
from geoalchemy2 import Geometry
|
||||
from sqlalchemy import Boolean, DateTime, Float, ForeignKey, Integer, Numeric, String, Text, UniqueConstraint
|
||||
from sqlalchemy import (
|
||||
Boolean,
|
||||
DateTime,
|
||||
Float,
|
||||
ForeignKey,
|
||||
Integer,
|
||||
Numeric,
|
||||
String,
|
||||
Text,
|
||||
UniqueConstraint,
|
||||
)
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
||||
from sqlalchemy.sql import func
|
||||
@@ -14,16 +24,23 @@ class Base(DeclarativeBase):
|
||||
|
||||
|
||||
class TimestampMixin:
|
||||
created_at: Mapped[DateTime] = mapped_column(DateTime(timezone=True), server_default=func.now(), nullable=False)
|
||||
created_at: Mapped[DateTime] = mapped_column(
|
||||
DateTime(timezone=True), server_default=func.now(), nullable=False
|
||||
)
|
||||
updated_at: Mapped[DateTime] = mapped_column(
|
||||
DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), nullable=False
|
||||
DateTime(timezone=True),
|
||||
server_default=func.now(),
|
||||
onupdate=func.now(),
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
|
||||
class User(Base, TimestampMixin):
|
||||
__tablename__ = "users"
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
||||
)
|
||||
username: Mapped[str] = mapped_column(String(64), unique=True, nullable=False)
|
||||
email: Mapped[str | None] = mapped_column(String(255), unique=True, nullable=True)
|
||||
full_name: Mapped[str | None] = mapped_column(String(128), nullable=True)
|
||||
@@ -35,11 +52,15 @@ class User(Base, TimestampMixin):
|
||||
class Station(Base, TimestampMixin):
|
||||
__tablename__ = "stations"
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
||||
)
|
||||
osm_id: Mapped[str | None] = mapped_column(String(32), nullable=True)
|
||||
name: Mapped[str] = mapped_column(String(128), nullable=False)
|
||||
code: Mapped[str | None] = mapped_column(String(16), nullable=True)
|
||||
location: Mapped[str] = mapped_column(Geometry(geometry_type="POINT", srid=4326), nullable=False)
|
||||
location: Mapped[str] = mapped_column(
|
||||
Geometry(geometry_type="POINT", srid=4326), nullable=False
|
||||
)
|
||||
elevation_m: Mapped[float | None] = mapped_column(Float, nullable=True)
|
||||
is_active: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
|
||||
|
||||
@@ -47,28 +68,50 @@ class Station(Base, TimestampMixin):
|
||||
class Track(Base, TimestampMixin):
|
||||
__tablename__ = "tracks"
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
||||
)
|
||||
name: Mapped[str | None] = mapped_column(String(128), nullable=True)
|
||||
start_station_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("stations.id", ondelete="RESTRICT"), nullable=False)
|
||||
end_station_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("stations.id", ondelete="RESTRICT"), nullable=False)
|
||||
start_station_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("stations.id", ondelete="RESTRICT"),
|
||||
nullable=False,
|
||||
)
|
||||
end_station_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("stations.id", ondelete="RESTRICT"),
|
||||
nullable=False,
|
||||
)
|
||||
length_meters: Mapped[float | None] = mapped_column(Numeric(10, 2), nullable=True)
|
||||
max_speed_kph: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||||
is_bidirectional: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
|
||||
is_bidirectional: Mapped[bool] = mapped_column(
|
||||
Boolean, nullable=False, default=True
|
||||
)
|
||||
status: Mapped[str] = mapped_column(String(32), nullable=False, default="planned")
|
||||
track_geometry: Mapped[str] = mapped_column(Geometry(geometry_type="LINESTRING", srid=4326), nullable=False)
|
||||
track_geometry: Mapped[str] = mapped_column(
|
||||
Geometry(geometry_type="LINESTRING", srid=4326), nullable=False
|
||||
)
|
||||
|
||||
__table_args__ = (
|
||||
UniqueConstraint("start_station_id", "end_station_id", name="uq_tracks_station_pair"),
|
||||
UniqueConstraint(
|
||||
"start_station_id", "end_station_id", name="uq_tracks_station_pair"
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
class Train(Base, TimestampMixin):
|
||||
__tablename__ = "trains"
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
||||
)
|
||||
designation: Mapped[str] = mapped_column(String(64), nullable=False, unique=True)
|
||||
operator_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL"))
|
||||
home_station_id: Mapped[uuid.UUID | None] = mapped_column(UUID(as_uuid=True), ForeignKey("stations.id", ondelete="SET NULL"))
|
||||
operator_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||
UUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL")
|
||||
)
|
||||
home_station_id: Mapped[uuid.UUID | None] = mapped_column(
|
||||
UUID(as_uuid=True), ForeignKey("stations.id", ondelete="SET NULL")
|
||||
)
|
||||
capacity: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
max_speed_kph: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
consist: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
@@ -77,14 +120,28 @@ class Train(Base, TimestampMixin):
|
||||
class TrainSchedule(Base, TimestampMixin):
|
||||
__tablename__ = "train_schedules"
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
train_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("trains.id", ondelete="CASCADE"), nullable=False)
|
||||
id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
|
||||
)
|
||||
train_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True), ForeignKey("trains.id", ondelete="CASCADE"), nullable=False
|
||||
)
|
||||
sequence_index: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
station_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("stations.id", ondelete="CASCADE"), nullable=False)
|
||||
scheduled_arrival: Mapped[DateTime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
scheduled_departure: Mapped[DateTime | None] = mapped_column(DateTime(timezone=True), nullable=True)
|
||||
station_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("stations.id", ondelete="CASCADE"),
|
||||
nullable=False,
|
||||
)
|
||||
scheduled_arrival: Mapped[DateTime | None] = mapped_column(
|
||||
DateTime(timezone=True), nullable=True
|
||||
)
|
||||
scheduled_departure: Mapped[DateTime | None] = mapped_column(
|
||||
DateTime(timezone=True), nullable=True
|
||||
)
|
||||
dwell_seconds: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||||
|
||||
__table_args__ = (
|
||||
UniqueConstraint("train_id", "sequence_index", name="uq_train_schedule_sequence"),
|
||||
UniqueConstraint(
|
||||
"train_id", "sequence_index", name="uq_train_schedule_sequence"
|
||||
),
|
||||
)
|
||||
|
||||
@@ -9,16 +9,20 @@ from backend.app.core.config import get_settings
|
||||
|
||||
settings = get_settings()
|
||||
|
||||
engine = create_engine(settings.sqlalchemy_database_url, echo=settings.database_echo, future=True)
|
||||
SessionLocal = sessionmaker(bind=engine, autoflush=False, autocommit=False, expire_on_commit=False)
|
||||
engine = create_engine(
|
||||
settings.sqlalchemy_database_url, echo=settings.database_echo, future=True
|
||||
)
|
||||
SessionLocal = sessionmaker(
|
||||
bind=engine, autoflush=False, autocommit=False, expire_on_commit=False
|
||||
)
|
||||
|
||||
|
||||
def get_db_session() -> Generator[Session, None, None]:
|
||||
session = SessionLocal()
|
||||
try:
|
||||
yield session
|
||||
finally:
|
||||
session.close()
|
||||
session = SessionLocal()
|
||||
try:
|
||||
yield session
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
|
||||
__all__ = ["engine", "SessionLocal", "get_db_session"]
|
||||
|
||||
@@ -8,9 +8,9 @@ from sqlalchemy.orm import Session
|
||||
from backend.app.db.session import SessionLocal
|
||||
from backend.app.repositories import (
|
||||
StationRepository,
|
||||
TrackRepository,
|
||||
TrainRepository,
|
||||
TrainScheduleRepository,
|
||||
TrackRepository,
|
||||
UserRepository,
|
||||
)
|
||||
|
||||
|
||||
@@ -1,39 +1,39 @@
|
||||
from .auth import (
|
||||
AuthResponse,
|
||||
LoginRequest,
|
||||
RegisterRequest,
|
||||
TokenPayload,
|
||||
TokenResponse,
|
||||
UserInDB,
|
||||
UserPublic,
|
||||
AuthResponse,
|
||||
LoginRequest,
|
||||
RegisterRequest,
|
||||
TokenPayload,
|
||||
TokenResponse,
|
||||
UserInDB,
|
||||
UserPublic,
|
||||
)
|
||||
from .base import (
|
||||
StationCreate,
|
||||
StationModel,
|
||||
TrackCreate,
|
||||
TrackModel,
|
||||
TrainScheduleCreate,
|
||||
TrainCreate,
|
||||
TrainModel,
|
||||
UserCreate,
|
||||
to_camel,
|
||||
StationCreate,
|
||||
StationModel,
|
||||
TrackCreate,
|
||||
TrackModel,
|
||||
TrainCreate,
|
||||
TrainModel,
|
||||
TrainScheduleCreate,
|
||||
UserCreate,
|
||||
to_camel,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"LoginRequest",
|
||||
"RegisterRequest",
|
||||
"AuthResponse",
|
||||
"TokenPayload",
|
||||
"TokenResponse",
|
||||
"UserInDB",
|
||||
"UserPublic",
|
||||
"StationCreate",
|
||||
"StationModel",
|
||||
"TrackCreate",
|
||||
"TrackModel",
|
||||
"TrainScheduleCreate",
|
||||
"TrainCreate",
|
||||
"TrainModel",
|
||||
"UserCreate",
|
||||
"to_camel",
|
||||
"LoginRequest",
|
||||
"RegisterRequest",
|
||||
"AuthResponse",
|
||||
"TokenPayload",
|
||||
"TokenResponse",
|
||||
"UserInDB",
|
||||
"UserPublic",
|
||||
"StationCreate",
|
||||
"StationModel",
|
||||
"TrackCreate",
|
||||
"TrackModel",
|
||||
"TrainScheduleCreate",
|
||||
"TrainCreate",
|
||||
"TrainModel",
|
||||
"UserCreate",
|
||||
"to_camel",
|
||||
]
|
||||
|
||||
@@ -10,6 +10,7 @@ def to_camel(string: str) -> str:
|
||||
head, *tail = string.split("_")
|
||||
return head + "".join(part.capitalize() for part in tail)
|
||||
|
||||
|
||||
IdT = TypeVar("IdT", bound=str)
|
||||
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
"""Repository abstractions for database access."""
|
||||
|
||||
from backend.app.repositories.stations import StationRepository
|
||||
from backend.app.repositories.train_schedules import TrainScheduleRepository
|
||||
from backend.app.repositories.tracks import TrackRepository
|
||||
from backend.app.repositories.train_schedules import TrainScheduleRepository
|
||||
from backend.app.repositories.trains import TrainRepository
|
||||
from backend.app.repositories.users import UserRepository
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from typing import Generic, Iterable, Optional, Sequence, Type, TypeVar
|
||||
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from backend.app.db.models import Base
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from geoalchemy2.elements import WKTElement
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from geoalchemy2.elements import WKTElement
|
||||
|
||||
from backend.app.db.models import Station
|
||||
from backend.app.repositories.base import BaseRepository
|
||||
from backend.app.models import StationCreate
|
||||
from backend.app.repositories.base import BaseRepository
|
||||
|
||||
|
||||
class StationRepository(BaseRepository[Station]):
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from uuid import UUID
|
||||
|
||||
import sqlalchemy as sa
|
||||
from geoalchemy2.elements import WKTElement
|
||||
from uuid import UUID
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from backend.app.db.models import Track
|
||||
from backend.app.repositories.base import BaseRepository
|
||||
from backend.app.models import TrackCreate
|
||||
from backend.app.repositories.base import BaseRepository
|
||||
|
||||
|
||||
class TrackRepository(BaseRepository[Track]):
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from uuid import UUID
|
||||
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from backend.app.db.models import TrainSchedule
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from uuid import UUID
|
||||
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from backend.app.db.models import Train
|
||||
from backend.app.repositories.base import BaseRepository
|
||||
from backend.app.models import TrainCreate
|
||||
from backend.app.repositories.base import BaseRepository
|
||||
|
||||
|
||||
class TrainRepository(BaseRepository[Train]):
|
||||
|
||||
@@ -17,11 +17,15 @@ class UserRepository(BaseRepository[User]):
|
||||
super().__init__(session)
|
||||
|
||||
def get_by_username(self, username: str) -> User | None:
|
||||
statement = sa.select(self.model).where(sa.func.lower(self.model.username) == username.lower())
|
||||
statement = sa.select(self.model).where(
|
||||
sa.func.lower(self.model.username) == username.lower()
|
||||
)
|
||||
return self.session.scalar(statement)
|
||||
|
||||
def list_recent(self, limit: int = 50) -> list[User]:
|
||||
statement = sa.select(self.model).order_by(self.model.created_at.desc()).limit(limit)
|
||||
statement = (
|
||||
sa.select(self.model).order_by(self.model.created_at.desc()).limit(limit)
|
||||
)
|
||||
return list(self.session.scalars(statement))
|
||||
|
||||
def create(self, data: UserCreate) -> User:
|
||||
|
||||
@@ -43,7 +43,9 @@ def to_public_user(user: UserInDB) -> UserPublic:
|
||||
return UserPublic(username=user.username, full_name=user.full_name)
|
||||
|
||||
|
||||
def register_user(username: str, password: str, full_name: Optional[str] = None) -> UserInDB:
|
||||
def register_user(
|
||||
username: str, password: str, full_name: Optional[str] = None
|
||||
) -> UserInDB:
|
||||
normalized_username = username.strip()
|
||||
if not normalized_username:
|
||||
raise ValueError("Username must not be empty")
|
||||
|
||||
@@ -6,13 +6,14 @@ from typing import Iterable, cast
|
||||
|
||||
from geoalchemy2.elements import WKBElement, WKTElement
|
||||
from geoalchemy2.shape import to_shape
|
||||
|
||||
try: # pragma: no cover - optional dependency guard
|
||||
from shapely.geometry import Point # type: ignore
|
||||
except ImportError: # pragma: no cover - allow running without shapely at import time
|
||||
Point = None # type: ignore[assignment]
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from backend.app.models import StationModel, TrackModel, TrainModel
|
||||
from backend.app.repositories import StationRepository, TrackRepository, TrainRepository
|
||||
|
||||
Reference in New Issue
Block a user