```python from fastapi import FastAPI, HTTPException from pydantic import BaseModel from typing import List, Optional app = FastAPI() # ── Models ──────────────────────────────────────────────────────────────────── class Book(BaseModel): title: str isbn: str price: float publication_year: int class Author(BaseModel): first_name: str last_name: str birth_year: int nationality: str class Publisher(BaseModel): name: str country: str founded_year: int website: str class Genre(BaseModel): name: str description: str parent_genre: Optional[str] = None created_at: str class Customer(BaseModel): name: str email: str phone: str joined_date: str # ── Storage ─────────────────────────────────────────────────────────────────── books_store: dict[int, Book] = {} books_counter: int = 0 authors_store: dict[int, Author] = {} authors_counter: int = 0 publishers_store: dict[int, Publisher] = {} publishers_counter: int = 0 genres_store: dict[int, Genre] = {} genres_counter: int = 0 customers_store: dict[int, Customer] = {} customers_counter: int = 0 # ── Books ───────────────────────────────────────────────────────────────────── @app.get("/books", response_model=List[Book]) def list_books() -> List[Book]: """Return all books.""" return list(books_store.values()) @app.get("/books/{book_id}", response_model=Book) def get_book(book_id: int) -> Book: """Return a single book by id.""" if book_id not in books_store: raise HTTPException(status_code=404, detail="Book not found") return books_store[book_id] @app.post("/books", response_model=Book) def create_book(book: Book) -> Book: """Create and return a new book.""" global books_counter books_counter += 1 books_store[books_counter] = book return book @app.put("/books/{book_id}", response_model=Book) def update_book(book_id: int, book: Book) -> Book: """Update and return an existing book.""" if book_id not in books_store: raise HTTPException(status_code=404, detail="Book not found") books_store[book_id] = book return book @app.delete("/books/{book_id}", status_code=204) def delete_book(book_id: int) -> None: """Delete a book by id.""" if book_id not in books_store: raise HTTPException(status_code=404, detail="Book not found") del books_store[book_id] # ── Authors ─────────────────────────────────────────────────────────────────── @app.get("/authors", response_model=List[Author]) def list_authors() -> List[Author]: """Return all authors.""" return list(authors_store.values()) @app.get("/authors/{author_id}", response_model=Author) def get_author(author_id: int) -> Author: """Return a single author by id.""" if author_id not in authors_store: raise HTTPException(status_code=404, detail="Author not found") return authors_store[author_id] @app.post("/authors", response_model=Author) def create_author(author: Author) -> Author: """Create and return a new author.""" global authors_counter authors_counter += 1 authors_store[authors_counter] = author return author @app.put("/authors/{author_id}", response_model=Author) def update_author(author_id: int, author: Author) -> Author: """Update and return an existing author.""" if author_id not in authors_store: raise HTTPException(status_code=404, detail="Author not found") authors_store[author_id] = author return author @app.delete("/authors/{author_id}", status_code=204) def delete_author(author_id: int) -> None: """Delete an author by id.""" if author_id not in authors_store: raise HTTPException(status_code=404, detail="Author not found") del authors_store[author_id] # ── Publishers ──────────────────────────────────────────────────────────────── @app.get("/publishers", response_model=List[Publisher]) def list_publishers() -> List[Publisher]: """Return all publishers.""" return list(publishers_store.values()) @app.get("/publishers/{publisher_id}", response_model=Publisher) def get_publisher(publisher_id: int) -> Publisher: """Return a single publisher by id.""" if publisher_id not in publishers_store: raise HTTPException(status_code=404, detail="Publisher not found") return publishers_store[publisher_id] @app.post("/publishers", response_model=Publisher) def create_publisher(publisher: Publisher) -> Publisher: """Create and return a new publisher.""" global publishers_counter publishers_counter += 1 publishers_store[publishers_counter] = publisher return publisher @app.put("/publishers/{publisher_id}", response_model=Publisher) def update_publisher(publisher_id: int, publisher: Publisher) -> Publisher: """Update and return an existing publisher.""" if publisher_id not in publishers_store: raise HTTPException(status_code=404, detail="Publisher not found") publishers_store[publisher_id] = publisher return publisher @app.delete("/publishers/{publisher_id}", status_code=204) def delete_publisher(publisher_id: int) -> None: """Delete a publisher by id.""" if publisher_id not in publishers_store: raise HTTPException(status_code=404, detail="Publisher not found") del publishers_store[publisher_id] # ── Genres ──────────────────────────────────────────────────────────────────── @app.get("/genres", response_model=List[Genre]) def list_genres() -> List[Genre]: """Return all genres.""" return list(genres_store.values()) @app.get("/genres/{genre_id}", response_model=Genre) def get_genre(genre_id: int) -> Genre: """Return a single genre by id.""" if genre_id not in genres_store: raise HTTPException(status_code=404, detail="Genre not found") return genres_store[genre_id] @app.post("/genres", response_model=Genre) def create_genre(genre: Genre) -> Genre: """Create and return a new genre.""" global genres_counter genres_counter += 1 genres_store[genres_counter] = genre return genre @app.put("/genres/{genre_id}", response_model=Genre) def update_genre(genre_id: int, genre: Genre) -> Genre: """Update and return an existing genre.""" if genre_id not in genres_store: raise HTTPException(status_code=404, detail="Genre not found") genres_store[genre_id] = genre return genre @app.delete("/genres/{genre_id}", status_code=204) def delete_genre(genre_id: int) -> None: """Delete a genre by id.""" if genre_id not in genres_store: raise HTTPException(status_code=404, detail="Genre not found") del genres_store[genre_id] # ── Customers ───────────────────────────────────────────────────────────────── @app.get("/customers", response_model=List[Customer]) def list_customers() -> List[Customer]: """Return all customers.""" return list(customers_store.values()) @app.get("/customers/{customer_id}", response_model=Customer) def get_customer(customer_id: int) -> Customer: """Return a single customer by id.""" if customer_id not in customers_store: raise HTTPException(status_code=404, detail="Customer not found") return customers_store[customer_id] @app.post("/customers", response_model=Customer) def create_customer(customer: Customer) -> Customer: """Create and return a new customer.""" global customers_counter customers_counter += 1 customers_store[customers_counter] = customer return customer @app.put("/customers/{customer_id}", response_model=Customer) def update_customer(customer_id: int, customer: Customer) -> Customer: """Update and return an existing customer.""" if customer_id not in customers_store: raise HTTPException(status_code=404, detail="Customer not found") customers_store[customer_id] = customer return customer @app.delete("/customers/{customer_id}", status_code=204) def delete_customer(customer_id: int) -> None: """Delete a customer by id.""" if customer_id not in customers_store: raise HTTPException(status_code=404, detail="Customer not found") del customers_store[customer_id] ```