Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Travis Dula - all three #85

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
30 changes: 30 additions & 0 deletions challenges/backend/backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from flask import Flask, jsonify, request
import json
app = Flask(__name__)

data_list = []

# TODO: better (more verbose) errors
@app.route('/data', methods=['GET', 'POST', 'PATCH'])

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you decide on what methods are appropriate here? For example would it make sense to include PUT or DELETE?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to implement only the requested methods, but with the current model I don't think PUT makes sense. If we had given the ability to create multiple data records, then PUT would be used to create new ones or overwrite existing ones. DELETE could be used in the current model to remove the only list.

def data_api():
global data_list
if request.method == 'GET':
return jsonify(data_list)
elif request.method == 'POST':
in_json = request.get_json()
is_num = lambda x: type(x) (int, float)
if len(in_json) == 500 and all(map(is_num, in_json)):
data_list = list(sorted(in_json))
return jsonify(data_list)
else:
return 'Bad request', 400
elif request.method == 'PATCH':
try:
data_list = list(sorted([int(request.data)] + data_list))
return jsonify(data_list)
except BaseException as e:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you decide that BaseException is the correct exception to use?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not ideal to handle exceptions in this manner, but I also cannot think of a situation in which it will behave in an unintended manner. I do notice now though that I didn't do sufficient testing with floats, because those won't work properly here.

print(e)
return 'Bad request', 400

if __name__ == '__main__':
app.run(debug=True)
3 changes: 3 additions & 0 deletions challenges/database/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Why?
I have very little prior database knowledge, so this is my best estimate after a short time of learning.
Thinking of it in terms of objects, the only two "objects" I could think of were `Customer` and `Order`, so I split the database into tables for each.
14 changes: 14 additions & 0 deletions challenges/database/generate_input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import json
with open('in.json', 'w') as f:
f.write(json.dumps({
1234: {
'name': 'Joe Smith',
'cellPhone': '405.867.5309',
'workPhone': '123.123.1234',
'email': '[email protected]',
'address': '123 Vic Way, Dallas TX 75001',
'basicWidgetOrder': 37,
'advancedWidgetOrder': 12,
'protectionPlan': True
}
}))
12 changes: 12 additions & 0 deletions challenges/database/in.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"1234": {
"name": "Joe Smith",
"cellPhone": "405.867.5309",
"workPhone": "123.123.1234",
"email": "[email protected]",
"address": "123 Vic Way, Dallas TX 75001",
"basicWidgetOrder": 37,
"advancedWidgetOrder": 12,
"protectionPlan": true
}
}
38 changes: 38 additions & 0 deletions challenges/database/jsontosql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import json
#import sqlite3

def main():
# potential improvements include using command line arguments
# (or another method) to pick the input file
input_file = 'in.json'
with open(input_file, 'r') as f:
old_database = json.loads(f.read())
#new_database = sqlite3.connect('output.db')
create='''CREATE TABLE Customer (
customerID int,
name varchar(255),
cellPhone varchar(255),
workPhone varchar(255),
email varchar(255),
address varchar(255)
);'''
#new_database.execute(create)
print(create)
create2='''CREATE TABLE Order (
customerID int,
basicWidgetOrder int,
advancedWidgetOrder int,
protectionPlan int,
);'''
print(create2)
for obj, values in old_database.items():
print(f"\
INSERT INTO Customer VALUES({obj}, \'{values['name']}\',\
\'{values['cellPhone']}\', \'{values['workPhone']}\',\
\'{values['email']}\', \'{values['address']}\');")
print(f"\
INSERT INTO Order VALUES({obj}, {values['basicWidgetOrder']}, \
{values['advancedWidgetOrder']}, {values['protectionPlan']});")

if __name__ == '__main__':
main()
Binary file added challenges/database/relational_db.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions challenges/frontend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarn.lock
5 changes: 5 additions & 0 deletions challenges/frontend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# How to run
```sh
yarn run http-server
```

Binary file added challenges/frontend/favicon-32x32.ico
Binary file not shown.
70 changes: 70 additions & 0 deletions challenges/frontend/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<!DOCTYPE html>
<html>

<head>
<title></title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<!--<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />-->
<link href="output.css" rel="stylesheet" />

<!--https://www.reddit.com/r/Thinking/comments/85f0vm/a_three_eyed_thinking_emoji_i_made/ -->
<!--credit given where it is due -->
<link rel="icon" type="image/png" href="favicon-32x32.ico" sizes="32x32">

<script src="https://www.google.com/recaptcha/api.js"></script>

<script>
function onSubmit(token) {
document.getElementById("form").submit();
console.log(document.getElementById("name").value);
console.log(document.getElementById("email").value);
console.log(document.getElementById("message").value);
}

</script>


</head>

<body class="bg-gray-700">
<div class="sm:mx-auto sm:container justify-center md:max-w-md m-4 flex flex-col font-mono">
<header class="text-gray-100 font-black text-4xl text-center">
big thonk inc.
</header>
<section class="leading-normal">
<form id="form" class="text-gray-100 font-extrabold text-2xl mt-3">
contact us.
<div class="text-lg mt-2">
<div class="text-gray-300 flex flex-row font-normal mb-1">
<label for="name" class="flex-grow text-left">name.</label>
<input class="text-gray-700 bg-gray-200 border-gray-500 rounded-sm md:w-80" type="text"
id="name" name="name" required>
</div>
<div class="text-gray-300 flex flex-row font-normal mb-1">
<label for="email" class="flex-grow text-left">email.</label>
<input class="text-gray-700 bg-gray-200 border-gray-500 rounded-sm md:w-80" type="email"
id="email" name="email" required>
</div>
<div class="text-gray-300 flex flex-row font-normal mb-2">
<label for="message" class="flex-grow text-left">message.</label>
<input class="text-gray-700 bg-gray-200 border-gray-500 rounded-sm md:w-80" type="text"
id="message" name="message" required>
</div>
<div class="text-gray-300 flex justify-center font-normal">
<button
class="g-recaptcha text-gray-700 bg-gray-200 hover:bg-gray-400 border-gray-500 rounded-sm w-20"
data-sitekey="6LeHFpcaAAAAAAZzV0CkeXZEQuoS1o8zi7AHRrei" data-action='submit'
data-callback='onSubmit' type="button" id="button" name="button" required>submit.</input>
</div>
</div>
</form>
</section>
<footer class="mt-4 text-xs text-center">
I'm not sure if I still need a footer
</footer>
</div>
</body>

</html>
5 changes: 5 additions & 0 deletions challenges/frontend/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@tailwind base;

@tailwind components;

@tailwind utilities;
Loading