-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
62 lines (44 loc) · 1.57 KB
/
app.py
File metadata and controls
62 lines (44 loc) · 1.57 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
from flask import Flask, request, jsonify
import sqlite3
app = Flask(__name__)
def dictionary_factory(db, row):
return {
"first_name": row[0],
"last_name": row[1],
"state": row[2],
"phonenumber": row[3],
"email": row[4]
}
def search_phone_book(**kwargs):
db = sqlite3.connect("phonebook.db")
search_first_name = kwargs.get("first_name")
search_last_name = kwargs.get("last_name")
search_state = kwargs.get("state")
if not any([search_first_name, search_last_name, search_state]):
return []
query = "SELECT * FROM people WHERE "
query_arguments = []
if search_first_name:
query_arguments.append(f"first_name='{search_first_name}'")
if search_last_name:
query_arguments.append(f"last_name='{search_last_name}'")
if search_state:
query_arguments.append(f"state='{search_state}'")
query += " AND ".join(query_arguments)
db.row_factory = dictionary_factory
return list(db.execute(query))
@app.route("/search/", methods=['GET'])
def search_phonebook():
first_name = request.args.get("firstName")
last_name = request.args.get("lastName")
state = request.args.get("state")
if not any([first_name, last_name, state]):
return jsonify({"error": "At least one of the three fields must be filled."}), 400
search_results = search_phone_book(
first_name=first_name,
last_name=last_name,
state=state
)
return jsonify(search_results)
if __name__ == "__main__":
app.run(debug=True, port=8080, threaded=True)