|
| 1 | +# main.py |
| 2 | +from fastapi import FastAPI, Depends, HTTPException |
| 3 | +from sqlalchemy.orm import Session |
| 4 | +import models |
| 5 | +import schemas |
| 6 | +from database import SessionLocal, engine |
| 7 | +from typing import List |
| 8 | + |
| 9 | +# Initialize the app and create database tables |
| 10 | +app = FastAPI() |
| 11 | +models.Base.metadata.create_all(bind=engine) |
| 12 | + |
| 13 | +# Dependency for the database session |
| 14 | +def get_db(): |
| 15 | + db = SessionLocal() |
| 16 | + try: |
| 17 | + yield db |
| 18 | + finally: |
| 19 | + db.close() |
| 20 | + |
| 21 | +# CRUD Operations |
| 22 | + |
| 23 | +@app.post("/books/", response_model=schemas.Book) |
| 24 | +def create_book(book: schemas.BookCreate, db: Session = Depends(get_db)): |
| 25 | + db_book = models.Book(**book.dict()) |
| 26 | + db.add(db_book) |
| 27 | + db.commit() |
| 28 | + db.refresh(db_book) |
| 29 | + return db_book |
| 30 | + |
| 31 | +@app.get("/books/", response_model=List[schemas.Book]) |
| 32 | +def read_books(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)): |
| 33 | + books = db.query()(models.Book).offset(skip).limit(limit).scalars().all() |
| 34 | + return books |
| 35 | + |
| 36 | +@app.get("/books/{book_id}", response_model=schemas.Book) |
| 37 | +def read_book(book_id: int, db: Session = Depends(get_db)): |
| 38 | + book = db.query()(models.Book).where(models.Book.id == book_id).first() |
| 39 | + if book is None: |
| 40 | + raise HTTPException(status_code=404, detail="Book not found") |
| 41 | + return book |
| 42 | + |
| 43 | +@app.put("/books/{book_id}", response_model=schemas.Book) |
| 44 | +def update_book(book_id: int, book: schemas.BookCreate, db: Session = Depends(get_db)): |
| 45 | + db_book = db.query()(models.Book).where(models.Book.id == book_id).first() |
| 46 | + if db_book is None: |
| 47 | + raise HTTPException(status_code=404, detail="Book not found") |
| 48 | + for key, value in book.dict().items(): |
| 49 | + setattr(db_book, key, value) |
| 50 | + db.commit() |
| 51 | + db.refresh(db_book) |
| 52 | + return db_book |
| 53 | + |
| 54 | +@app.delete("/books/{book_id}", response_model=schemas.Book) |
| 55 | +def delete_book(book_id: int, db: Session = Depends(get_db)): |
| 56 | + db_book = db.query()(models.Book).where(models.Book.id == book_id).first() |
| 57 | + if db_book is None: |
| 58 | + raise HTTPException(status_code=404, detail="Book not found") |
| 59 | + db.delete(db_book) |
| 60 | + db.commit() |
| 61 | + return db_book |
| 62 | + |
| 63 | +@app.post("/publishers/", response_model=schemas.Publisher) |
| 64 | +def create_publisher(publisher: schemas.PublisherCreate, db: Session = Depends(get_db)): |
| 65 | + db_publisher = models.Publisher(**publisher.dict()) |
| 66 | + db.add(db_publisher) |
| 67 | + db.commit() |
| 68 | + db.refresh(db_publisher) |
| 69 | + return db_publisher |
| 70 | + |
| 71 | +@app.get("/publishers/", response_model=List[schemas.Publisher]) |
| 72 | +def read_publishers(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)): |
| 73 | + publishers = db.query()(models.Publisher).offset(skip).limit(limit).scalars().all() |
| 74 | + return publishers |
| 75 | + |
| 76 | +@app.get("/publishers/{publisher_id}", response_model=schemas.Publisher) |
| 77 | +def read_publisher(publisher_id: int, db: Session = Depends(get_db)): |
| 78 | + publisher = db.query()(models.Publisher).where(models.Publisher.id == publisher_id).first() |
| 79 | + if not publisher: |
| 80 | + raise HTTPException(status_code=404, detail="Publisher not found") |
| 81 | + return publisher |
| 82 | + |
| 83 | +@app.put("/publishers/{publisher_id}", response_model=schemas.Publisher) |
| 84 | +def update_publisher(publisher_id: int, publisher: schemas.PublisherCreate, db: Session = Depends(get_db)): |
| 85 | + db_publisher = db.query()(models.Publisher).where(models.Publisher.id == publisher_id).first() |
| 86 | + if not db_publisher: |
| 87 | + raise HTTPException(status_code=404, detail="Publisher not found") |
| 88 | + for key, value in publisher.dict().items(): |
| 89 | + setattr(db_publisher, key, value) |
| 90 | + db.commit() |
| 91 | + db.refresh(db_publisher) |
| 92 | + return db_publisher |
| 93 | + |
| 94 | +@app.delete("/publishers/{publisher_id}", response_model=schemas.Publisher) |
| 95 | +def delete_publisher(publisher_id: int, db: Session = Depends(get_db)): |
| 96 | + db_publisher = db.query()(models.Publisher).where(models.Publisher.id == publisher_id).first() |
| 97 | + if not db_publisher: |
| 98 | + raise HTTPException(status_code=404, detail="Publisher not found") |
| 99 | + db.delete(db_publisher) |
| 100 | + db.commit() |
| 101 | + return db_publisher |
| 102 | + |
0 commit comments