
- Added comprehensive book management with CRUD operations - Implemented inventory tracking with stock management and reservations - Created order management system with status tracking - Integrated Stripe payment processing with payment intents - Added SQLite database with SQLAlchemy ORM and Alembic migrations - Implemented health check and API documentation endpoints - Added comprehensive error handling and validation - Configured CORS middleware for frontend integration
85 lines
2.9 KiB
Python
85 lines
2.9 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from sqlalchemy.orm import Session
|
|
from typing import List, Optional
|
|
from app.db.session import get_db
|
|
from app.models import Book, Inventory
|
|
from app.schemas import BookCreate, BookUpdate, BookResponse
|
|
|
|
router = APIRouter(prefix="/books", tags=["books"])
|
|
|
|
@router.post("/", response_model=BookResponse)
|
|
def create_book(book: BookCreate, db: Session = Depends(get_db)):
|
|
db_book = db.query(Book).filter(Book.isbn == book.isbn).first()
|
|
if db_book:
|
|
raise HTTPException(status_code=400, detail="Book with this ISBN already exists")
|
|
|
|
db_book = Book(**book.model_dump())
|
|
db.add(db_book)
|
|
db.commit()
|
|
db.refresh(db_book)
|
|
|
|
inventory = Inventory(book_id=db_book.id, quantity=0)
|
|
db.add(inventory)
|
|
db.commit()
|
|
|
|
return db_book
|
|
|
|
@router.get("/", response_model=List[BookResponse])
|
|
def get_books(
|
|
skip: int = Query(0, ge=0),
|
|
limit: int = Query(100, ge=1, le=1000),
|
|
category: Optional[str] = None,
|
|
author: Optional[str] = None,
|
|
search: Optional[str] = None,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
query = db.query(Book).filter(Book.is_active)
|
|
|
|
if category:
|
|
query = query.filter(Book.category.ilike(f"%{category}%"))
|
|
if author:
|
|
query = query.filter(Book.author.ilike(f"%{author}%"))
|
|
if search:
|
|
query = query.filter(
|
|
(Book.title.ilike(f"%{search}%")) |
|
|
(Book.author.ilike(f"%{search}%")) |
|
|
(Book.description.ilike(f"%{search}%"))
|
|
)
|
|
|
|
return query.offset(skip).limit(limit).all()
|
|
|
|
@router.get("/{book_id}", response_model=BookResponse)
|
|
def get_book(book_id: int, db: Session = Depends(get_db)):
|
|
book = db.query(Book).filter(Book.id == book_id, Book.is_active).first()
|
|
if not book:
|
|
raise HTTPException(status_code=404, detail="Book not found")
|
|
return book
|
|
|
|
@router.put("/{book_id}", response_model=BookResponse)
|
|
def update_book(book_id: int, book_update: BookUpdate, db: Session = Depends(get_db)):
|
|
book = db.query(Book).filter(Book.id == book_id).first()
|
|
if not book:
|
|
raise HTTPException(status_code=404, detail="Book not found")
|
|
|
|
update_data = book_update.model_dump(exclude_unset=True)
|
|
if "isbn" in update_data:
|
|
existing_book = db.query(Book).filter(Book.isbn == update_data["isbn"], Book.id != book_id).first()
|
|
if existing_book:
|
|
raise HTTPException(status_code=400, detail="Book with this ISBN already exists")
|
|
|
|
for field, value in update_data.items():
|
|
setattr(book, field, value)
|
|
|
|
db.commit()
|
|
db.refresh(book)
|
|
return book
|
|
|
|
@router.delete("/{book_id}")
|
|
def delete_book(book_id: int, db: Session = Depends(get_db)):
|
|
book = db.query(Book).filter(Book.id == book_id).first()
|
|
if not book:
|
|
raise HTTPException(status_code=404, detail="Book not found")
|
|
|
|
book.is_active = False
|
|
db.commit()
|
|
return {"message": "Book deactivated successfully"} |