mirror of
https://github.com/wowlikon/LibraryAPI.git
synced 2025-12-11 21:30:46 +00:00
Форматирование кода, добавление лого, исправление тестов, улучшение эндпоинтов и документации
This commit is contained in:
43
tests/mock_routers/genres.py
Normal file
43
tests/mock_routers/genres.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from tests.mocks.mock_storage import mock_storage
|
||||
|
||||
router = APIRouter(prefix="/genres", tags=["genres"])
|
||||
|
||||
|
||||
@router.post("/")
|
||||
def create_genre(genre: dict):
|
||||
return mock_storage.create_genre(genre["name"])
|
||||
|
||||
|
||||
@router.get("/")
|
||||
def read_genres():
|
||||
genres = mock_storage.get_all_genres()
|
||||
return {"genres": genres, "total": len(genres)}
|
||||
|
||||
|
||||
@router.get("/{genre_id}")
|
||||
def get_genre(genre_id: int):
|
||||
genre = mock_storage.get_genre(genre_id)
|
||||
if not genre:
|
||||
raise HTTPException(status_code=404, detail="genre not found")
|
||||
|
||||
books = mock_storage.get_books_by_genre(genre_id)
|
||||
genre_with_books = genre.copy()
|
||||
genre_with_books["books"] = books
|
||||
return genre_with_books
|
||||
|
||||
|
||||
@router.put("/{genre_id}")
|
||||
def update_genre(genre_id: int, genre: dict):
|
||||
updated_genre = mock_storage.update_genre(genre_id, genre.get("name"))
|
||||
if not updated_genre:
|
||||
raise HTTPException(status_code=404, detail="genre not found")
|
||||
return updated_genre
|
||||
|
||||
|
||||
@router.delete("/{genre_id}")
|
||||
def delete_genre(genre_id: int):
|
||||
genre = mock_storage.delete_genre(genre_id)
|
||||
if not genre:
|
||||
raise HTTPException(status_code=404, detail="genre not found")
|
||||
return genre
|
||||
Reference in New Issue
Block a user