forked from p1v2/hillel-flask
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
85 lines (58 loc) · 2.09 KB
/
app.py
File metadata and controls
85 lines (58 loc) · 2.09 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
from flask import Flask, request
import sqlite3
from serializers import serialize_book
app = Flask(__name__)
def get_books(connection):
cursor = connection.cursor()
response = cursor.execute("select * from books")
books_representation = []
for item in response:
books_representation.append(serialize_book(item))
return books_representation
def create_book(connection):
body = request.json
book_name = body["name"]
if book_name == "":
return {"error": "Book name cannot be empty"}, 400
cursor = connection.cursor()
cursor.execute(f"insert into books (name) values ('{book_name}')")
connection.commit()
return "OK"
@app.route("/books", methods=["GET", "POST"])
def books():
connection = sqlite3.connect("db.sqlite")
try:
if request.method == "GET":
# GET /books - list of all books
return get_books(connection)
elif request.method == "POST":
# POST /books - create a book
return create_book(connection)
finally:
connection.close()
def get_book(connection, book_id):
cursor = connection.cursor()
response = cursor.execute(f"select * from books where id={book_id}")
book_representation = response.fetchone()
if book_representation is None:
return {"error": "Book not found"}, 404
return serialize_book(book_representation)
def delete_book(connection, book_id):
cursor = connection.cursor()
cursor.execute(f"delete from books where id={book_id}")
connection.commit()
# No Content
return "", 204
@app.route("/books/<int:book_id>", methods=["GET", "PUT", "PATCH", "DELETE"])
def book(book_id):
connection = sqlite3.connect("db.sqlite")
if request.method == "GET":
return get_book(connection, book_id)
elif request.method == "PUT":
return "book update will be there"
elif request.method == "PATCH":
return "book partial update will be there"
elif request.method == "DELETE":
return delete_book(connection, book_id)
if __name__ == "__main__":
app.run(port=5001, debug=True)