-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
66 lines (44 loc) · 1.33 KB
/
Copy pathmain.py
File metadata and controls
66 lines (44 loc) · 1.33 KB
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
63
64
65
66
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
app = FastAPI(title="FastAPI first steps", version="0.0.1")
@app.get("/")
async def root():
return {"message": "Hello World"}
@app.get("/foo")
async def root():
return {"message": "bar"}
class Word(BaseModel):
word: str
requested: int = 0
word_records = [
Word(word="Apfel"),
Word(word="Auto"),
Word(word="Berlin")]
@app.get("/words")
async def get_all_words():
return word_records
@app.get("/words/{word}")
async def get_one_word(word: str):
for record in word_records:
if record.word == word:
record.requested += 1
return record
raise HTTPException(status_code=404, detail="Word not found")
@app.post("/words")
async def create_word(word: Word):
word_records.append(word)
return word
@app.delete("/words/{word}")
async def delete_word(word: str):
global word_records
deleted_word_count = 0
word_records_new = []
for record in word_records:
if record.word == word:
deleted_word_count += 1
continue
word_records_new.append(record)
word_records = word_records_new
if deleted_word_count == 0:
raise HTTPException(status_code=404, detail="Word not found")
return {"message": f"{deleted_word_count} record(s) deleted"}