def delete(self, name): if BookModel.find_by_name(name): connection = sqlite3.connect('data.db') cursor = connection.cursor() query = "DELETE FROM books WHERE name = ?" cursor.execute(query, (name, )) connection.close() return {'message': 'Book deleted successfully'} else: return {'message': 'Book does not exist'}
def post(self): json_data = request.get_json(force=True) if not json_data: return {"error": "bad request"}, HTTPStatus.BAD_REQUEST data, errors = book_schema.load(json_data) if errors: return errors, HTTPStatus.BAD_REQUEST book = BookModel(data["title"], data["pages"]) db.session.add(book) db.session.commit() result = book_schema.dump(book).data return result, HTTPStatus.CREATED
def put(self, title: str): data = Book.parser.parse_args() book = BookModel.find_by_title(title) if book: book.price = data['price'] else: book = BookModel(title, **data) book.save_to_db() return book.json()
def put(self, name): request_data = Book.parser.parse_args() book = BookModel.find_by_name(name) new_book = BookModel(name, request_data['price']) if book: try: new_book.update( ) # same as BookModel.insert(new_book) but since new book is an item model rather # than a dict it can be inserted this way except: return { 'message': 'An error occurred while updating the book' }, 500 else: try: new_book.insert() except: return { 'message': 'An error occurred while updating the book' }, 500 return new_book.json()
def post(self, name): book = BookModel.find_by_name(name) if book: return { 'message': 'A book with name {} already exists'.format(name) }, 400 request_data = Book.parser.parse_args() new_book = BookModel(name, request_data['price']) try: new_book.insert() except: return {'message': 'An error occurred while inserting.'}, 500 return new_book.json(), 201
def post(self, title: str): if BookModel.find_by_title(title): return { 'message': "An book with name '{}' already exists.".format(title) }, 400 data = Book.parser.parse_args() book = BookModel(title, **data) try: book.save_to_db() except: return {"message": "An error occurred inserting the book."}, 500 return book.json(), 201
def get(self, name): book = BookModel.find_by_name(name) if book: return book.json() return {'message': 'Book is not available'}, 404
def delete(self, title: str): book = BookModel.find_by_title(title) if book: book.delete_from_db() return {'message': 'book deleted.'} return {'message': 'book not found.'}, 404
def get(self, title: str): book = BookModel.find_by_title(title) if book: return book.json() return {'message': 'Book not found'}, 404