2021-03-28 18:23:05 -04:00
|
|
|
from flask import Flask, request
|
2021-03-04 10:23:21 -05:00
|
|
|
app = Flask(__name__)
|
|
|
|
|
2021-03-15 17:33:18 -04:00
|
|
|
import json
|
|
|
|
import sqlite3
|
|
|
|
import os
|
|
|
|
DB_PATH = os.path.join(os.path.dirname(__file__), 'links.db')
|
|
|
|
|
2021-03-04 10:23:21 -05:00
|
|
|
@app.route('/')
|
|
|
|
def hello_world():
|
2021-03-17 02:33:12 -04:00
|
|
|
return 'Hello from backend!'
|
2021-03-15 17:33:18 -04:00
|
|
|
|
|
|
|
def regen_JSON():
|
|
|
|
"""Gets links from DB and outputs them in JSON"""
|
|
|
|
con = sqlite3.connect(DB_PATH)
|
|
|
|
con.row_factory = sqlite3.Row
|
|
|
|
cur = con.cursor()
|
|
|
|
cur.execute('SELECT url, name FROM links ORDER BY position')
|
|
|
|
|
|
|
|
links_list = []
|
|
|
|
for row in cur.fetchall():
|
|
|
|
d = dict(zip(row.keys(), row))
|
|
|
|
links_list.append(d)
|
|
|
|
links_json = json.dumps(links_list, indent=4)
|
|
|
|
|
|
|
|
con.close()
|
|
|
|
return links_json
|
2021-03-04 10:23:21 -05:00
|
|
|
|
2021-03-28 18:23:05 -04:00
|
|
|
@app.route('/clicks', methods=['POST'])
|
|
|
|
def update_clicks():
|
|
|
|
if ('url' not in request.json or 'name' not in request.json):
|
|
|
|
return 'url and/or name not found', 500
|
|
|
|
else:
|
|
|
|
url_id = request.json['url']
|
|
|
|
url_name = request.json['name']
|
|
|
|
con = sqlite3.connect(DB_PATH)
|
|
|
|
cur = con.cursor()
|
|
|
|
cur.execute("UPDATE links SET clicks=clicks + 1 WHERE url=? AND name=?", [url_id, url_name])
|
|
|
|
con.commit()
|
|
|
|
con.close()
|
|
|
|
return 'ok'
|
|
|
|
|
2021-03-04 10:23:21 -05:00
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(debug=True)
|