57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
from bson.objectid import ObjectId
|
|
import sys
|
|
|
|
class BookDAO:
|
|
|
|
# constructor for the class
|
|
def __init__(self, database):
|
|
self.db = database
|
|
self.books = database.books
|
|
|
|
def get_books(self):
|
|
cursor = self.books.find()
|
|
l = []
|
|
for book in cursor:
|
|
l.append(
|
|
{'title':book['title'],
|
|
'_id':book['_id'],
|
|
'publisher':book['publisher'],
|
|
'isbn':book['isbn'],
|
|
'author':book['author']}
|
|
)
|
|
return l
|
|
|
|
def get_book(self, book_id):
|
|
book = self.books.find_one({"_id": ObjectId(book_id)})
|
|
return book
|
|
|
|
def insert_entry(self, book_title):
|
|
print "inserting book entry", book_title
|
|
|
|
book = {"title": book_title}
|
|
|
|
# now insert the book
|
|
try:
|
|
result = self.books.insert_one(book)
|
|
print "Matching book: ", result.matched_count
|
|
print "Modified book: ", result.modified_count
|
|
except:
|
|
print "Error inserting book"
|
|
print "Unexpected error:", sys.exc_info()
|
|
|
|
def update_entry(self, book_id, book_title):
|
|
print "updating book entry", book_title
|
|
|
|
filter_doc = {"_id": ObjectId(book_id)}
|
|
book = { "$set": {"name": book_title}}
|
|
|
|
# now insert the book
|
|
try:
|
|
result = self.books.update_one(filter_doc, book)
|
|
print "Modified book: ", result.modified_count
|
|
except:
|
|
print "Error inserting book"
|
|
print "Unexpected error:", sys.exc_info()
|
|
|