-
Notifications
You must be signed in to change notification settings - Fork 0
/
book_dao.py
98 lines (77 loc) · 2.71 KB
/
book_dao.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
from pymongo_connector import collection, publisher_collection
#Search methods
def findAll():
results = collection.find()
return results
def findByTitle(book_title):
results = collection.find({'title': book_title})
return results
def findByPublisher(book_publisher):
results = collection.find({'published_by': book_publisher})
return results
def findByPriceRange(min_price, max_price):
results = collection.find({'price':
{"$gt": int(min_price),
"$lt": int(max_price)
}})
return results
def findByTitlePublisher(title, publisher):
results = collection.find({'title': title,
'published_by': publisher})
return results
#Add a new book to the database
def insertNewBook(ISBN, title, year, publisher, prev_edition, price):
new_book = {"ISBN": ISBN,
"title": title,
"year": year,
"published_by": publisher,
"previous_edition": prev_edition,
"price": price}
collection.insert_one(new_book)
#Add a new publisher
def insertNewPublisher(name, phone, city):
new_publisher = {"name": name,
"phone": phone,
"city": city}
publisher_collection.insert_one(new_publisher)
#Edit book functions
def editBookTitle(ISBN, newTitle):
myQuery = {"ISBN": ISBN}
newvalues = {"$set":
{"title": newTitle}
}
collection.update_one(myQuery, newvalues)
def editBookPublishedYear(ISBN, newYear):
myQuery = {"ISBN": ISBN}
newvalues = {"$set":
{"year": newYear}
}
collection.update_one(myQuery, newvalues)
def editBookPublisher(ISBN, newPublisher):
myQuery = {"ISBN": ISBN}
newvalues = {"$set":
{"published_by": newPublisher}
}
collection.update_one(myQuery, newvalues)
def editBookPrevEdition(ISBN, newPrevEdition):
myQuery = {"ISBN": ISBN}
newvalues = {"$set":
{"previous_edition": newPrevEdition}
}
collection.update_one(myQuery, newvalues)
def editBookPrice(ISBN, newPrice):
myQuery = {"ISBN": ISBN}
newvalues = {"$set":
{"price": newPrice}
}
collection.update_one(myQuery, newvalues)
##helper function for editing books
def searchBookISBN(ISBN):
result = collection.find({'ISBN': ISBN })
return result
def searchPublisher(Publisher):
result = publisher_collection.find({'name': Publisher})
return result
#delete a book from the table
def deleteBook(ISBN):
collection.delete_one({'ISBN': ISBN})