1010app = FastAPI ()
1111models .Base .metadata .create_all (bind = engine )
1212
13+
1314# Dependency for the database session
1415def get_db ():
1516 db = SessionLocal ()
@@ -18,15 +19,18 @@ def get_db():
1819 finally :
1920 db .close ()
2021
22+
2123# Utility Functions
2224def get_book_or_404 (book_id : int , db : Session ):
2325 book = db .query (models .Book ).filter (models .Book .id == book_id ).first ()
2426 if not book :
2527 raise HTTPException (status_code = 404 , detail = "Book not found" )
2628 return book
2729
30+
2831# CRUD Operations
2932
33+
3034@app .post ("/books/" , response_model = schemas .Book )
3135def create_book (book : schemas .BookCreate , db : Session = Depends (get_db )):
3236 db_book = models .Book (** book .dict ())
@@ -35,18 +39,21 @@ def create_book(book: schemas.BookCreate, db: Session = Depends(get_db)):
3539 db .refresh (db_book )
3640 return db_book
3741
42+
3843@app .get ("/books/" , response_model = List [schemas .Book ])
3944def read_books (skip : int = 0 , limit : int = 10 , db : Session = Depends (get_db )):
4045 books = db .query (models .Book ).offset (skip ).limit (limit ).all ()
4146 return books
4247
48+
4349@app .get ("/books/{book_id}" , response_model = schemas .Book )
4450def read_book (book_id : int , db : Session = Depends (get_db )):
4551 book = db .query (models .Book ).filter (models .Book .id == book_id ).first ()
4652 if book is None :
4753 raise HTTPException (status_code = 404 , detail = "Book not found" )
4854 return book
4955
56+
5057@app .put ("/books/{book_id}" , response_model = schemas .Book )
5158def update_book (book_id : int , book : schemas .BookCreate , db : Session = Depends (get_db )):
5259 db_book = db .query (models .Book ).filter (models .Book .id == book_id ).first ()
@@ -58,6 +65,7 @@ def update_book(book_id: int, book: schemas.BookCreate, db: Session = Depends(ge
5865 db .refresh (db_book )
5966 return db_book
6067
68+
6169@app .delete ("/books/{book_id}" , response_model = schemas .Book )
6270def delete_book (book_id : int , db : Session = Depends (get_db )):
6371 db_book = db .query (models .Book ).filter (models .Book .id == book_id ).first ()
@@ -67,6 +75,7 @@ def delete_book(book_id: int, db: Session = Depends(get_db)):
6775 db .commit ()
6876 return db_book
6977
78+
7079@app .post ("/publishers/" , response_model = schemas .Publisher )
7180def create_publisher (publisher : schemas .PublisherCreate , db : Session = Depends (get_db )):
7281 db_publisher = models .Publisher (** publisher .dict ())
@@ -75,18 +84,21 @@ def create_publisher(publisher: schemas.PublisherCreate, db: Session = Depends(g
7584 db .refresh (db_publisher )
7685 return db_publisher
7786
87+
7888@app .get ("/publishers/" , response_model = List [schemas .Publisher ])
7989def read_publishers (skip : int = 0 , limit : int = 10 , db : Session = Depends (get_db )):
8090 publishers = db .query (models .Publisher ).offset (skip ).limit (limit ).all ()
8191 return publishers
8292
93+
8394@app .get ("/publishers/{publisher_id}" , response_model = schemas .Publisher )
8495def read_publisher (publisher_id : int , db : Session = Depends (get_db )):
8596 publisher = db .query (models .Publisher ).filter (models .Publisher .id == publisher_id ).first ()
8697 if not publisher :
8798 raise HTTPException (status_code = 404 , detail = "Publisher not found" )
8899 return publisher
89100
101+
90102@app .put ("/publishers/{publisher_id}" , response_model = schemas .Publisher )
91103def update_publisher (publisher_id : int , publisher : schemas .PublisherCreate , db : Session = Depends (get_db )):
92104 db_publisher = db .query (models .Publisher ).filter (models .Publisher .id == publisher_id ).first ()
@@ -98,6 +110,7 @@ def update_publisher(publisher_id: int, publisher: schemas.PublisherCreate, db:
98110 db .refresh (db_publisher )
99111 return db_publisher
100112
113+
101114@app .delete ("/publishers/{publisher_id}" , response_model = schemas .Publisher )
102115def delete_publisher (publisher_id : int , db : Session = Depends (get_db )):
103116 db_publisher = db .query (models .Publisher ).filter (models .Publisher .id == publisher_id ).first ()
@@ -106,4 +119,3 @@ def delete_publisher(publisher_id: int, db: Session = Depends(get_db)):
106119 db .delete (db_publisher )
107120 db .commit ()
108121 return db_publisher
109-
0 commit comments