feat: T03 backend core – FastAPI + DB models + Equipment API + Redis cache + health + tests
- FastAPI app with CORS, lifespan handlers - Pydantic Settings config (DB, Redis, CORS, SMTP, JWT, Rentman) - SQLAlchemy async engine + session (DeclarativeBase) - 6 DB models: EquipmentCache, RentalRequest, RentalRequestItem, Contact, AdminUser, SyncLog - Pydantic schemas: EquipmentItem, EquipmentDetail, PaginatedEquipment, ContactCreate, ContactResponse - Redis cache helper: set/get/delete_pattern, rate limiting, equipment key builders - Equipment router: list (search/category/sort/pagination), detail, categories – all cached - Contact router: POST with Pydantic validation + rate limiting (5/min) - Health router: GET /api/health with DB + Redis status - 28 pytest tests (all pass, 90% coverage) - Dockerfile, requirements.txt, pytest.ini, test_report.md
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
"""SQLAlchemy models package."""
|
||||
|
||||
from app.models.admin_user import AdminUser
|
||||
from app.models.contact import Contact
|
||||
from app.models.equipment import EquipmentCache
|
||||
from app.models.rental_request import RentalRequest, RentalRequestItem
|
||||
from app.models.sync_log import SyncLog
|
||||
|
||||
__all__ = ["AdminUser", "Contact", "EquipmentCache", "RentalRequest", "RentalRequestItem", "SyncLog"]
|
||||
@@ -0,0 +1,19 @@
|
||||
"""AdminUser SQLAlchemy model."""
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import Integer, String, func
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from app.database import Base
|
||||
|
||||
|
||||
class AdminUser(Base):
|
||||
"""Admin user for backend authentication."""
|
||||
|
||||
__tablename__ = "admin_users"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
username: Mapped[str] = mapped_column(String(64), unique=True, nullable=False)
|
||||
password_hash: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(server_default=func.now(), nullable=False)
|
||||
@@ -0,0 +1,23 @@
|
||||
"""Contact SQLAlchemy model."""
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import Boolean, Integer, String, Text, func
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from app.database import Base
|
||||
|
||||
|
||||
class Contact(Base):
|
||||
"""Contact form submission."""
|
||||
|
||||
__tablename__ = "contacts"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
name: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
email: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
phone: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
||||
message: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
privacy_consent: Mapped[bool] = mapped_column(Boolean, nullable=False)
|
||||
email_sent: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(server_default=func.now(), nullable=False)
|
||||
@@ -0,0 +1,36 @@
|
||||
"""EquipmentCache SQLAlchemy model."""
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import JSON, Boolean, Index, Integer, Numeric, String, Text, func
|
||||
from sqlalchemy.dialects.postgresql import JSONB
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from app.database import Base
|
||||
|
||||
|
||||
class EquipmentCache(Base):
|
||||
"""Cached equipment data imported from Rentman."""
|
||||
|
||||
__tablename__ = "equipment_cache"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
rentman_id: Mapped[str] = mapped_column(String(64), unique=True, nullable=False, index=True)
|
||||
name: Mapped[str] = mapped_column(String(255), nullable=False, index=True)
|
||||
number: Mapped[str | None] = mapped_column(String(64), nullable=True, index=True)
|
||||
category: Mapped[str | None] = mapped_column(String(128), nullable=True, index=True)
|
||||
subcategory: Mapped[str | None] = mapped_column(String(128), nullable=True)
|
||||
description: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
specifications: Mapped[dict | None] = mapped_column(JSON, nullable=True)
|
||||
images: Mapped[list | None] = mapped_column(JSON, nullable=True)
|
||||
rental_price: Mapped[float | None] = mapped_column(Numeric(10, 2), nullable=True)
|
||||
brand: Mapped[str | None] = mapped_column(String(128), nullable=True)
|
||||
available: Mapped[bool] = mapped_column(Boolean, default=True, nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(server_default=func.now(), nullable=False)
|
||||
updated_at: Mapped[datetime] = mapped_column(server_default=func.now(), onupdate=func.now(), nullable=False)
|
||||
|
||||
__table_args__ = (
|
||||
Index("idx_equipment_category", "category"),
|
||||
Index("idx_equipment_name", "name"),
|
||||
Index("idx_equipment_number", "number"),
|
||||
)
|
||||
@@ -0,0 +1,62 @@
|
||||
"""RentalRequest and RentalRequestItem SQLAlchemy models."""
|
||||
|
||||
from datetime import date, datetime
|
||||
|
||||
from sqlalchemy import Date, ForeignKey, Index, Integer, Numeric, String, Text, func
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.database import Base
|
||||
|
||||
|
||||
class RentalRequest(Base):
|
||||
"""Rental request submitted by a customer."""
|
||||
|
||||
__tablename__ = "rental_requests"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
reference_number: Mapped[str] = mapped_column(String(32), unique=True, nullable=False)
|
||||
event_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
date_start: Mapped[date] = mapped_column(Date, nullable=False)
|
||||
date_end: Mapped[date] = mapped_column(Date, nullable=False)
|
||||
location: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
person_count: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||||
contact_name: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
contact_company: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
contact_email: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
contact_phone: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
||||
contact_street: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
contact_postalcode: Mapped[str | None] = mapped_column(String(16), nullable=True)
|
||||
contact_city: Mapped[str | None] = mapped_column(String(128), nullable=True)
|
||||
message: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
status: Mapped[str] = mapped_column(String(32), default="pending", nullable=False)
|
||||
rentman_request_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
||||
rentman_sync_status: Mapped[str] = mapped_column(String(32), default="pending", nullable=False)
|
||||
rentman_sync_error: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(server_default=func.now(), nullable=False)
|
||||
|
||||
items: Mapped[list["RentalRequestItem"]] = relationship(back_populates="rental_request", cascade="all, delete-orphan")
|
||||
|
||||
__table_args__ = (
|
||||
Index("idx_rental_status", "status"),
|
||||
)
|
||||
|
||||
|
||||
class RentalRequestItem(Base):
|
||||
"""Individual equipment line item in a rental request."""
|
||||
|
||||
__tablename__ = "rental_request_items"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
rental_request_id: Mapped[int] = mapped_column(ForeignKey("rental_requests.id", ondelete="CASCADE"), nullable=False)
|
||||
equipment_id: Mapped[int | None] = mapped_column(ForeignKey("equipment_cache.id"), nullable=True)
|
||||
equipment_name: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
rentman_equipment_id: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
||||
quantity: Mapped[int] = mapped_column(Integer, nullable=False, default=1)
|
||||
unit_price: Mapped[float | None] = mapped_column(Numeric(10, 2), nullable=True)
|
||||
rentman_sync_status: Mapped[str] = mapped_column(String(32), default="pending", nullable=False)
|
||||
|
||||
rental_request: Mapped["RentalRequest"] = relationship(back_populates="items")
|
||||
|
||||
__table_args__ = (
|
||||
Index("idx_rental_items_request", "rental_request_id"),
|
||||
)
|
||||
@@ -0,0 +1,27 @@
|
||||
"""SyncLog SQLAlchemy model."""
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import DateTime, Index, Integer, String, Text, func
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from app.database import Base
|
||||
|
||||
|
||||
class SyncLog(Base):
|
||||
"""Log entry for equipment sync operations."""
|
||||
|
||||
__tablename__ = "sync_log"
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
|
||||
sync_type: Mapped[str] = mapped_column(String(32), default="equipment", nullable=False)
|
||||
status: Mapped[str] = mapped_column(String(32), nullable=False)
|
||||
items_processed: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
|
||||
items_failed: Mapped[int] = mapped_column(Integer, default=0, nullable=False)
|
||||
error_message: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
started_at: Mapped[datetime] = mapped_column(server_default=func.now(), nullable=False)
|
||||
completed_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
|
||||
|
||||
__table_args__ = (
|
||||
Index("idx_sync_log_started", "started_at"),
|
||||
)
|
||||
Reference in New Issue
Block a user