-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
91 lines (57 loc) · 2.03 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
from flask import Flask, request, jsonify
from flask_cors import CORS
import mysql.connector
app = Flask(__name__)
CORS(app)
# MySQL connection configuration
db = mysql.connector.connect(
host='localhost',
user='root',
password='math',
database='userdb'
)
# Route to handle adding a new user
@app.route('/adduser', methods=['POST'])
def add_user():
data = request.get_json()
name = data.get('name')
user_id = data.get('user_id')
password = data.get('password')
mobile = data.get('mobile')
try:
with db.cursor() as cursor:
sql = "INSERT INTO users (name,id,password,mobile) VALUES (%s, %s,%s,%s)"
values = (name,user_id, password,mobile)
cursor.execute(sql, values)
db.commit()
return jsonify({'message': 'User added successfully'})
except mysql.connector.Error as err:
return jsonify({'error': str(err)}), 500
@app.route('/addlike', methods=['POST'])
def add_like():
data = request.get_json()
user_id = data.get('user_id')
liked = data.get('liked')
try:
with db.cursor() as cursor:
sql = "UPDATE users set liked=%s where id=%s"
values = (liked,user_id)
cursor.execute(sql, values)
db.commit()
return jsonify({'message': 'User added successfully'})
except mysql.connector.Error as err:
return jsonify({'error': str(err)}), 500
@app.route('/checkuser', methods=['POST'])
def check_user():
data = request.get_json()
user_id = data.get('user_id')
try:
with db.cursor() as cursor:
sql = "SELECT * FROM users where id=%s"
cursor.execute(sql, (user_id,))
info = cursor.fetchall()
return jsonify({'message': info})
except mysql.connector.Error as err:
return jsonify({'error': str(err)}), 500
if __name__ == '__main__':
app.run(debug=True)