-
Notifications
You must be signed in to change notification settings - Fork 2
/
tasks.py
55 lines (45 loc) · 1.54 KB
/
tasks.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import sys
import json
import mariadb
import os
import flask
from flask import request
from flask import Blueprint
from dotenv import load_dotenv
load_dotenv()
tasks = Blueprint('tasks', __name__)
config = {
'host': os.getenv("DB_HOST"),
'port': int(os.getenv("DB_PORT")),
'user': os.getenv("DB_USER"),
'password': os.getenv("DB_PASS"),
'database': os.getenv("DB_NAME"),
'ssl': 1,
'autocommit': True
}
@tasks.route('/api/tasks', methods=['GET','POST','PUT','DELETE'])
def index():
conn = mariadb.connect(**config)
cur = conn.cursor()
json_data = []
if request.method == 'GET':
cur.execute("select * from tasks order by id desc")
row_headers=[x[0] for x in cur.description]
rv = cur.fetchall()
for result in rv:
json_data.append(dict(zip(row_headers,result)))
if request.method == 'POST':
description = request.json['description']
cur.execute("insert into tasks (description) values (?)",[description])
json_data = { 'success': True }
if request.method == 'PUT':
id = request.json['id']
description = request.json['description']
completed = request.json['completed']
cur.execute("update tasks set description = ?, completed = ? where id = ?", [description,completed,id])
json_data = { 'success': True }
if request.method == 'DELETE':
id = request.args.get('id')
cur.execute("delete from tasks where id = ?",[id])
json_data = { 'success': True }
return json.dumps(json_data), 200, {'ContentType':'application/json'}