def delete(self, product_id): """Delete a Product by id""" app.logger.info('Request to delete product with the id [%s] provided', product_id) product = Product.find(product_id) if product: product.delete() return '', status.HTTP_204_NO_CONTENT
def get(self, 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: api.abort(status.HTTP_404_NOT_FOUND, "Product with id '{}' was not found.".format(product_id)) return product.serialize(), status.HTTP_200_OK
def put(self, product_id): app.logger.info('Request to update product with id: %s', product_id) check_content_type('application/json') product = Product.find(product_id) if not product: 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 product.deserialize(data) product.id = product_id product.save() return product.serialize(), status.HTTP_200_OK
def put(self, product_id): """Buy a Product by id""" app.logger.info('Request for buy a product') product = Product.find(product_id) if not product: api.abort(status.HTTP_404_NOT_FOUND, "Product with id '{}' was not found.".format(product_id)) elif product.stock == 0: api.abort( status.HTTP_409_CONFLICT, "Product with id '{}' has been sold out!".format(product_id)) else: product.stock = product.stock - 1 product.save() app.logger.info('Product with id [%s] has been bought!', product.id) return product.serialize(), status.HTTP_200_OK
def test_find_product(self): """ Find a Product by ID """ Product(name="Wagyu Tenderloin Steak", category="food", stock=11, price=20.56, description="The most decadent, succulent cut of beef, ever." ).save() shampo = Product(name="shampos", category="Health Care", stock=48, price=12.34) shampo.save() product = Product.find(shampo.id) self.assertIsNot(product, None) self.assertEqual(product.id, shampo.id) self.assertEqual(product.name, "shampos") self.assertEqual(product.category, "Health Care")