-
Notifications
You must be signed in to change notification settings - Fork 5
/
people.py
executable file
·87 lines (73 loc) · 2.08 KB
/
people.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from flask import make_response, abort
# JSON Data to serve with our API
PEOPLE = {
"ricky": {
"fname": "ricky",
"lname": "ponting",
"age": "11",
"favorite_color": "pink",
},
"chris": {
"fname": "chris",
"lname": "gayle",
"age": "29",
"favorite_color": "green",
},
"shane": {
"fname": "shane",
"lname": "warne",
"age": "33",
"favorite_color": "white",
},
}
def get_all():
return [PEOPLE[key] for key in sorted(PEOPLE.keys())]
def get_one(fname):
if fname in PEOPLE:
person = PEOPLE.get(fname)
else:
abort(
404, "Person with first name {fname} not found".format(fname=fname)
)
return person
def add(person):
fname = person.get("fname", None)
lname = person.get("lname", None)
age = person.get("age", None)
favorite_color = person.get("favorite_color", None)
if fname not in PEOPLE and fname is not None:
PEOPLE[fname] = {
"fname": fname,
"lname": lname,
"age": age,
"favorite_color": favorite_color
}
return make_response(
"Record {fname} successfully created".format(fname=fname), 201
)
else:
abort(
406,
"Peron with first name {fname} already exists".format(fname=fname),
)
def update(fname, person):
if fname in PEOPLE:
PEOPLE[fname]["fname"] = person.get("fname")
PEOPLE[fname]["lname"] = person.get("lname")
PEOPLE[fname]["age"] = person.get("age")
PEOPLE[fname]["favorite_color"] = person.get("favorite_color")
return PEOPLE[fname]
else:
abort(
404, "Person with first name {fname} not found".format(fname=fname)
)
def delete(fname):
if fname in PEOPLE:
del PEOPLE[fname]
return make_response(
"{fname} successfully deleted".format(fname=fname), 200
)
else:
abort(
404, "Person with first name {fname} not found".format(fname=fname)
)