def clear_cart_items_endpoint(cart_id): """Clear all items from the Cart `cart_id`.""" cart = storage.get_cart(cart_id) cart.remove_all_items() return "", 204
def add_multiple_items_to_cart_endpoint(cart_id): """Add multiple items to the cart..""" cart = storage.get_cart(cart_id) request_body = request.get_json() errors = [] for item in request_body: try: if "quantity" not in item: raise InvalidQuantityException if "itemId" not in item: raise InvalidItemException quantity = item.get("quantity") item_id = item.get("itemId") item = Item(item_id, quantity) cart.add_item(item) except BaseCartException as error: errors.append(error.to_dict()) response = {"cart": cart.to_json(), "_errors": errors} return jsonify(response), 200
def increment_item_quantity_endpoint(cart_id, item_id): cart = storage.get_cart(cart_id) item = cart.get_item_by_id(item_id) item.set_quantity(item.get_quantity() + 1) return jsonify(cart.to_json()), 200
def delete_item_from_cart_endpoint(cart_id, item_id): cart = storage.get_cart(cart_id) item = cart.get_item_by_id(item_id) cart.remove_item(item) return "", 204
def delete_item_from_cart_endpoint(cart_id, item_id): """Delete the Item `item_id` from the Cart `cart_id`.""" cart = storage.get_cart(cart_id) item = cart.get_item_by_id(item_id) cart.remove_item(item) return "", 204
def decrement_item_quantity_endpoint(cart_id, item_id): cart = storage.get_cart(cart_id) item = cart.get_item_by_id(item_id) quantity = item.get_quantity() - 1 if quantity == 0: cart.remove_item(item) else: item.set_quantity(quantity) return jsonify(cart.to_json()), 200
def update_item_to_cart_endpoint(cart_id, item_id): """Update the Item `item_id` on the Cart `cart_id`.""" cart = storage.get_cart(cart_id) item = cart.get_item_by_id(item_id) request_body = request.get_json() if "quantity" not in request_body: raise InvalidQuantityException quantity = request_body.get("quantity") item.set_quantity(quantity) return jsonify(cart.to_json()), 200
def add_item_to_cart_endpoint(cart_id, item_id): """Add the Item `item_id` to the Cart `cart_id`.""" cart = storage.get_cart(cart_id) request_body = request.get_json() if "quantity" not in request_body: raise InvalidQuantityException quantity = request_body.get("quantity") item = Item(item_id, quantity) cart.add_item(item) return jsonify(cart.to_json()), 200
def add_item_to_cart_endpoint(cart_id, item_id): cart = storage.get_cart(cart_id) # get_json will refuse to parse the body if there's no 'application/json' Content-Type header set. # you could do json.loads(request.data) here instead, but we chose to use get_json's force flag. request_body = request.get_json(force=True) if 'quantity' not in request_body: raise InvalidQuantityException quantity = request_body.get('quantity') item = Item(item_id, quantity) cart.add_item(item) return jsonify(cart.to_json()), 200
def bulk_add_item_to_cart_endpoint(cart_id): # get_json will refuse to parse the body if there's no 'application/json' Content-Type header set. # you could do json.loads(request.data) here instead, but we chose to use get_json's force flag. items = request.get_json(force=True) validation_errors = [] for index, item in enumerate(items): try: if 'quantity' not in item: raise InvalidQuantityException if 'item_id' not in item: raise MissingItemIdException except (MissingItemIdException, InvalidQuantityException) as e: validation_errors.append({'index': index, 'error': e.message}) if validation_errors: return jsonify(validation_errors), 400 cart = storage.get_cart(cart_id) for item in items: item = Item(item['item_id'], item['quantity']) cart.add_item(item) return jsonify(cart.to_json()), 200
def test_get_cart_id_exists(): with patch.dict(storage._carts, {1: mock.sentinel}): assert storage.get_cart(1) == mock.sentinel
def test_get_cart_id_doesnt_exist(): with patch.dict(storage._carts, {}): with pytest.raises(CartNotFoundException): storage.get_cart(1)
def get_cart_endpoint(cart_id): """Return a Cart identified by `cart_id`.""" cart = storage.get_cart(cart_id) return jsonify(cart.to_json()), 200
def clear_cart_endpoint(cart_id): cart = storage.get_cart(cart_id) cart.clear() return "", 204
def get_cart_endpoint(cart_id): cart = storage.get_cart(cart_id) return jsonify(cart.to_json()), 200