-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
42 lines (33 loc) · 1.08 KB
/
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
from fastapi import FastAPI
from models import Blog
app = FastAPI()
@app.get("/")
def index():
return {"data": "blog Home"}
blogs = list()
# Get all blog
@app.get("/blog")
async def get_blogs(limit=10,published:bool =True, sort: str | None = None):
if published==True:
return {"blogs": f'{limit} published blogs from db'}
elif published==False:
return {"blogs": f'{limit} unpublished blogs from db'}
elif published==False:
return {"blogs": f'{limit} published blogs from db'}
@app.post("/blog")
async def create_blog(blog: Blog):
return {'data':f'Blog with title {blog.title} created'}
@app.get('/blog/unpublished')
def unpublished():
return {'data': 'all unpublished blogs'}
# Get single blog
@app.get("/blog/{blog_id}")
async def show(blog_id: int):
for blog in blogs:
if blog.id==blog_id:
return blog
return {"message": f"No blogs found for id {blog_id}"}
# get comments for blog with id
@app.get('/blog/{id}/comments')
def comments(id: int, limit: int | None = None):
return {'comments': {'1','2'},'limit':limit}