-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
98 lines (91 loc) · 3.49 KB
/
main.py
File metadata and controls
98 lines (91 loc) · 3.49 KB
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
from flask import Flask, request, jsonify
from models import db, Author, Book
app = Flask(__name__)
port = 5000
app.config['SQLALCHEMY_DATABASE_URI']= 'postgresql+psycopg2://postgres:123456@localhost:5432/test'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False
@app.route('/')
def hello_world():
return 'Hello world!'
@app.route('/authors', methods=['GET'])
def get_authors():
try:
authors = Author.query.all()
authors_data = []
for author in authors:
author_data = {
'id': author.id,
'name': author.name,
'age': author.age,
'created_at': author.created_at,
'books': []
}
for book in author.books:
book_data = {
'id': book.id,
'isbn': book.isbn,
'name': book.name,
'cant_pages': book.cant_pages,
'created_at': book.created_at
}
author_data['books'].append(book_data)
authors_data.append(author_data)
return jsonify({'authors': authors_data})
except Exception as error:
print('Error', error)
return jsonify({'message': 'Internal server error'}), 500
@app.route('/authors', methods=['POST'])
def add_author():
try:
data = request.json
name = data.get('name')
age = data.get('age')
if not name or not age:
return jsonify({'message': 'Bad request, name or age not found'}), 400
new_author = Author(name=name, age=age)
db.session.add(new_author)
db.session.commit()
return jsonify({'author': {'id': new_author.id, 'name': new_author.name, 'age': new_author.age, 'created_at': new_author.created_at}}), 201
except Exception as error:
print('Error', error)
return jsonify({'message': 'Internal server error'}), 500
@app.route('/books', methods=['GET'])
def get_books():
try:
books = Book.query.all()
books_data = []
for book in books:
book_data = {
'id': book.id,
'isbn': book.isbn,
'name': book.name,
'created_at': book.created_at,
'cant_pages': book.cant_pages
}
books_data.append(book_data)
return jsonify({'books': books_data})
except Exception as error:
print('Error', error)
return jsonify({'message': 'Internal server error'}), 500
@app.route('/books', methods=['POST'])
def add_book():
try:
data = request.json
isbn = data.get('isbn')
name = data.get('name')
cant_pages = data.get('cant_pages')
author_id = data.get('author_id')
if not name or not cant_pages or not author_id or not isbn:
return jsonify({'message': 'Bad request, isbn or name or cantPages or author not found'}), 400
new_book = Book(isbn=isbn, name=name, cant_pages=cant_pages, author_id=author_id)
db.session.add(new_book)
db.session.commit()
return jsonify({'book': {'id': new_book.id, 'isbn': new_book.isbn, 'name': new_book.name, 'cant_pages': new_book.cant_pages, 'created_at': new_book.created_at}}), 201
except Exception as error:
print('Error', error)
return jsonify({'message': 'Internal server error'}), 500
if __name__ == '__main__':
db.init_app(app)
with app.app_context():
db.create_all()
app.run(host='0.0.0.0', debug=True, port=port)