Skip to content

Commit 22b6d81

Browse files
committed
feat: Solve issue hackdartstorm#175
1 parent fed9345 commit 22b6d81

1 file changed

Lines changed: 134 additions & 0 deletions

File tree

‎FastAPI/book_library_api/main.py‎

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
"""
2+
FastAPI Book Library Management API
3+
4+
A complete REST API for managing a book library.
5+
Supports CRUD operations: Create, Read, Update, Delete.
6+
7+
Run with: uvicorn main:app --reload
8+
Docs: http://127.0.0.1:8000/docs
9+
"""
10+
11+
from fastapi import FastAPI, HTTPException, status
12+
from pydantic import BaseModel, Field
13+
from typing import Optional
14+
15+
app = FastAPI(
16+
title="Book Library API",
17+
description="A simple API to manage a book library with CRUD operations",
18+
version="1.0.0",
19+
)
20+
21+
22+
# Pydantic models for request/response validation
23+
class BookCreate(BaseModel):
24+
"""Schema for creating a new book"""
25+
26+
title: str = Field(..., min_length=1, description="Book title")
27+
author: str = Field(..., min_length=1, description="Author of the book")
28+
year: int = Field(..., description="Publication year")
29+
30+
31+
class BookUpdate(BaseModel):
32+
"""Schema for updating an existing book"""
33+
34+
title: Optional[str] = Field(None, min_length=1)
35+
author: Optional[str] = Field(None, min_length=1)
36+
year: Optional[int] = Field(None, ge=1000, le=9999)
37+
38+
39+
class Book(BaseModel):
40+
"""Schema for book response"""
41+
42+
book_id: int
43+
title: str
44+
author: str
45+
year: int
46+
47+
48+
# In-memory data storage (replace with database in production)
49+
books_db: list[Book] = [
50+
Book(book_id=1, title="Satanás", author="Mario Mendoza", year=2002),
51+
Book(book_id=2, title="Satanás", author="Mario Mendoza", year=2002),
52+
Book(book_id=3, title="Satanás", author="Mario Mendoza", year=2002),
53+
]
54+
55+
# Auto-increment ID counter
56+
next_book_id = 4
57+
58+
59+
@app.get("/", tags=["Root"])
60+
async def root():
61+
"""Root endpoint - API information"""
62+
return {
63+
"message": "Welcome to Book Library API",
64+
"docs": "/docs",
65+
"endpoints": {"books": "/books"},
66+
}
67+
68+
69+
@app.get("/books", response_model=list[Book], tags=["Books"])
70+
async def list_books():
71+
"""Get all books"""
72+
return books_db
73+
74+
75+
@app.get("/books/{book_id}", response_model=Book, tags=["Books"])
76+
async def get_book(book_id: int):
77+
"""Get a specific book by ID"""
78+
for book in books_db:
79+
if book.book_id == book_id:
80+
return book
81+
raise HTTPException(
82+
status_code=status.HTTP_404_NOT_FOUND,
83+
detail=f"Book with ID {book_id} not found",
84+
)
85+
86+
87+
@app.post(
88+
"/books", response_model=Book, status_code=status.HTTP_201_CREATED, tags=["Books"]
89+
)
90+
async def create_book(book_data: BookCreate):
91+
"""Create a new book"""
92+
global next_book_id
93+
94+
new_book = Book(
95+
book_id=next_book_id,
96+
title=book_data.title,
97+
author=book_data.author,
98+
year=book_data.year,
99+
)
100+
books_db.append(new_book)
101+
next_book_id += 1
102+
103+
return new_book
104+
105+
106+
@app.put("/books/{book_id}", response_model=Book, tags=["Books"])
107+
async def update_book(book_id: int, book_update: BookUpdate):
108+
"""Update an existing book"""
109+
for book in books_db:
110+
if book.book_id == book_id:
111+
if book_update.title is not None:
112+
book.title = book_update.title
113+
if book_update.author is not None:
114+
book.author = book_update.author
115+
if book_update.year is not None:
116+
book.year = book_update.year
117+
return book
118+
raise HTTPException(
119+
status_code=status.HTTP_404_NOT_FOUND,
120+
detail=f"Book with ID {book_id} not found",
121+
)
122+
123+
124+
@app.delete("/books/{book_id}", status_code=status.HTTP_204_NO_CONTENT, tags=["Books"])
125+
async def delete_book(book_id: int):
126+
"""Delete a book"""
127+
for book_index, book in enumerate(books_db):
128+
if book.book_id == book_id:
129+
books_db.pop(book_index)
130+
return
131+
raise HTTPException(
132+
status_code=status.HTTP_404_NOT_FOUND,
133+
detail=f"Book with ID {book_id} not found",
134+
)

0 commit comments

Comments
 (0)