def get(self, customer_id, product_id): """ Retrieve a single shop cart item """ app.logger.info( 'Request for shopcart item with customer %s, product %s...', customer_id, product_id) item = Shopcart.find_by_customer_id_and_product_id( customer_id, product_id) if item: return item.serialize(), status.HTTP_200_OK api.abort(status.HTTP_404_NOT_FOUND, "Product not in cart")
def put(self, customer_id, product_id): """ Update an item from shopcart This endpoint will update a item for the selected product in the shopcart """ app.logger.info( 'Request to update shopcart item with customer_id: %s, product_id: %s', customer_id, product_id) cart_item = Shopcart.find_by_customer_id_and_product_id( customer_id, product_id) if not cart_item: app.logger.info( "Customer_id and product_id for update have not been found") api.abort( status.HTTP_400_BAD_REQUEST, 'No product with id [{}] found for customer id [{}].'.format( product_id, customer_id)) app.logger.debug('Payload = %s', api.payload) data = api.payload update_cart_item = Shopcart() update_cart_item.deserialize(data) try: requested_quantity = int(update_cart_item.quantity) app.logger.info("requested_quantity = %s", requested_quantity) except ValueError: app.logger.info('Non-integer quantity requested') api.abort(status.HTTP_400_BAD_REQUEST, 'Non-integer quantity given') # bounds check if requested_quantity < 1: app.logger.info('Negative quantity requested') api.abort( status.HTTP_400_BAD_REQUEST, 'No positive product with id [{}] found for customer id [{}].'. format(product_id, customer_id)) # process to update the request cart_item.quantity = requested_quantity app.logger.info("cart_item.quantity = %s", cart_item.quantity) cart_item.state = SHOPCART_ITEM_STAGE['ADDED'] cart_item.save() app.logger.info( 'Quantity for customer id %s and product id %s has been updated', customer_id, product_id) return cart_item.serialize(), status.HTTP_200_OK
def put(self, customer_id, product_id): """ Purchase an item from shopcart This endpoint will place an order for the selected product in the shopcart """ app.logger.info( 'Request to move product with id %s for customer with id %s to checkout', product_id, customer_id) cart_item = Shopcart.find_by_customer_id_and_product_id( customer_id, product_id) if cart_item is None: app.logger.info("No product with id %s found for customer id %s", product_id, customer_id) api.abort( status.HTTP_400_BAD_REQUEST, 'No product with id [{}] found for customer id [{}].'.format( product_id, customer_id)) #return make_response(jsonify(message='Invalid request params'), status.HTTP_400_BAD_REQUEST) try: post_url = "{}/orders".format(ORDER_HOST_URL) request_data = {} request_data['customer_id'] = cart_item.customer_id request_data['product_id'] = cart_item.product_id request_data['price'] = cart_item.price request_data['quantity'] = cart_item.quantity response = requests.post(url=post_url, json=request_data) app.logger.info( "Product with id %s for customer id %s moved from shopcart to order", cart_item.product_id, cart_item.customer_id) except Exception as ex: app.logger.error( "Something went wrong while moving product from shopcart to order %s", ex) cart_item.state = SHOPCART_ITEM_STAGE['DONE'] cart_item.save() app.logger.info( 'Shopcart with product id %s and customer id %s moved to checkout', product_id, customer_id) return make_response( jsonify(message="Product moved to Order Successfully", data=cart_item.serialize()), status.HTTP_200_OK)
def delete(self, customer_id, product_id): """ Delete an item from shopcart This endpoint will delete a item for the selected product in the shopcart """ app.logger.info( 'Request to delete an existing shopcart item with customer id: %s and product_id: %s', customer_id, product_id) cart_item = Shopcart.find_by_customer_id_and_product_id( customer_id, product_id) if cart_item: app.logger.info( 'Found item with customer id and product id and it will be deleted' ) cart_item.delete() # should return 204 whether item is found or not found as discussed in class return make_response('', status.HTTP_204_NO_CONTENT)
def test_find_by_customer_id_and_product_id(self): """ Test find by customer id and product id """ Shopcart(product_id= 1, customer_id= 1).save() item = Shopcart.find_by_customer_id_and_product_id(1, 1) self.assertEqual(item.customer_id, 1) self.assertEqual(item.product_id, 1)