style: apply ruff formatting to source and test files
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
"""Database connection and session management."""
|
||||
|
||||
from collections.abc import AsyncGenerator
|
||||
from contextlib import asynccontextmanager
|
||||
from typing import AsyncGenerator
|
||||
|
||||
from sqlalchemy.ext.asyncio import (
|
||||
AsyncEngine,
|
||||
@@ -38,7 +38,7 @@ AsyncSessionLocal = async_sessionmaker(
|
||||
)
|
||||
|
||||
|
||||
async def get_session() -> AsyncGenerator[AsyncSession, None]:
|
||||
async def get_session() -> AsyncGenerator[AsyncSession]:
|
||||
"""Get database session."""
|
||||
async with AsyncSessionLocal() as session:
|
||||
try:
|
||||
@@ -48,7 +48,7 @@ async def get_session() -> AsyncGenerator[AsyncSession, None]:
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def get_session_context() -> AsyncGenerator[AsyncSession, None]:
|
||||
async def get_session_context() -> AsyncGenerator[AsyncSession]:
|
||||
"""Get database session as context manager."""
|
||||
async with AsyncSessionLocal() as session:
|
||||
try:
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""SQLAlchemy ORM models."""
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from datetime import UTC, datetime
|
||||
from uuid import uuid4
|
||||
|
||||
from sqlalchemy import JSON, Boolean, DateTime, String, Text
|
||||
@@ -14,27 +14,21 @@ class PostORM(Base): # type: ignore[valid-type,misc]
|
||||
|
||||
__tablename__ = "posts"
|
||||
|
||||
id: Mapped[str] = mapped_column(
|
||||
String(36), primary_key=True, default=lambda: str(uuid4())
|
||||
)
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=lambda: str(uuid4()))
|
||||
title: Mapped[str] = mapped_column(String(200), nullable=False)
|
||||
content: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
slug: Mapped[str] = mapped_column(
|
||||
String(200), nullable=False, unique=True, index=True
|
||||
)
|
||||
slug: Mapped[str] = mapped_column(String(200), nullable=False, unique=True, index=True)
|
||||
author_id: Mapped[str] = mapped_column(String(100), nullable=False, index=True)
|
||||
published: Mapped[bool] = mapped_column(
|
||||
Boolean, default=False, nullable=False, index=True
|
||||
)
|
||||
published: Mapped[bool] = mapped_column(Boolean, default=False, nullable=False, index=True)
|
||||
tags: Mapped[list[str]] = mapped_column(JSON, default=list)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
default=lambda: datetime.now(timezone.utc),
|
||||
default=lambda: datetime.now(UTC),
|
||||
nullable=False,
|
||||
)
|
||||
updated_at: Mapped[datetime] = mapped_column(
|
||||
DateTime(timezone=True),
|
||||
default=lambda: datetime.now(timezone.utc),
|
||||
onupdate=lambda: datetime.now(timezone.utc),
|
||||
default=lambda: datetime.now(UTC),
|
||||
onupdate=lambda: datetime.now(UTC),
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""Dishka providers for dependency injection."""
|
||||
|
||||
from typing import AsyncGenerator
|
||||
from collections.abc import AsyncGenerator
|
||||
|
||||
from dishka import Provider, Scope, provide
|
||||
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession
|
||||
@@ -30,7 +30,7 @@ class DatabaseProvider(Provider):
|
||||
return engine
|
||||
|
||||
@provide(scope=Scope.REQUEST)
|
||||
async def get_session(self) -> AsyncGenerator[AsyncSession, None]:
|
||||
async def get_session(self) -> AsyncGenerator[AsyncSession]:
|
||||
"""Provide database session per request."""
|
||||
async with AsyncSessionLocal() as session:
|
||||
try:
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""Exception handling middleware."""
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.responses import JSONResponse
|
||||
@@ -33,9 +33,7 @@ def get_status_code(exc: DomainException) -> int:
|
||||
return 500
|
||||
|
||||
|
||||
async def domain_exception_handler(
|
||||
request: Request, exc: DomainException
|
||||
) -> JSONResponse:
|
||||
async def domain_exception_handler(request: Request, exc: DomainException) -> JSONResponse:
|
||||
"""Handle domain exceptions."""
|
||||
status_code = get_status_code(exc)
|
||||
return JSONResponse(
|
||||
@@ -43,22 +41,20 @@ async def domain_exception_handler(
|
||||
content={
|
||||
"error": exc.__class__.__name__,
|
||||
"message": exc.message,
|
||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||
"timestamp": datetime.now(UTC).isoformat(),
|
||||
"path": str(request.url.path),
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
async def http_exception_handler(
|
||||
request: Request, exc: StarletteHTTPException
|
||||
) -> JSONResponse:
|
||||
async def http_exception_handler(request: Request, exc: StarletteHTTPException) -> JSONResponse:
|
||||
"""Handle HTTP exceptions."""
|
||||
return JSONResponse(
|
||||
status_code=exc.status_code,
|
||||
content={
|
||||
"error": "HTTPException",
|
||||
"message": str(exc.detail),
|
||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||
"timestamp": datetime.now(UTC).isoformat(),
|
||||
"path": str(request.url.path),
|
||||
},
|
||||
)
|
||||
@@ -71,7 +67,7 @@ async def generic_exception_handler(request: Request, exc: Exception) -> JSONRes
|
||||
content={
|
||||
"error": "InternalServerError",
|
||||
"message": "An unexpected error occurred",
|
||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||
"timestamp": datetime.now(UTC).isoformat(),
|
||||
"path": str(request.url.path),
|
||||
},
|
||||
)
|
||||
|
||||
@@ -47,9 +47,7 @@ class SQLAlchemyPostRepository(PostRepository):
|
||||
|
||||
async def get_by_id(self, entity_id: UUID) -> Post | None:
|
||||
"""Get post by ID."""
|
||||
result = await self._session.execute(
|
||||
select(PostORM).where(PostORM.id == str(entity_id))
|
||||
)
|
||||
result = await self._session.execute(select(PostORM).where(PostORM.id == str(entity_id)))
|
||||
orm = result.scalar_one_or_none()
|
||||
return self._to_domain(orm) if orm else None
|
||||
|
||||
@@ -67,9 +65,7 @@ class SQLAlchemyPostRepository(PostRepository):
|
||||
|
||||
async def update(self, entity: Post) -> None:
|
||||
"""Update existing post."""
|
||||
result = await self._session.execute(
|
||||
select(PostORM).where(PostORM.id == str(entity.id))
|
||||
)
|
||||
result = await self._session.execute(select(PostORM).where(PostORM.id == str(entity.id)))
|
||||
orm = result.scalar_one()
|
||||
|
||||
orm.title = entity.title.value
|
||||
@@ -83,25 +79,19 @@ class SQLAlchemyPostRepository(PostRepository):
|
||||
|
||||
async def delete(self, entity_id: UUID) -> None:
|
||||
"""Delete post by ID."""
|
||||
result = await self._session.execute(
|
||||
select(PostORM).where(PostORM.id == str(entity_id))
|
||||
)
|
||||
result = await self._session.execute(select(PostORM).where(PostORM.id == str(entity_id)))
|
||||
orm = result.scalar_one_or_none()
|
||||
if orm:
|
||||
await self._session.delete(orm)
|
||||
|
||||
async def exists(self, entity_id: UUID) -> bool:
|
||||
"""Check if post exists."""
|
||||
result = await self._session.execute(
|
||||
select(PostORM).where(PostORM.id == str(entity_id))
|
||||
)
|
||||
result = await self._session.execute(select(PostORM).where(PostORM.id == str(entity_id)))
|
||||
return result.scalar_one_or_none() is not None
|
||||
|
||||
async def get_by_slug(self, slug: str) -> Post | None:
|
||||
"""Get post by slug."""
|
||||
result = await self._session.execute(
|
||||
select(PostORM).where(PostORM.slug == slug)
|
||||
)
|
||||
result = await self._session.execute(select(PostORM).where(PostORM.slug == slug))
|
||||
orm = result.scalar_one_or_none()
|
||||
return self._to_domain(orm) if orm else None
|
||||
|
||||
@@ -154,9 +144,7 @@ class SQLAlchemyPostRepository(PostRepository):
|
||||
|
||||
async def slug_exists(self, slug: str) -> bool:
|
||||
"""Check if slug exists."""
|
||||
result = await self._session.execute(
|
||||
select(PostORM).where(PostORM.slug == slug)
|
||||
)
|
||||
result = await self._session.execute(select(PostORM).where(PostORM.slug == slug))
|
||||
return result.scalar_one_or_none() is not None
|
||||
|
||||
async def search(
|
||||
|
||||
Reference in New Issue
Block a user