def get_prod_info(prod_id): """ Return ProductInformation identified by prod_id. --- tags: - Inventory produces: - application/json parameters: - name: prod_id in: path description: ID of Inventory entry to retrieve type: integer required: true responses: 200: description: Inventory entry returned schema: $ref: '#/definitions/Product' 404: description: Inventory entry not found """ app.logger.info("GET received, retrieve id {}.".format(prod_id)) prod_info = ProductInformation.find(prod_id) if not prod_info: raise NotFound(NOT_FOUND_MSG.format(prod_id)) return jsonify(prod_info.serialize()), status.HTTP_200_OK
def test_automatic_restock(self): """ Test automatic restocking functionality """ prod_info = ProductInformation(prod_id=1, prod_name='Storm Trooper', \ new_qty=20, used_qty=30, open_boxed_qty=40, \ restock_level=1000, restock_amt=100) prod_info.save() retrived_prod_info = ProductInformation.find(prod_id=1) self.assertIsNotNone(retrived_prod_info) total_qty = retrived_prod_info.new_qty + retrived_prod_info.used_qty \ + retrived_prod_info.open_boxed_qty self.assertEqual(total_qty, 1090)
def restock_action(prod_id): """ Restock new quantity. This endpoint will update the number of new_qty of the given prod_id. --- tags: - Inventory consumes: - application/json definitions: Restock_Amount: type: object properties: restock_amt: type: integer minimum: 0 description: Amount to be added to the product's new_amt field. parameters: - name: prod_id in: path description: ID of product. type: integer required: true - in: body name: body required: true schema: id: data $ref: '#/definitions/Restock_Amount' responses: 200: description: Product restocked successfully. 400: description: Bad Request (invalid input data) """ check_content_type(JSON) app.logger.info("PUT received, restock id {} with {}.".format( prod_id, request.get_json())) prod_info = ProductInformation.find(prod_id) if not prod_info: raise NotFound(NOT_FOUND_MSG.format(prod_id)) data = request.get_json() add_amt = data.get('restock_amt') if (len(list(data.keys())) != 1) or (add_amt is None) or (add_amt < 0): raise BadRequest("Please only give 'restock_amt' as input.") prod_info.restock(add_amt) prod_info.save() return make_response(jsonify(prod_info.serialize()), status.HTTP_200_OK)
def update_prod_info(prod_id): """ Update ProdcutInformation This endpoint will update product inventory information (by id) based on data posted in the body --- tags: - Inventory consumes: - application/json produces: - application/json parameters: - name: prod_id in: path description: ID of product information to be updated type: integer required: true - in: body name: body schema: id: data $ref: '#/definitions/Product' responses: 200: description: Inventory information Updated schema: $ref: '#/definitions/Product' 400: description: Bad Request (the posted data was not valid) """ check_content_type(JSON) app.logger.info("PUT received, update id {} with payload {}.".format( prod_id, request.get_json())) prod_info = ProductInformation.find(prod_id) if not prod_info: raise NotFound(NOT_FOUND_MSG.format(prod_id)) prod_info.deserialize_update(request.get_json()) prod_info.save() return make_response(jsonify(prod_info.serialize()), status.HTTP_200_OK)
def delete_prod_info(prod_id): """ Deletes a ProductInformation This endpoint will delete a ProductInformation based on the prod_id specified in the path. Should always return 200 OK. --- tags: - Inventory parameters: - in: path name: prod_id type: integer required: true description: prod_id of the product information to be deleted. responses: 200: description: Product information deleted. """ app.logger.info("DELETE received, delete id {}.".format(prod_id)) prod_info = ProductInformation.find(prod_id) if prod_info: prod_info.delete() return make_response('', status.HTTP_200_OK)
def create_prod_info(): """ Creates a ProductInformation This endpoint will create a ProductInformation based the data in the body that is posted --- tags: - Inventory consumes: - application/json produces: - application/json definitions: Product: type: object properties: prod_id: type: integer description: Unique ID of the product. prod_name: type: string description: Name for the product. new_qty: type: integer description: Quantity of condition "new". used_qty: type: integer description: Quantity of condition "used". open_boxed_qty: type: integer description: Quantity of condition "open boxed". restock_level: type: integer minimum: -1 description: Bottom line of a product's quantity with condition "new". restock_amt: type: integer description: Quantity to be added to a product's quantity with condition "new". parameters: - in: body name: body required: true schema: id: data required: - prod_id - prod_name $ref: '#/definitions/Product' responses: 201: description: Product information created schema: $ref: '#/definitions/Product' 400: description: Bad Request (invalid posted data) """ check_content_type(JSON) app.logger.info("POST received, create with payload {}.".format( request.get_json())) prod_info = ProductInformation() prod_info.deserialize(request.get_json()) if ProductInformation.find(prod_info.prod_id): raise BadRequest(CANNOT_CREATE_MSG.format(prod_info.prod_id)) prod_info.save() message = prod_info.serialize() location_url = url_for(GET_PROD_INFO, prod_id=prod_info.prod_id, _external=True) return make_response(jsonify(message), status.HTTP_201_CREATED, {LOCATION: location_url})