Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions challenges/backend/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from flask import Flask, request
from flask_restful import Resource,Api,reqparse, abort
import requests

app = Flask(__name__)
api = Api(app)

NUMBERLIST = {"list":[]}

class ListProcess(Resource):
def get(self):
#Sort before returning
if(len(NUMBERLIST['list']) > 0):
NUMBERLIST['list'].sort()
return NUMBERLIST
else:
abort(404, message="No lists found.")

def post(self):
numlist = str(request.data).strip()
numlist = numlist.split(sep=',')
templist = []
if(len(numlist) == 500):
allnumbers = True
for entry in numlist:
entry = entry.replace('b\'','')
entry = entry.replace('\'','')
if(entry.isdigit() == False):
allnumbers = False
else:
templist.append(int(entry))
if(allnumbers == True):
NUMBERLIST['list'] = templist
return NUMBERLIST
else:
abort(400,message="Bad input. All entries must be numbers.")
else:
abort(400, message="Bad input. Length must be 500.")

def patch(self):
entry = str(request.data).strip()
entry = entry.replace('b\'', '')
entry = entry.replace('\'', '')
if(entry.isdigit()):
NUMBERLIST['list'].append(int(entry))
NUMBERLIST['list'].sort()
return NUMBERLIST
else:
abort(400,message="Bad input. All entries must be numbers.")

api.add_resource(ListProcess, "/data/",endpoint='data')

if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)
16 changes: 16 additions & 0 deletions challenges/backend/tester.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import requests
import random
payload = ''
for i in range(499):
temp = random.randint(0,500)
payload = payload + str(temp) + ','
payload = payload + str(random.randint(0,500))
patchload = str(random.randint(0,500))
r=requests.get("http://localhost:5000/data/")
print("get", r.text)
r=requests.post("http://localhost:5000/data/",data=payload)
print("post",r.text)
r=requests.get("http://localhost:5000/data/")
print("get", r.text)
r=requests.patch("http://localhost:5000/data/",data=patchload)
print("patch", r.text)
Binary file added challenges/database/ER_Diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions challenges/database/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import sqlite3
import sys
#This program should only be run once to construct the database.
if(len(sys.argv) == 2):
handle = sys.argv[1]
con = sqlite3.connect(handle)
cur = con.cursor()
query = '''CREATE TABLE record
(id smallint,
basic int,
advanced int,
plan bool,
PRIMARY KEY(id))'''
print(query)
cur.execute(query)
query = '''CREATE TABLE customer
(name varchar(20),
email varchar(50),
workphone varchar(20),
cellphone varchar(20),
address tinytext,
order_id smallint,
PRIMARY KEY(name,email),
FOREIGN KEY(order_id) REFERENCES record(id))'''
print(query)
cur.execute(query)
con.commit()
con.close()
else:
print("Expected input: python3 database.py <database file>")
40 changes: 40 additions & 0 deletions challenges/database/db_migrate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import sqlite3
import json
import sys
json_dict = {}
if(len(sys.argv) == 3):
with open (sys.argv[1], 'r') as handle:
json_dict = json.load(handle)
con = sqlite3.connect(sys.argv[2])
cur = con.cursor()
values = ('(' + str(json_dict['record_id']) + ',' +
str(json_dict['basic_order']) + ',' +
str(json_dict['advanced_order']) + ',' +
str(int(json_dict['protection_plan'])) + ')')
query = "INSERT INTO record " + values
print(query)
#This is the query, but doing this directly is insecure. Instead, we do this:
cur.execute("INSERT INTO record values (?,?,?,?)",(
json_dict['record_id'],
json_dict['basic_order'],
json_dict['advanced_order'],
int(json_dict['protection_plan'])))
values = ('(\'' + json_dict['name'] + '\',\'' +
json_dict['email'] + '\',\'' +
json_dict['work'] + '\',\'' +
json_dict['cell'] + '\',\'' +
json_dict['address'] + '\',' +
str(json_dict['record_id']) + ')')
query = "INSERT INTO custormer " + values
print(query)
cur.execute("INSERT INTO customer values (?,?,?,?,?,?)",(
json_dict['name'],
json_dict['email'],
json_dict['work'],
json_dict['cell'],
json_dict['address'],
json_dict['record_id']))
con.commit()
con.close()
else:
print("Expected input: python3 db_migrate.py <json file> <database file>")
1 change: 1 addition & 0 deletions challenges/database/record.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"record_id": 1234, "name": "Joe Smith", "cell": "405.868.5309", "work": "123.123.1234", "email": "[email protected]", "address": "123 Vic Way, Dallas TX 75001", "basic_order": 37, "advanced_order": 12, "protection_plan": true}
Binary file added challenges/frontend/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions challenges/frontend/frontend.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="icon" type="image/png" href="favicon.png">
<script src="https://www.google.com/recaptcha/api.js"></script>
</head>
<body>
<h2>Erik's HTML Company</h2>
<h3>Contact Us</h3>
<form id="contact-form" onsubmit="onSubmit()">

<label for="fname">First Name</label><br>
<input type="text" id="fname" name="fname" value=""><br>
<label for="lname">Last Name</label><br>
<input type="text" id="lname" name="lname" value=""><br>
<label for="pnum">Phone Number</label><br>
<input type="text" id="pnum" name="pnum" value=""><br>
</form>
<script>
function onSubmit(token)
{
console.log("Results");
document.getElementById("contact-form").submit();
console.log("First Name:" + document.getElementById("fname").value);
console.log("Last Name:" + document.getElementById("lname").value);
console.log("Phone Number:" + document.getElementById("pnum").value);
}
</script>
<button type="submit" form="contact-form" value="submit">Submit</button>
</body>
</html>