-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
128 lines (112 loc) · 3.77 KB
/
main.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
import pymysql
from app import app
from config import mysql
from flask import jsonify
from flask import flash, request
@app.route('/create', methods=['POST'])
def create_customer():
try:
_json = request.json
_name = _json['name']
_email = _json['email']
_phone = _json['phone']
_address = _json['address']
if _name and _email and _phone and _address and request.method == 'POST':
conn = mysql.connect()
cursor = conn.cursor(pymysql.cursors.DictCursor)
sqlQuery = "INSERT INTO customer_data(name, email, phone, address) VALUES(%s, %s, %s, %s)"
bindData = (_name, _email, _phone, _address)
cursor.execute(sqlQuery, bindData)
conn.commit()
respone = jsonify('Employee added successfully!')
respone.status_code = 200
return respone
else:
return showMessage("Employee added not successfully")
except Exception as e:
print(e)
finally:
cursor.close()
conn.close()
@app.route('/customer')
def customer():
try:
conn = mysql.connect()
cursor = conn.cursor(pymysql.cursors.DictCursor)
cursor.execute("SELECT id, name, email, phone, address FROM customer_data")
customerRows = cursor.fetchall()
respone = jsonify(customerRows)
respone.status_code = 200
return respone
except Exception as e:
print(e)
finally:
cursor.close()
conn.close()
@app.route('/customer/<int:customer_id>')
def customer_details(customer_id):
try:
conn = mysql.connect()
cursor = conn.cursor(pymysql.cursors.DictCursor)
cursor.execute("SELECT id, name, email, phone, address FROM customer_data WHERE id =%s", customer_id)
customerRow = cursor.fetchone()
respone = jsonify(customerRow)
respone.status_code = 200
return respone
except Exception as e:
print(e)
finally:
cursor.close()
conn.close()
@app.route('/update', methods=['PUT'])
def update_customer():
try:
_json = request.json
_id = _json['id']
_name = _json['name']
_email = _json['email']
_phone = _json['phone']
_address = _json['address']
if _name and _email and _phone and _address and _id and request.method == 'PUT':
sqlQuery = "UPDATE customer_data SET name=%s, email=%s, phone=%s, address=%s WHERE id=%s"
bindData = (_name, _email, _phone, _address, _id,)
conn = mysql.connect()
cursor = conn.cursor()
cursor.execute(sqlQuery, bindData)
conn.commit()
respone = jsonify('Employee updated successfully!')
respone.status_code = 200
return respone
else:
return showMessage()
except Exception as e:
print(e)
finally:
cursor.close()
conn.close()
@app.route('/delete/<int:id>', methods=['DELETE'])
def delete_customer(id):
try:
conn = mysql.connect()
cursor = conn.cursor()
cursor.execute("DELETE FROM customer_data WHERE id =%s", (id,))
conn.commit()
respone = jsonify('Employee deleted successfully!')
respone.status_code = 200
return respone
except Exception as e:
print(e)
finally:
cursor.close()
conn.close()
@app.errorhandler(404)
def showMessage(error=None):
message = {
'status': 404,
'message': 'Record not found: ' + request.url,
}
respone = jsonify(message)
respone.status_code = 404
return respone
if __name__ == "__main__":
app.run()