-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
130 lines (108 loc) · 3.14 KB
/
app.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
from flask import Flask, request
from flask_cors import CORS
import mysql.connector
import json
app = Flask(__name__)
CORS(app)
db = mysql.connector.connect(
host="localhost", user="root", passwd="", database="todoapp")
@app.route("/auth", methods=['POST'])
def auth():
data = request.get_json()
username = data['username']
password = data['password']
cursor = db.cursor()
check_user_stmt = "SELECT name FROM users WHERE name = '" + username+"'"
insrt_stmt = "INSERT INTO users(name, password) VALUES(%s, %s)"
data = (username, password)
check_credential_stmt = "SELECT name, password FROM users WHERE name = '" + \
username+"' and password='"+password+"'"
try:
cursor.execute(check_user_stmt)
res = cursor.fetchall()
if res:
cursor.execute(check_credential_stmt)
res2 = cursor.fetchall()
if(res2):
return json.dumps({
"status": True,
})
else:
return json.dumps({
"status": False,
"error": "Invalid credentials, Try Again"
})
else:
cursor.execute(insrt_stmt, data)
db.commit()
return json.dumps({
"status": True,
})
except:
return json.dumps({
"status": False,
"error": "Something Went Wrong, Try Again"
})
@app.route("/add", methods=["POST"])
def add():
data = request.get_json()
username = data["username"]
greet = data["greet"]
cursor = db.cursor()
stmt = "INSERT INTO tasks(name, task) VALUES(%s, %s)"
data = (username, greet)
try:
cursor.execute(stmt, data)
db.commit()
return json.dumps({
"status": True,
})
except:
return json.dumps({
"status": False,
"error": "Couldn't create task, Try Again"
})
@app.route("/list", methods=["POST"])
def listAll():
def transform(x):
return {
'task': x[0],
'taskid': x[1]
}
data = request.get_json()
username = data["username"]
cursor = db.cursor()
stmt = "SELECT task, taskid FROM tasks WHERE name='" + username+"'"
try:
cursor.execute(stmt)
res = cursor.fetchall()
return json.dumps({
"status": True,
"data": list(map(transform, res))
})
except Exception as e:
return json.dumps({
"status": False,
"error": "Something went wrong, Try Again"
})
@app.route("/delete", methods=["POST"])
def delete():
data = request.get_json()
taskid = data["id"]
print(type(taskid))
cursor = db.cursor()
stmt = "DELETE FROM tasks WHERE taskid= {}".format(taskid)
try:
res = cursor.execute(stmt)
db.commit()
return json.dumps({
"status": True,
"data": res
})
except Exception as e:
return json.dumps({
"status": False,
"error": "Something Went Wrong, try again"
})
if __name__ == "__main__":
app.run()