13 testiä, ForeignKey-relaatio, uniikki suomalainen testidata (Aleksis Kivi, Tove Jansson jne). Testattu Docker-kontissa.
111 lines
3.3 KiB
Python
111 lines
3.3 KiB
Python
"""FastAPI CRUD — kaksi endpoint-settiä, Author ja Post."""
|
|
|
|
from fastapi import FastAPI, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
|
|
from models import SessionLocal, Author, Post
|
|
from schemas import AuthorCreate, AuthorResponse, PostCreate, PostResponse
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
def get_db():
|
|
"""Tietokantasessio per pyyntö."""
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
# --- Author ---
|
|
|
|
|
|
@app.post("/authors/", response_model=AuthorResponse, status_code=201)
|
|
def create_author(item: AuthorCreate, db: Session = Depends(get_db)):
|
|
db_item = Author(**item.model_dump())
|
|
db.add(db_item)
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
|
|
@app.get("/authors/", response_model=list[AuthorResponse])
|
|
def list_authors(db: Session = Depends(get_db)):
|
|
return db.query(Author).all()
|
|
|
|
|
|
@app.get("/authors/{item_id}", response_model=AuthorResponse)
|
|
def get_author(item_id: int, db: Session = Depends(get_db)):
|
|
item = db.query(Author).filter(Author.id == item_id).first()
|
|
if not item:
|
|
raise HTTPException(status_code=404, detail="Author not found")
|
|
return item
|
|
|
|
|
|
@app.put("/authors/{item_id}", response_model=AuthorResponse)
|
|
def update_author(item_id: int, item: AuthorCreate, db: Session = Depends(get_db)):
|
|
db_item = db.query(Author).filter(Author.id == item_id).first()
|
|
if not db_item:
|
|
raise HTTPException(status_code=404, detail="Author not found")
|
|
for key, value in item.model_dump().items():
|
|
setattr(db_item, key, value)
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
|
|
@app.delete("/authors/{item_id}", status_code=204)
|
|
def delete_author(item_id: int, db: Session = Depends(get_db)):
|
|
db_item = db.query(Author).filter(Author.id == item_id).first()
|
|
if not db_item:
|
|
raise HTTPException(status_code=404, detail="Author not found")
|
|
db.delete(db_item)
|
|
db.commit()
|
|
|
|
|
|
# --- Post ---
|
|
|
|
|
|
@app.post("/posts/", response_model=PostResponse, status_code=201)
|
|
def create_post(item: PostCreate, db: Session = Depends(get_db)):
|
|
db_item = Post(**item.model_dump())
|
|
db.add(db_item)
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
|
|
@app.get("/posts/", response_model=list[PostResponse])
|
|
def list_posts(db: Session = Depends(get_db)):
|
|
return db.query(Post).all()
|
|
|
|
|
|
@app.get("/posts/{item_id}", response_model=PostResponse)
|
|
def get_post(item_id: int, db: Session = Depends(get_db)):
|
|
item = db.query(Post).filter(Post.id == item_id).first()
|
|
if not item:
|
|
raise HTTPException(status_code=404, detail="Post not found")
|
|
return item
|
|
|
|
|
|
@app.put("/posts/{item_id}", response_model=PostResponse)
|
|
def update_post(item_id: int, item: PostCreate, db: Session = Depends(get_db)):
|
|
db_item = db.query(Post).filter(Post.id == item_id).first()
|
|
if not db_item:
|
|
raise HTTPException(status_code=404, detail="Post not found")
|
|
for key, value in item.model_dump().items():
|
|
setattr(db_item, key, value)
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
|
|
@app.delete("/posts/{item_id}", status_code=204)
|
|
def delete_post(item_id: int, db: Session = Depends(get_db)):
|
|
db_item = db.query(Post).filter(Post.id == item_id).first()
|
|
if not db_item:
|
|
raise HTTPException(status_code=404, detail="Post not found")
|
|
db.delete(db_item)
|
|
db.commit()
|