-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
108 lines (73 loc) · 2.82 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
from flask import Flask, request, jsonify, make_response
from flask_sqlalchemy import SQLAlchemy
from marshmallow_sqlalchemy import SQLAlchemySchema
from marshmallow import fields
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:%40jardeljr.7@localhost/flask'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # Optional: Suppress warning
db = SQLAlchemy(app)
class Author(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(20))
specialisation = db.Column(db.String(50))
def create(self):
db.session.add(self)
db.session.commit()
return self
def __init__(self, name, specialisation):
self.name = name
self.specialisation = specialisation
def __repr__(self):
return f"Product {self.id}"
#serializer
class AuthorSchema(SQLAlchemySchema):
class Meta(SQLAlchemySchema.Meta):
model = Author
sqla_session = db.session
id = fields.Number(dump_only=True)
name = fields.String(required=True)
specialisation = fields.String(required=True)
@app.route("/authors", methods=['GET'])
def index():
get_authors = Author.query.all()
author_schema = AuthorSchema(many=True)
authors = author_schema.dump(get_authors)
return make_response(jsonify({"autors": authors}))
@app.route("/authors", methods=['POST'])
def create_author():
data = request.get_json()
author_schema = AuthorSchema()
author = author_schema.load(data)
author_instance = Author(**author)
created_author = author_instance.create()
result = author_schema.dump(created_author)
return make_response(jsonify({"author": author}), 201)
@app.route("/authors/<id>", methods=['GET'])
def get_author_by_id(id):
get_author = Author.query.get(id)
author_schema = AuthorSchema()
author = author_schema.dump(get_author)
return make_response(jsonify({"author": author}))
@app.route("/authors/<id>", methods=['PUT'])
def update_author_by_id(id):
data = request.get_json()
get_author = Author.query.get(id)
if data.get("specialisation"):
get_author.specialisation = data['specialisation']
if data.get('name'):
get_author.name = data['name']
db.session.add(get_author)
db.session.commit()
author_schema = AuthorSchema(only=['id', 'name', 'specialisation'])
author = author_schema.dump(get_author)
return make_response(jsonify({"author": author}))
@app.route("/authors/<id>", methods=['DELETE'])
def delete_author_by_id(id):
get_author = Author.query.get(id)
db.session.delete(get_author)
db.session.commit()
return make_response("", 204)
if __name__ == "__main__":
with app.app_context():
db.create_all() # Creates the tables if they don't already exist
app.run(debug=True)