def delete_products(order_id, product_id): """ Delete an Product This endpoint will delete an Product based the id specified in the path """ app.logger.info("Request to delete order with id: %s", order_id) # pylint: disable=maybe-no-member product = Product.find(product_id) if product: product.delete() return make_response("", status.HTTP_204_NO_CONTENT)
def get_products(product_id): """ Retrieve a single Product This endpoint will return a product based on it's id """ app.logger.info("Request for product with id: %s", product_id) product = Product.find(product_id) if not product: raise NotFound( "Product with id '{}' was not found.".format(product_id)) return make_response(jsonify(product.serialize()), status.HTTP_200_OK)
def delete_products(product_id): """ Delete a Product This endpoint will delete a Product based the id specified in the path """ app.logger.info("Request to delete product with id: %s", product_id) product = Product.find(product_id) if product: product.delete() app.logger.info("Product with ID [%s] delete complete.", product_id) return make_response("", status.HTTP_204_NO_CONTENT)
def delete(self, product_id): """ Delete a Product This endpoint will delete a product based the id specified in the path """ app.logger.info("Request to delete product with id: %s", product_id) try: product_id = int(product_id) except ValueError: app.logger.info("Invalid Product ID.") api.abort(status.HTTP_400_BAD_REQUEST, "Invalid Product ID.") product = Product.find(product_id) if product: product.delete() app.logger.info("Product with id [%s] delete complete.", product_id) return make_response(jsonify(message=''), status.HTTP_204_NO_CONTENT)
def test_find_product(self): """ Find a Product by ID """ Product(name="iPhone X", description="Black iPhone", category="Technology", price=999.99).create() t_v = Product(name="TV", description="Black Sony TV", category="Technology", price=1999.99) t_v.create() product = Product.find(t_v.id) self.assertIsNot(product, None) self.assertEqual(product.id, t_v.id) self.assertEqual(product.name, "TV") self.assertEqual(product.description, "Black Sony TV") self.assertEqual(product.category, "Technology") self.assertEqual(product.price, 1999.99)
def test_find_product(self): """ Find a Product by ID """ products = ProductFactory.create_batch(3) for product in products: product.create() logging.debug(products) # make sure they got saved self.assertEqual(len(Product.all()), 3) # find the 2nd product in the list test_product = Product.find(products[1].id) self.assertIsNot(test_product, None) self.assertEqual(test_product.id, products[1].id) self.assertEqual(test_product.name, products[1].name) self.assertEqual(test_product.available, products[1].available) self.assertEqual(test_product.sku, products[1].sku) self.assertEqual(test_product.price, products[1].price) self.assertEqual(test_product.stock, products[1].stock) self.assertEqual(test_product.size, products[1].size) self.assertEqual(test_product.color, products[1].color) self.assertEqual(test_product.category, products[1].category)
def get(self, product_id): """ Retrieve a product This endpoint will return a product based on its id """ app.logger.info("Request for product with id: %s", product_id) try: product_id = int(product_id) except ValueError: app.logger.info("Invalid Product ID.") api.abort(status.HTTP_400_BAD_REQUEST, "Invalid Product ID.") product = Product.find(product_id) if not product: app.logger.info("Product with id [%s] was not found.", product_id) api.abort(status.HTTP_404_NOT_FOUND, "Product with id '{}' was not found.".format(product_id)) app.logger.info("Returning product with id [%s].", product.id) return product.serialize(), status.HTTP_200_OK
def update_product(product_id): """ Update an existing product This endpoint will update a Product based on the body that is posted """ app.logger.info("Request to update product with id: %s", product_id) stock = request.args.get("stock") product = Product.find(product_id) if not product: raise NotFound( "Product with id '{}' was not found.".format(product_id)) elif stock: product.restock(stock) product.save() return make_response(jsonify(product.serialize()), status.HTTP_200_OK) check_content_type("application/json") product.deserialize(request.get_json()) product.id = product_id product.save() return make_response(jsonify(product.serialize()), status.HTTP_200_OK)
def put(self, product_id): """ Update a product This endpoint will update a product based on the request body """ app.logger.info("Request to update product with id: %s", product_id) check_content_type("application/json") try: product_id = int(product_id) except ValueError: app.logger.info("Invalid Product ID.") api.abort(status.HTTP_400_BAD_REQUEST, "Invalid Product ID.") product = Product.find(product_id) if not product: app.logger.info("Product with id [%s] was not found.", product_id) api.abort(status.HTTP_404_NOT_FOUND, "Product with id '{}' was not found.".format(product_id)) app.logger.debug('Payload = %s', api.payload) data = api.payload if 'name' not in data or data['name'] == "": data['name'] = product.name if 'category' not in data or data['category'] == "": data['category'] = product.category if 'description' not in data or data['description'] == "": data['description'] = product.description if 'price' not in data or data['price'] == "": data['price'] = product.price try: product.deserialize(data) except DataValidationError as error: api.abort(status.HTTP_400_BAD_REQUEST, str(error)) product.update() app.logger.info("Product with id [%s] updated.", product.id) return product.serialize(), status.HTTP_200_OK
def post(self, product_id): """ Purchase a product This endpoint will purchase a product based on the request body which should include the amount, user id, and shopcart id """ app.logger.info("Request to purchase product with id: %s", product_id) check_content_type("application/json") request_body = request.get_json() if product_id == "" or 'amount' not in request_body or 'user_id' not in request_body or request_body[ 'amount'] == "" or request_body['user_id'] == "": api.abort(status.HTTP_400_BAD_REQUEST, "Fields cannot be empty") try: product_id = int(product_id) except ValueError: app.logger.info("Invalid Product ID.") api.abort(status.HTTP_400_BAD_REQUEST, "Invalid Product ID. Must be Integer") product = Product.find(product_id) if not product: api.abort(status.HTTP_404_NOT_FOUND, "Product with id '{}' was not found.".format(product_id)) try: user_id = int(request_body['user_id']) except ValueError: app.logger.info("Invalid User ID.") api.abort(status.HTTP_400_BAD_REQUEST, "Invalid User ID. Must be Integer") try: amount_update = int(request_body['amount']) except ValueError: app.logger.info("Invalid Amount.") api.abort(status.HTTP_400_BAD_REQUEST, "Invalid Amount. Must be Integer") header = {'Content-Type': 'application/json'} resp = requests.get('{}?user_id={}'.format(SHOPCART_ENDPOINT, user_id)) app.logger.info("Trying to purchase product") r_json = resp.json() if len(r_json) == 0: info_json = {"user_id": user_id} create_shopcart_resp = create_shopcart(SHOPCART_ENDPOINT, header, info_json) if create_shopcart_resp.status_code == 201: message = create_shopcart_resp.json() shopcart_id = message['id'] new_item = {} new_item["sku"] = product_id new_item["amount"] = amount_update product = product.serialize() new_item["name"] = product["name"] new_item["price"] = product["price"] add_into_shopcart = add_item_to_shopcart( SHOPCART_ENDPOINT + "/{}/items".format(shopcart_id), header, new_item) if add_into_shopcart.status_code == 201: return make_response( jsonify( message= 'Product successfully added into the shopping cart' ), status.HTTP_200_OK) return api.abort( status.HTTP_400_BAD_REQUEST, 'Product not successfully added into the shopping cart') return api.abort( status.HTTP_400_BAD_REQUEST, 'Cannot create shopcart so cannot add product into shopping cart' ) shopcart_id = r_json[0]['id'] new_item = {} new_item["sku"] = product_id new_item["amount"] = amount_update product = product.serialize() new_item["name"] = product["name"] new_item["price"] = product["price"] add_into_shopcart = add_item_to_shopcart( SHOPCART_ENDPOINT + "/{}/items".format(shopcart_id), header, new_item) if add_into_shopcart.status_code == 201: return make_response( jsonify( message='Product successfully added into the shopping cart' ), status.HTTP_200_OK) return api.abort( status.HTTP_404_NOT_FOUND, 'Product was not added in the shopping cart because of an error')