From 64911a52b67e4918d4bbf3197afda7381ba2b91f Mon Sep 17 00:00:00 2001 From: wowlikon Date: Tue, 24 Jun 2025 13:37:19 +0300 Subject: [PATCH] Deleted unused import and corrected typing --- library_service/routers/authors.py | 16 ++++++++++++++++ library_service/routers/books.py | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/library_service/routers/authors.py b/library_service/routers/authors.py index c47a033..b23042a 100644 --- a/library_service/routers/authors.py +++ b/library_service/routers/authors.py @@ -1,5 +1,6 @@ from fastapi import APIRouter, Depends, HTTPException from sqlmodel import Session, select +from typing import List from library_service.settings import get_session from library_service.models.db import Author, AuthorBookLink, Book, AuthorWithBooks @@ -78,3 +79,18 @@ def delete_author(author_id: int, session: Session = Depends(get_session)): session.delete(author) session.commit() return author_read + +# Get all books for an author +@router.get("/{author_id}/books/", response_model=List[BookRead]) +def get_books_for_author(author_id: int, session: Session = Depends(get_session)): + author = session.get(Author, author_id) + if not author: + raise HTTPException(status_code=404, detail="Author not found") + + books = session.exec( + select(Book) + .join(AuthorBookLink) + .where(AuthorBookLink.author_id == author_id) + ).all() + + return [BookRead(**book.model_dump()) for book in books] diff --git a/library_service/routers/books.py b/library_service/routers/books.py index 2c77e48..635927e 100644 --- a/library_service/routers/books.py +++ b/library_service/routers/books.py @@ -1,6 +1,6 @@ from fastapi import APIRouter, Depends, HTTPException from sqlmodel import Session, select -from typing import Deque, List +from typing import List from library_service.settings import get_session from library_service.models.db import Book, Author, AuthorBookLink, BookWithAuthors