def get(self): cart = db.session.query(Cart).order_by(Cart.id).all() if cart: schema = CartSchema(many=True) data = schema.dump(cart).data logger.info("Cart data fetched succesfully ") return data else: logger.warning("no data is available in cart") return ("no data is available in cart")
def get(self, status): cart = db.session.query(Cart).filter(Cart.status == status).all() if cart: schema = CartSchema() data = schema.dump(cart, many=True).data logger.info("Cart data fetched succesfully based on status") return data else: logger.warning( "no product is available in the cart with such status") return ("no product is available in the cart with such status")
def update_cart(cart_id): data = request.get_json() get_cart = Cart.query.get(cart_id) if data.get('idUser'): get_cart.idUser = data['idUser'] if data.get('products'): get_cart.products = data['products'] db.session.add(get_cart) db.session.commit() cart_schema = CartSchema() cart = cart_schema.dump(get_cart) return make_response(jsonify({"cart": cart}))
def post(self): data = request.get_json() user = data['user_id'] dit = {key: value for key, value in data.items()} obj = db.session.query(Cart).filter( and_(Cart.user_id == user, Cart.status == False)).first() if obj is None: schema = CartSchema() new_cart = schema.load(dit, session=db.session).data db.session.add(new_cart) db.session.commit() data = schema.dump(new_cart).data logger.info("Product successfully added to cart ") return data else: logger.warning("you already have one item in cart") return ("you already have one item in cart")
def create_cart(): data = request.get_json() if 'products' in data: products = data['products'] if not products: return make_response("empty cart", 400) for key, product in enumerate(products): r = requests.get(os.environ['INVENTORY_ADDRESS'] + '/api/products/' + str(product['id'])) if not r.json()['product']: return make_response("non existent product", 404) if r.json()['product']['stock'] < int(product['quantity']): return make_response( "not enough stock for product" + str(product['id']), 400) data['products'][key]['amount'] = str( int(r.json()['product']['price']) * int(product['quantity'])) cart_schema = CartSchema() cart = cart_schema.load(data) result = cart_schema.dump(cart.create()) return make_response(jsonify({"cart": result}), 200)
def get_cart_by_user(user_id): get_cart = Cart.query.filter(Cart.idUser == user_id) cart_schema = CartSchema(many=True) cart = cart_schema.dump(get_cart) return make_response(jsonify({"cart": cart}))
def get_cart_by_id(cart_id): get_cart = Cart.query.get(cart_id) cart_schema = CartSchema() cart = cart_schema.dump(get_cart) return make_response(jsonify({"cart": cart}))
def get_all_carts(): get_carts = Cart.query.all() cart_schema = CartSchema(many=True) carts = cart_schema.dump(get_carts) return make_response(jsonify({"carts": carts}))