-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
92 lines (86 loc) · 2.47 KB
/
backend.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
88
89
90
91
92
from flask import Flask, request, jsonify
app = Flask(__name__)
users = {
'users_list' :
[
{
'id' : 'xyz789',
'name' : 'Charlie',
'job': 'Janitor',
},
{
'id' : 'abc123',
'name': 'Mac',
'job': 'Bouncer',
},
{
'id' : 'ppp222',
'name': 'Mac',
'job': 'Professor',
},
{
'id' : 'yat999',
'name': 'Dee',
'job': 'Aspring actress',
},
{
'id' : 'zap555',
'name': 'Dennis',
'job': 'Bartender',
}
]
}
@app.route('/users', methods=['GET', 'POST', 'DELETE'])
def get_users():
if request.method == 'GET':
search_username = request.args.get('name')
search_job = request.args.get('job')
if search_username and search_job :
subdict = {'users_list' : []}
for user in users['users_list']:
if user['name'] == search_username and user['job'] == search_job :
subdict['users_list'].append(user)
return subdict
if search_username or search_job :
subdict = {'users_list' : []}
for user in users['users_list']:
if user['name'] == search_username or user['job'] == search_job :
subdict['users_list'].append(user)
return subdict
return users
elif request.method == 'POST':
userToAdd = request.get_json()
users['users_list'].append(userToAdd)
resp = jsonify(success=True)
#resp.status_code = 200 #optionally, you can always set a response code.
# 200 is the default code for a normal response
return resp
elif request.method == 'DELETE':
userToDelete = request.get_json()
users['users_list'].remove(userToDelete)
resp = jsonify(success=True)
return resp
@app.route('/users/<id>')
def get_user(id):
if id :
for user in users['users_list']:
if user['id'] == id:
return user
return ({})
return users
# @app.route('/users')
# def get_users():
# search_username = request.args.get('name') #accessing the value of parameter 'name'
# if search_username :
# subdict = {'users_list' : []}
# for user in users['users_list']:
# if user['name'] == search_username:
# subdict['users_list'].append(user)
# return subdict
# return users
# @app.route('/users')
# def get_users():
# return users
@app.route('/')
def hello_world():
return 'Hello, World!'