def get(self, name): found_book = BookModel.find_by_name(name) if found_book: return { 'book': found_book.json() }, 404 if found_book is None else 200 return {'message': 'Item Not Found'}, 404
def put(self): data = BookList.parser.parse_args() fetched_book = BookModel.find_by_name(data['name']) book = BookModel(**data) if not fetched_book: book.save_to_db() else: fetched_book.price = data['price'] fetched_book.save_to_db() return book.json(), 200
def post(self): book = request.get_json() if not book: return {'message': 'Please insert book name and price'}, 400 if BookModel.find_by_name(book.get('name')): return {'message': 'A book with the same name already exists'}, 400 try: BookModel(**book).save_to_db() except: return {'message': 'Something went wrong'} return book, 201
def delete(self, name): try: BookModel.find_by_name(name).delete_from_db() return {'message': 'Item deleted'} except: return {'message': 'Delete operation failed. Item not found'}