-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
62 lines (53 loc) · 1.62 KB
/
server.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
55
56
57
58
59
60
61
62
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
next_post_id: int = 0
app = FastAPI()
db = {"posts": []}
class Post(BaseModel):
title: str
body: str
@app.get("/")
async def root():
return {"message": "This is the C4C Backend Workshop Backend!"}
# Returns all posts
@app.get("/posts")
def get_posts():
return db['posts']
# Creates a new entry for Post
# Will raise exception if body parameters are invalid
@app.post("/posts")
def add_post(new_post: Post):
if (new_post.title.strip() == '' or new_post.body.strip() == ''):
raise HTTPException(status_code=400, detail="Invalid input")
else:
global next_post_id
id = next_post_id
db['posts'].append({ 'id': id, 'title': new_post.title, 'body': new_post.body })
next_post_id += 1
return id
# Removes a post from the 'database'
@app.delete("/posts/{post_id}")
def delete_post(post_id: int):
posts = db['posts']
for post in posts:
if post_id == post['id']:
db['posts'].remove(post)
return 'OK'
raise HTTPException(status_code=404, detail="Item not found")
# Retrieves a single post from the 'database'
@app.get("/posts/{post_id}")
def get_post(post_id: int):
posts = db['posts']
for post in posts:
if post_id == post['id']:
return post
raise HTTPException(status_code=404, detail="Item not found")
# Exercises
# Updates a post in the 'database'
@app.put("/posts/{post_id}")
def update_post(post_id: int, updated_post: Post):
pass
# Deletes all posts from the 'database'
@app.delete("/posts")
def delete_all_posts():
pass