forked from egrfly/instabook-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook_management.py
30 lines (26 loc) · 1.08 KB
/
book_management.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
from utils import get_db_connection
def search_books(title):
"""
Return a list of books whose titles are like a particular title
Required columns: b.id, b.title, b.author, AVG(r.score)
Required tables: books b, book_ratings r
"""
with get_db_connection() as connection:
with connection.cursor() as cursor:
# Temporary code
books_with_average_scores = [(1, 'Some Book', 'Some Author', 5),
(2, 'Some Other Book', 'Some Other Author', 4)]
return [book for book in books_with_average_scores if title in book[1]]
def get_book_details(book_id):
"""
Return details of a specific book
Required columns: b.id, b.title, b.author, AVG(r.score)
Required tables: books b, book_ratings r
"""
with get_db_connection() as connection:
with connection.cursor() as cursor:
# Temporary code
if book_id == 1:
return 1, 'Some Book', 'Some Author', 5
else:
return book_id, 'Some Other Book', 'Some Other Author', 4