128 lines
4.7 KiB
Python
128 lines
4.7 KiB
Python
from fastapi import FastAPI, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from models import Base, engine, SessionLocal, User, Article, Comment
|
|
from schemas import UserCreate, ArticleCreate, CommentCreate, UserResponse, ArticleResponse, CommentResponse
|
|
|
|
app = FastAPI()
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
@app.post("/users/", response_model=UserResponse, status_code=201)
|
|
def create_user(item: UserCreate, db: Session = Depends(get_db)):
|
|
db_item = User(**item.model_dump())
|
|
db.add(db_item)
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
@app.get("/users/", response_model=list[UserResponse])
|
|
def list_users(db: Session = Depends(get_db)):
|
|
return db.query(User).all()
|
|
|
|
@app.get("/users/{item_id}", response_model=UserResponse)
|
|
def get_user(item_id: int, db: Session = Depends(get_db)):
|
|
item = db.query(User).filter(User.id == item_id).first()
|
|
if not item:
|
|
raise HTTPException(status_code=404, detail="User not found")
|
|
return item
|
|
|
|
@app.put("/users/{item_id}", response_model=UserResponse)
|
|
def update_user(item_id: int, item: UserCreate, db: Session = Depends(get_db)):
|
|
db_item = db.query(User).filter(User.id == item_id).first()
|
|
if not db_item:
|
|
raise HTTPException(status_code=404, detail="User 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("/users/{item_id}", status_code=204)
|
|
def delete_user(item_id: int, db: Session = Depends(get_db)):
|
|
db_item = db.query(User).filter(User.id == item_id).first()
|
|
if not db_item:
|
|
raise HTTPException(status_code=404, detail="User not found")
|
|
db.delete(db_item)
|
|
db.commit()
|
|
|
|
@app.post("/articles/", response_model=ArticleResponse, status_code=201)
|
|
def create_article(item: ArticleCreate, db: Session = Depends(get_db)):
|
|
db_item = Article(**item.model_dump())
|
|
db.add(db_item)
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
@app.get("/articles/", response_model=list[ArticleResponse])
|
|
def list_articles(db: Session = Depends(get_db)):
|
|
return db.query(Article).all()
|
|
|
|
@app.get("/articles/{item_id}", response_model=ArticleResponse)
|
|
def get_article(item_id: int, db: Session = Depends(get_db)):
|
|
item = db.query(Article).filter(Article.id == item_id).first()
|
|
if not item:
|
|
raise HTTPException(status_code=404, detail="Article not found")
|
|
return item
|
|
|
|
@app.put("/articles/{item_id}", response_model=ArticleResponse)
|
|
def update_article(item_id: int, item: ArticleCreate, db: Session = Depends(get_db)):
|
|
db_item = db.query(Article).filter(Article.id == item_id).first()
|
|
if not db_item:
|
|
raise HTTPException(status_code=404, detail="Article 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("/articles/{item_id}", status_code=204)
|
|
def delete_article(item_id: int, db: Session = Depends(get_db)):
|
|
db_item = db.query(Article).filter(Article.id == item_id).first()
|
|
if not db_item:
|
|
raise HTTPException(status_code=404, detail="Article not found")
|
|
db.delete(db_item)
|
|
db.commit()
|
|
|
|
@app.post("/comments/", response_model=CommentResponse, status_code=201)
|
|
def create_comment(item: CommentCreate, db: Session = Depends(get_db)):
|
|
db_item = Comment(**item.model_dump())
|
|
db.add(db_item)
|
|
db.commit()
|
|
db.refresh(db_item)
|
|
return db_item
|
|
|
|
@app.get("/comments/", response_model=list[CommentResponse])
|
|
def list_comments(db: Session = Depends(get_db)):
|
|
return db.query(Comment).all()
|
|
|
|
@app.get("/comments/{item_id}", response_model=CommentResponse)
|
|
def get_comment(item_id: int, db: Session = Depends(get_db)):
|
|
item = db.query(Comment).filter(Comment.id == item_id).first()
|
|
if not item:
|
|
raise HTTPException(status_code=404, detail="Comment not found")
|
|
return item
|
|
|
|
@app.put("/comments/{item_id}", response_model=CommentResponse)
|
|
def update_comment(item_id: int, item: CommentCreate, db: Session = Depends(get_db)):
|
|
db_item = db.query(Comment).filter(Comment.id == item_id).first()
|
|
if not db_item:
|
|
raise HTTPException(status_code=404, detail="Comment 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("/comments/{item_id}", status_code=204)
|
|
def delete_comment(item_id: int, db: Session = Depends(get_db)):
|
|
db_item = db.query(Comment).filter(Comment.id == item_id).first()
|
|
if not db_item:
|
|
raise HTTPException(status_code=404, detail="Comment not found")
|
|
db.delete(db_item)
|
|
db.commit()
|