forked from kfurtado2171/CIS469-Flask-React_CRUD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
97 lines (85 loc) · 2.97 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
from flask import Flask, request, jsonify,send_file
import sqlite3
from flask_cors import CORS, cross_origin
import os
#app = Flask(__name__)
# Distribution Version
app = Flask(__name__, static_folder='_frontend/users/dist', static_url_path='')
# Add CORS support
CORS(app)
@app.route('/', methods=['GET'])
def defaultPage():
return send_file(os.path.join(app.static_folder, 'index.html'))
# create the database first
@app.route('/createDB')
def index():
conn = sqlite3.connect('mydatabase.db')
cursor = conn.cursor()
cursor.execute('''CREATE TABLE users
(id INTEGER PRIMARY KEY, name TEXT, email TEXT)''')
conn.commit()
conn.close()
return 'Dabase Created'
# Route for creating a new user
@app.route('/api/users', methods=['POST'])
def create_user():
name = request.json['name']
email = request.json['email']
conn = sqlite3.connect('mydatabase.db')
cursor = conn.cursor()
cursor.execute('INSERT INTO users (name, email) VALUES (?, ?)', (name, email))
conn.commit()
new_id = cursor.lastrowid
conn.close()
response = jsonify({'id': new_id, 'name': name, 'email': email}), 201
return response
# Route for retrieving all users
@app.route('/api/users', methods=['GET'])
def get_users():
conn = sqlite3.connect('mydatabase.db')
cursor = conn.cursor()
cursor.execute('SELECT id, name, email FROM users')
rows = cursor.fetchall()
conn.close()
users = [{'id': row[0], 'name': row[1], 'email': row[2]} for row in rows]
return jsonify(users)
# Route for retrieving a single user by ID
@app.route('/api/users/<int:user_id>', methods=['GET'])
def get_user(user_id):
print(user_id)
conn = sqlite3.connect('mydatabase.db')
cursor = conn.cursor()
cursor.execute('SELECT id, name, email FROM users WHERE id=?', (user_id,))
row = cursor.fetchone()
conn.close()
if row:
user = {'id': row[0], 'name': row[1], 'email': row[2]}
response = jsonify(user)
return response
else:
response = jsonify({'error': 'User not found'})
return response
# Route for updating an existing user by ID
@app.route('/api/users/<int:user_id>', methods=['PUT'])
def update_user(user_id):
name = request.json['name']
email = request.json['email']
conn = sqlite3.connect('mydatabase.db')
cursor = conn.cursor()
cursor.execute('UPDATE users SET name=?, email=? WHERE id=?', (name, email, user_id))
conn.commit()
conn.close()
response = jsonify({'message': 'User updated successfully'})
return response
# Route for deleting a user by ID
@app.route('/api/users/<int:user_id>', methods=['DELETE'])
def delete_user(user_id):
conn = sqlite3.connect('mydatabase.db')
cursor = conn.cursor()
cursor.execute('DELETE FROM users WHERE id=?', (user_id,))
conn.commit()
conn.close()
response = jsonify({'message': 'User deleted successfully'})
return response
if __name__ == '__main__':
app.run(debug=True,host="0.0.0.0",port=5002)