async def delete_book_data(id: str): deleted_book = delete_book(id) if deleted_book: return ResponseModel("Book with ID:{} removed.".format(id), "Book deleted successfully.") return ErrorResponseModel("An error occured.", 404, "Book with ID:{0} doesn't exist".format(id))
async def delete_book_data(id: str): book = await delete_book(id) if book: return ResponseModel(f"Book data with id {id} deleted successfully", "Book data deleted successfully") return ErrorResponse("An error occured", 404, f"Book data with id {id} does not exist")
async def update_book_data(id: str, book: UpdateBookSchema = Body(...)): book = {k: v for k, v in book.dict().items() if v is not None} updated_book = await update_book(id, book) if updated_book: return ResponseModel(f"Book with the id {id} updated successfully", "book updated successfully") return ErrorResponse("An error occured", 404, "There was an error updating the book data")
def borrow_book_task(user_id, book_id): user_data = retrieve_user(user_id) book_data = retrieve_book(book_id) if checkBook(book_data["id"]): return {"message": "This book already borrowed."} else: add_borrow_book(user_data, book_data) return ResponseModel("Book borrowed", "Book borrowed.")
async def update_book_data(id, req: UpdateBookModel = Body(...)): req = {k: v for k, v in req.dict().items() if v is not None} updated_book = update_book(id, req) if updated_book: return ResponseModel( "Book with ID:{} name update is successful.".format(id), "Book name updated successfully,") return ErrorResponseModel("An error occured.", 404, "There was an error updating the book data.")
async def return_book(book_id): if checkBook(book_id): deleted_book = delete_borrow_book(book_id) if deleted_book: return ResponseModel("The book has been returned.", "The book has been returned") return ErrorResponseModel("An error occured.", 404, "The book could not be returned.") else: return ErrorResponseModel("This book is not on loan", 404, "This book is not on loan.")
async def get_book(id: str): book = await retrieve_book(id) if book: return ResponseModel(book, "Book retrieved successfully") return ErrorResponse("An error occured", 404, "Book dosen't exist")
async def get_books(): books = await retrieve_books() if books: return ResponseModel(books, "Books retrieved successfully") return ResponseModel(books, "Empty list returned")
async def add_book_data(book: BookSchema = Body(...)): book = jsonable_encoder(book) new_book = await add_book(book) return ResponseModel(new_book, "Book added successfully.")
async def borrowed_book(): books = get_borrowed_books() if books: return ResponseModel(books, "Books data retrieved successfully.") return ResponseModel(books, "Empty list returned.")
async def borrow_book(user_id, book_id): borrow_book_task.delay(user_id, book_id) return ResponseModel("Task started", "Borrowed.")
async def get_book_data(id): book = retrieve_book(id) if book: return ResponseModel(book, "Book data retrieved successfully.") return ErrorResponseModel("An error occured. ", 404, "Book doesn't exist.")
def get_books(): books = retrieve_books() if books: return ResponseModel(books, "Book data retrieved successfully.") return ResponseModel(books, "Empty list returned.")