1
0
Fork 0
master
Simon Moser vor 3 Jahren
Commit 8b7d77f76d

2
.gitignore vendored

@ -0,0 +1,2 @@
env
__pycache__

@ -0,0 +1,55 @@
from flask import Flask, jsonify, request, render_template
app = Flask(__name__)
incomes = [
{ 'description': 'engineer', 'amount': 5000 },
{ 'description': 'cleaner', 'amount': 1000 }
]
@app.route('/', methods=["GET"])
def frontend():
return render_template("index.html")
@app.route('/incomes', methods=['GET'])
def get_incomes():
return jsonify(incomes)
@app.route('/incomes/average', methods=["GET"])
def avg():
salaries = []
for inc in incomes:
salaries.append(int(inc["amount"]))
return str(sum(salaries)/len(salaries))
@app.route('/income', methods=['POST'])
def add_income():
if(request.headers.get('x-api-key') != "secretp4ssw0rd"):
return '', 401
incomes.append(request.get_json())
return '', 204
@app.route('/income/<desc>', methods=['GET'])
def get_income(desc):
try:
return jsonify([inc for inc in incomes if inc['description'] == desc])
except:
return '', 404
@app.route('/income/<desc>', methods=['DELETE'])
def del_income(desc):
if(request.headers.get('x-api-key') != "secretp4ssw0rd"):
return '', 401
for inc in incomes:
if inc['description'] == desc:
incomes.remove(inc)
return '', 204
@app.after_request
def apply_caching(response):
response.headers["Access-Control-Allow-Origin"] = "*"
response.headers["Access-Control-Allow-Methods"] = "*"
response.headers["Access-Control-Allow-Headers"] = "*"
return response

@ -0,0 +1,57 @@
<html>
<head>
<title>Income client</title>
</head>
<body>
<p>Please share your income for comparison!!</p>
<table id="all-incomes"><tr><td>Placeholder</td></tr></table>
Average: <input id="avg" readonly>
<br><br><br>
<div>
Description: <input id="description" value=""><br>
Amount: <input id="amount" value=""><br>
<button onclick="add_income()">Add Income</button>
</div>
<script>
function all_incomes() {
const xhttp = new XMLHttpRequest();
document.getElementById("all-incomes").innerHTML = "<tr><th>Description</th><th>Amount</th></tr>";
xhttp.onload = function() {
for (let job of JSON.parse(this.responseText)) {
if (job === null) {
continue;
}
document.getElementById("all-incomes").innerHTML += `<tr><td>${job["description"]}</td><td>${job["amount"]}</td></tr>`;
}
}
xhttp.open("GET", "/incomes", true);
xhttp.send();
const xhttp2 = new XMLHttpRequest();
xhttp2.onload = function() {
document.getElementById("avg").value = this.responseText;
}
xhttp2.open("GET", "/incomes/average", true);
xhttp2.send();
}
function add_income() {
const xhttp3 = new XMLHttpRequest();
xhttp3.onload = function() {
all_incomes();
document.getElementById("description").value = "";
document.getElementById("amount").value = "";
}
xhttp3.open("POST", "/income", true);
let job = {
description: `${document.getElementById("description").value}`,
amount: `${document.getElementById("amount").value}`
}
xhttp3.setRequestHeader("x-api-key", "secretp4ssw0rd");
xhttp3.setRequestHeader("Content-type", "application/json");
xhttp3.send(JSON.stringify(job));
}
all_incomes();
</script>
</body>
</html>
Laden…
Abbrechen
Speichern