def update_user(pk): data = request.get_json() try: session.query(User).filter(User.id == pk).update(data) session.commit() except Exception: return jsonify({'message': "Commit was failed"}), 405 return jsonify({'message': "Success"}), 200
def update_product(pk): data = request.get_json() try: check_None(Product, pk) except Exception: return jsonify({'message': "Product not found"}), 404 session.query(Product).filter(Product.id == pk).update(data) session.commit() return jsonify({'message': "Success"}), 200
def update_purchase(pk): data = request.get_json() try: check_None(Purchase, pk) except Exception: return jsonify({'message': "Purchase not found"}), 404 if session.query(Purchase).filter(Purchase.id == pk).filter( Purchase.userID == get_jwt_identity()): purchase_check = session.query(Purchase).filter( Purchase.id == pk).filter(Purchase.userID == get_jwt_identity()) purchase_check.update(data) session.commit() return jsonify({'message': "Success"}), 200 else: return jsonify({'message': "Not your purchase"}), 405
def get_products(): try: products = ProductSchema() except Exception: return jsonify({'message': "Products not found"}), 404 global_products = session.query(Product).order_by(Product.name).all() return jsonify(ProductSchema(many=True).dump(global_products))
def user_login(): data = request.get_json() email = data.get('email') password = data.get('password') if not email or not password: return jsonify({'Login': "******"}), 400 user = session.query(User).filter(User.email == email).one() if user.password == password: return jsonify(login="******"), 200 return jsonify(login="******"), 400
def cancel_purchase(pk): try: purchase = check_None(Purchase, pk) except Exception: return jsonify({'message': "Purchase not found"}), 404 purchase_check = session.query(Purchase).filter(Purchase.id == pk).filter( Purchase.userID == get_jwt_identity()) if purchase_check is not None: session.delete(purchase) session.commit() return jsonify({'message': "Success"}), 200 else: return jsonify({'message': "Not your purchase"}), 200
def check_None(cls, pk): obj = session.query(cls).get(pk) if obj is None: raise Exception return obj