def get_copies(book_id):
     """
     Method to get all copies of a book.
     :param book_id: Integer record of a book.
     :return: List of dictionaries of copies.
     """
     if BookDao.contains(book_id):
         return BookCopyDao.get_book_copies(book_id)
     else:
         abort(404, 'Resource not found: book_id')
 def create_copy(book_id):
     """
     Method to create a new copy for a book.
     :param book_id: Book record to create a copy for.
     :return: Dictionary of created Copy.
     """
     print('BookCopyChecker.create_copy()')
     if BookDao.contains(book_id):
         return BookCopyDao.create(book_id)
     else:
         abort(404, 'Resource not found: book_id')
Beispiel #3
0
def create_collection(collection_json):
    """
    Function to create a collection from collection Json object.
    :param collection_json: Json collection object.
    :return: the new collection ID
    """
    print("collection_checker.create_collection()")
    title = collection_json['title']
    collection_id = collection_dao.create_collection(title)

    list_book_ids = collection_json['book_ids']
    for book_id in list_book_ids:
        if BookDao.contains(book_id):
            continue
        else:
            abort(404, 'Invalid book ID')

    for book_id in list_book_ids:
        book = BookDao.get_book_object(book_id)
        collection_dao.append_collection(collection_id, book)
    return collection_dao.get_collection(collection_id)
 def get_notes(book_id):
     """
     Method to retrieve a list of notes about a book.
     :param book_id: the unique book identifier.
     :return: list of notes for a book.
     """
     print('NoteChecker.get_notes()')
     if BookDao.contains(book_id):
         list_notes = NoteDao.get_notes(book_id)
         return list_notes
     else:
         abort(404, 'Resource not found: book_id')
 def add_book_to_author(book_id, author_id):
     """
     Method to add an existing book to an existing author.
     :param book_id: id of existing book.
     :param author_id: id of existing author.
     :return: dictionary of updated author object.
     """
     if BookDao.contains(book_id):
         if AuthorDao.contains(author_id):
             author_dict = AuthorDao.add_book(book_id, author_id)
             return author_dict
         else:
             abort(404, 'Resource not found: author_id')
     else:
         abort(404, 'Resource not found: book_id')
Beispiel #6
0
def delete_book_from_collection_id(collection_id, book_id):
    """
    Deletes a book from a collection by ID name.
    :param collection_id: ID of collection.
    :param book_id: ID of book to be removed.
    :return: The updated collection.
    """
    a_collection = BookCollection.query.get(collection_id)
    a_book = BookDao.get_book_object(book_id)
    if a_collection is None:
        abort(404, 'Cannot remove a book from empty collection.')
    elif a_book is None:
        abort(404, 'This book does not exist in the collection.')
    else:
        updated_collection = collection_dao.delete_book(collection_id, book_id)
        return updated_collection
 def update_author(book_id, author_id, json_author_info):
     """
     Method to update an author record.
     :param book_id: record of a book.
     :param author_id: record of author.
     :param json_author_info: json of author information to be updated.
     :return: dictionary of updated author record.
     """
     if BookDao.contains(book_id):
         if AuthorDao.contains(author_id):
             author = AuthorChecker.json_to_dict(json_author_info)
             return AuthorDao.update(author_id, **author)
         else:
             abort(404, 'Resource not found: author_id')
     else:
         abort(404, 'Resource not found: book_id')
Beispiel #8
0
def add_book_to_collection_id(collection_id, book_id):
    """
    Adds a book to a collection by ID name.
    :param collection_id: ID of collection.
    :param book_id: ID of book to be added.
    :return: The updated collection.
    """
    a_collection = BookCollection.query.get(collection_id)
    a_book = BookDao.get_book_object(book_id)
    if a_collection is None:
        abort(404, 'Collection is empty')
    elif a_book is None:
        abort(404, 'This book does not exist in the collection.')
    else:
        updated_collection = collection_dao.append_collection(
            collection_id, a_book)
        return updated_collection
 def create_notes(book_id, notes_array):
     """
     Method to create a list of notes about a particular book.
     :param book_id: the unique book identifier.
     :param notes_array: list of Strings representing the notes to add.
     :return: a list of notes.
     """
     print('NoteChecker.create_notes()')
     list_notes = []
     if BookDao.contains(book_id):
         for note in notes_array:
             print(note)
             created_note = NoteChecker.create_note(book_id, note)
             list_notes.append(created_note)
         return list_notes
     else:
         abort(404, 'Resource not found: book_id')
 def create_note(book_id, note):
     """
     Method to create a note about a particular book.
     :param book_id: the unique book identifier.
     :param note: String representation of the note.
     :return: The book note.
     """
     print('NoteChecker.create_note()')
     if BookDao.contains(book_id):
         if NoteDao.contains(note.get('note_title')):
             abort(400,
                   'Duplicate key violates unique constraint: note_title')
         else:
             note = NoteDao.create(book_id, note)
             return note
     else:
         abort(404, 'Resource not found: book_id')
    def create_authors(book_id, list_authors):
        """
        Method to generate a list of authors. Depends on create_author() to create the correct author record.
        :param book_id: record of book to add to authors.
        :param list_authors: json list of authors to be created and book added to.
        :return: list of dictionary of created authors.
        """
        print('author_checker.create_authors()')
        list_new_authors = []

        if BookDao.contains(book_id):

            for author in list_authors:
                an_author = AuthorChecker.create_author(book_id, author)
                list_new_authors.append(an_author)
            return list_new_authors
        else:
            abort(404, 'Resource not found: book_id')
 def delete_note(book_id, note_title):
     """
     Method to delete a note if book id, note title are valid and there is a relationship between them.
     :param book_id: record of a book.
     :param note_title: record of a note.
     :return: null.
     """
     print('NoteChecker.delete()')
     if BookDao.contains(book_id):
         if NoteDao.contains_relationship(book_id, note_title):
             return NoteDao.delete(note_title)
         else:
             abort(
                 404,
                 'Resource not found: note_title & book_id do not share a record'
             )
     else:
         abort(404, 'Resource not found: book_id')
 def update_note(book_id, note_title, note):
     """
     Method to update a note record if book id, note title are valid and there is a relationship between them.
     :param book_id: record of a book.
     :param note_title: record of a note.
     :param note: the amended note text.
     :return: dictionary of note object.
     """
     print('NoteChecker.update_note()')
     if BookDao.contains(book_id):
         if NoteDao.contains_relationship(book_id, note_title):
             return NoteDao.update(note_title, note)
         else:
             abort(
                 404,
                 'Resource not found: note_title & book_id do not share a record'
             )
     else:
         abort(404, 'Resource not found: book_id')
 def create_author(book_id, author_json):
     """
     Method to create a new author and attribute a book to author.  If author record already exists, book is added.
     to existing author record.
     :param book_id:  id of existing book.
     :param author_json: json of author information.
     :return: dictionary of created author.
     """
     print('author_checker.create_author()')
     if BookDao.contains(book_id):
         author_dict = AuthorChecker.json_to_dict(author_json)
         if AuthorDao.contains_author(author_dict):
             author_id = AuthorDao.get_author_ID(author_dict)
             results = AuthorChecker.add_book_to_author(book_id, author_id)
             return results
         else:
             an_author = AuthorDao.create(book_id, author_dict)
             return an_author
     else:
         abort(404, 'Resource not found: book_id')
def get_reminders():
    """
    Return the list of checkouts and user's emails that need reminders.
    :return: the list of checkouts that need to be sent email notifications along with the user emails.
    """
    new_list = []
    list_of_reminders = CheckoutDao.get_reminders()

    for checkout in list_of_reminders:
        book = BookDao.get(checkout['book_id'])
        user = UserDao.get(checkout['user_id'])
        title = book['title']
        user_first_name = user['user_first_name']
        user_last_name = user['user_last_name']
        user_email = user['user_email']
        entry = joint_to_dict(checkout['checkout_date'], checkout['due_date'],
                              user_first_name, user_last_name, user_email,
                              title)
        new_list.append(entry)

    return new_list