forked from 8BitJonny/CS50-Final-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackend.py
40 lines (35 loc) · 1.51 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
from flask import Flask, request, jsonify
import sqlite3
app = Flask(__name__)
@app.route("/index")
def index():
# Build Sql Connection, get all rows and return the json object of it
with sqlite3.connect("company.db") as con:
cursor = con.cursor()
cursor.execute("SELECT * FROM employees")
db_employees = cursor.fetchall()
return jsonify(db_employees)
@app.route("/add", methods=["POST"])
def add():
# Build Sql Connection, insert the values that come from the querystring
with sqlite3.connect("company.db") as con:
cursor = con.cursor()
cursor.execute("INSERT INTO employees (firstname, lastname, birthdate, age, sex, workload_per_week, work_group) VALUES (?,?,?,?,?,?,?)",
(request.args.get("firstName", ""),
request.args.get("lastName", ""),
request.args.get("birthdate", ""),
request.args.get("age", ""),
request.args.get("gender", ""),
int(request.args.get("workload", "")),
request.args.get("department", "")))
return "200"
@app.route("/delete", methods=["POST"])
def delete():
# Build Sql Connection, get UID from querystring and execute Delete Statements
if request.args.get("uid", ""):
with sqlite3.connect("company.db") as con:
cursor = con.cursor()
cursor.execute("DELETE FROM employees WHERE id = (?)", (request.args.get("uid",""),))
return "200"
if __name__ == "__main__":
app.run(host="127.0.0.1", port=5001, debug=True)