示例#1
0
 def create_book(self, book):
     db = SQLiteDatabaseConnection()
     with db:
         existing_book = db.get_book_by_author_and_name(
             author=book["author"], name=book["name"])
         if existing_book:
             raise BookAlreadyExists(book)
     book_model = BooksDBModel(**book)
     with db:
         db.add_book(book_model)
         book = db.get_book_by_author_and_name(
             name=book_model.name, author=book_model.author).serialize()
     return book
示例#2
0
 def edit_book(self, book_id, new_book):
     db = SQLiteDatabaseConnection()
     existing_book = self.get_book(book_id)
     if not existing_book:
         raise ResourceNotFound(resource_type="Book",
                                field="id",
                                value=book_id)
     new_book["id"] = book_id
     new_book_model = BooksDBModel(**new_book)
     with db:
         existing_book = db.get_book_by_author_and_name(
             name=new_book_model.name, author=new_book_model.author)
         if existing_book and not existing_book.id == book_id:
             raise BookAlreadyExists(new_book)
         rows = db.update_book(book_id, new_book_model)
         if rows == 0:
             raise DatabaseCommunicationIssue("update book")
     return self.get_book(book_id)
示例#3
0
 def list_books(self):
     db = SQLiteDatabaseConnection()
     with db:
         books_list = BooksDBModel.serialize_list(db.get_all_books())
     return books_list
示例#4
0
 def get_book_by_partial_author_name(self, author_name):
     db = SQLiteDatabaseConnection()
     with db:
         books = db.get_books_by_partial_author_name(author_name)
         books_list = BooksDBModel.serialize_list(books)
     return books_list
from library_backend.models.database.books_db_model import BooksDBModel
from library_backend.models.database.users_db_model import UsersDBModel

books = [
    BooksDBModel(
        name="A song of ice and fire",
        author="George R.R. Martin",
        description="Some say it is better than the tv series",
        cover=
        "https://upload.wikimedia.org/wikipedia/en/thumb/d/dc/A_Song_of_Ice_and_Fire_book_collection_box_set_cover.jpg/220px-A_Song_of_Ice_and_Fire_book_collection_box_set_cover.jpg"
    ),
    BooksDBModel(
        name="Nightflyers",
        author="George R.R. Martin",
        description=
        "On a mission aboard the Nightflyer, the most advanced ship ever built, a team of scientists embark on an expedition to make first contact with alien life. Set in the year 2093, their mission takes them beyond the edge of the solar system, farther than mankind has ever gone before. But when terrifying and violent events start to occur, the team and crew begin to question themselves, each other, and their reclusive captain. They soon come to realize that the true horror isn't waiting for them in outer space -- it's already on their ship.",
        cover=
        "https://vignette.wikia.nocookie.net/nightflyers8841/images/6/60/Nightflyers_Book_Cover.jpg/revision/latest?cb=20181024125449"
    ),
    BooksDBModel(
        name="Horus Heresy: Horus Rising",
        author="Dan Abnett",
        description=
        "It is the 31st millennium. Under the benevolent leadership of the Immortal Emperor, the Imperium of Man has stretched out across the galaxy. It is a golden age of discovery and conquest. But now, on the eve of victory, the Emperor leaves the front lines, entrusting the great crusade to his favourite son, Horus. Promoted to Warmaster, can the idealistic Horus carry out the Emperor's grand plan, or will this promotion sow the seeds of heresy amongst his brothers? Horus Rising is the first chapter in the epic tale of the Horus Heresy, a galactic civil war that threatened to bring about the extinction of humanity.",
        cover=
        "https://wh40k.lexicanum.com/mediawiki/images/d/d0/Horusrising.jpg"),
    BooksDBModel(
        name="Horus Heresy: False Gods",
        author="Graham McNeil",
        description=
        "The Great Crusade that has taken humanity into the stars continues. The Emperor of mankind has handed the reins of command to his favoured son, the Warmaster Horus. Yet all is not well in the armies of the Imperium. Horus is still battling against the jealousy and resentment of his brother primarchs and, when he is injured in combat on the planet Davin, he must also battle his inner daemons. With all the temptations that Chaos has to offer, can the weakened Horus resist?",
 def add_book(self, book_model: BooksDBModel):
     book_id = str(uuid.uuid4())
     book_model.id = book_id
     self.session.add(book_model)