def insert_book(payload): data = request.get_json() ibookname = data.get('bookname', '') iauthor = data.get('author', '') iprice = data.get('price', '') iquantity = data.get('quantity', '') icategory = data.get('category', '') iagegroup = data.get('agegroup', '') if ((ibookname == '') or (iauthor == '') or (icategory == '') or (iprice == '') or (iquantity == '')) or (iagegroup == ''): abort(422) ctgrid = Category.query.filter(Category.type == icategory).one_or_none() agrpid = Kidsage.query.filter(Kidsage.agegroup == iagegroup).one_or_none() if ((ctgrid is None) or (agrpid is None)): abort(422) try: book = Books(bookname=ibookname, author=iauthor, price=iprice, quantity=iquantity, category_id=ctgrid.id, age_id=agrpid.id) book.insert() # return success message return jsonify({ 'success': True, 'message': 'Book created successfully!' }), 200 except Exception: abort(500)
def get_book_by_id(id): book = Books.objects(id=id).first() if not book: #book not found, return error return jsonify({'error': 'invalid book id!'}) else: return jsonify(book.to_json())
def get_all_books(): book = Books.objects() if not book: #book not found, return error return jsonify({'error': 'no books found!'}) else: return jsonify(book.to_json())
def delete_book(id): body = request.get_json() book = Books.objects(id=id).first() if not book: #book not found, return error return jsonify({'error': 'invalid book delete request!'}) else: book.delete() return jsonify({'success': 'book delete complete!'})
def update_book(id): body = request.get_json() book = Books.objects(id=body['id']).first() if not book: #book not found, return error return jsonify({'error': 'invalid book update request!'}) else: book.update(**body) return jsonify(book.to_json())
def get(self): book = Books.objects().to_json() return Response(book, mimetype="applicaiton/json", status=200)
def post(self): body = request.get_json() book = Books(**body).save() id = book.b_id return {'id': str(id)}, 200
def add_book(): body = request.get_json() #extracting book data from request book = Books(**body).save() return jsonify(book.to_json())