-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.py
74 lines (58 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
from flask import Flask, request, jsonify, render_template
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
CORS(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///contacts.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class Contact(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
email = db.Column(db.String(100), unique=True, nullable=False)
phone = db.Column(db.String(15), nullable=False)
def to_dict(self):
return {
'id': self.id,
'name': self.name,
'email': self.email,
'phone': self.phone
}
with app.app_context():
db.create_all()
# Defautl route for Index.html
@app.route("/")
def index():
return render_template("index.html")
@app.route('/contacts', methods=['POST'])
def add_contact():
data = request.get_json()
new_contact = Contact(name=data['name'], email=data['email'], phone=data['phone'])
db.session.add(new_contact)
db.session.commit()
return jsonify(new_contact.to_dict()), 201
@app.route('/contacts', methods=['GET'])
def get_contacts():
contacts = Contact.query.all()
return jsonify([contact.to_dict() for contact in contacts]), 200
@app.route('/contacts/<int:id>', methods=['GET'])
def get_contact(id):
contact = Contact.query.get_or_404(id)
return jsonify(contact.to_dict()), 200
@app.route('/contacts/<int:id>', methods=['PUT'])
def update_contact(id):
data = request.get_json()
contact = Contact.query.get_or_404(id)
contact.name = data['name']
contact.email = data['email']
contact.phone = data['phone']
db.session.commit()
return jsonify(contact.to_dict()), 200
@app.route('/contacts/<int:id>', methods=['DELETE'])
def delete_contact(id):
contact = Contact.query.get_or_404(id)
db.session.delete(contact)
db.session.commit()
return jsonify({"message": "Contact deleted"}), 204
if __name__ == '__main__':
app.run(debug=True)