-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
54 lines (44 loc) · 1.05 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
43
44
45
46
47
48
49
50
51
52
53
54
from random import randrange
from typing import Optional
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Post(BaseModel):
title: str
content: str
published: bool = True
rating: Optional[int] = None
my_posts = [
{
"title": "title of post 1",
"content": "content of post 1",
"id": 1
},{
"title": "title of post 1",
"content": "content of post 2",
"id": 2
}
]
def find_post(id):
for p in my_posts:
if p["id"] == id:
return p
@app.get("/")
async def root():
return {"test-message": "Hello World"}
@app.get("/posts")
async def get_posts():
return {"data": my_posts}
@app.post("/posts")
async def create_posts(post: Post):
post_dict = post.dict()
post_dict['id'] = randrange(0, 1000000)
my_posts.append(post_dict)
return {"message": post_dict}
@app.get("/posts/{id}")
async def get_post(id):
post = find_post(int(id))
return {
"message": "You selected post {id}",
"post-detail": post
}