Init project structure
This commit is contained in:
@@ -0,0 +1 @@
|
||||
"""Version 1 API routers."""
|
||||
@@ -0,0 +1,22 @@
|
||||
"""Authentication API endpoints."""
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
|
||||
from app.api.deps import get_auth_service
|
||||
from app.models.token import LoginRequest, TokenResponse
|
||||
from app.services.auth_service import AuthService, InvalidCredentialsError
|
||||
|
||||
router = APIRouter(prefix="/auth", tags=["auth"])
|
||||
|
||||
|
||||
@router.post("/token", response_model=TokenResponse)
|
||||
async def login_for_access_token(
|
||||
credentials: LoginRequest,
|
||||
service: AuthService = Depends(get_auth_service),
|
||||
) -> TokenResponse:
|
||||
try:
|
||||
user = await service.authenticate(credentials.email, credentials.password)
|
||||
except InvalidCredentialsError as exc: # pragma: no cover - thin API
|
||||
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail=str(exc)) from exc
|
||||
return service.create_access_token(user)
|
||||
@@ -0,0 +1,37 @@
|
||||
"""User API endpoints."""
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
|
||||
from app.api.deps import get_user_service
|
||||
from app.models.user import UserCreate, UserRead
|
||||
from app.services.user_service import UserAlreadyExistsError, UserNotFoundError, UserService
|
||||
|
||||
router = APIRouter(prefix="/users", tags=["users"])
|
||||
|
||||
|
||||
@router.get("/", response_model=list[UserRead])
|
||||
async def list_users(service: UserService = Depends(get_user_service)) -> list[UserRead]:
|
||||
users = await service.list_users()
|
||||
return [UserRead.model_validate(user) for user in users]
|
||||
|
||||
|
||||
@router.post("/", response_model=UserRead, status_code=status.HTTP_201_CREATED)
|
||||
async def create_user(
|
||||
user_in: UserCreate,
|
||||
service: UserService = Depends(get_user_service),
|
||||
) -> UserRead:
|
||||
try:
|
||||
user = await service.create_user(user_in)
|
||||
except UserAlreadyExistsError as exc: # pragma: no cover - thin API layer
|
||||
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=str(exc)) from exc
|
||||
return UserRead.model_validate(user)
|
||||
|
||||
|
||||
@router.get("/{user_id}", response_model=UserRead)
|
||||
async def get_user(user_id: int, service: UserService = Depends(get_user_service)) -> UserRead:
|
||||
try:
|
||||
user = await service.get_user(user_id)
|
||||
except UserNotFoundError as exc: # pragma: no cover - thin API layer
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=str(exc)) from exc
|
||||
return UserRead.model_validate(user)
|
||||
Reference in New Issue
Block a user