-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
47 lines (32 loc) · 931 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from fastapi import FastAPI, Depends
from sqlalchemy.orm import Session
from database import get_db, Post, Base, engine
from pydantic import BaseModel
from typing import List
Base.metadata.create_all(bind=engine)
app = FastAPI()
class PostCreate(BaseModel):
title: str
content: str
published: bool = True
class PostResponse(BaseModel):
id: int
title: str
content: str
published: bool
class Config:
from_attributes = True
@app.get("/")
async def health_check():
return {"status": "healthy"}
@app.post("/posts", response_model=PostResponse)
def create_post(post: PostCreate, db: Session = Depends(get_db)):
new_post = Post(**post.dict())
db.add(new_post)
db.commit()
db.refresh(new_post)
return new_post
@app.get("/posts", response_model=List[PostResponse])
def get_posts(db: Session = Depends(get_db)):
posts = db.query(Post).all()
return posts