def get_suppliers(supplier_id): """ Retrieve a single Supplier This endpoint will return a Supplier based on it's id """ app.logger.info("Request for Supplier with id: %s", supplier_id) supplier = Supplier.find_or_404(supplier_id) return make_response(jsonify(supplier.serialize()), status.HTTP_200_OK)
def get_suppliers_products(supplier_id): """ Retrieve a single Supplier's prpoducts This endpoint will return a Supplier's products based on Supplier's id """ app.logger.info("Request for Supplier's products for id: %s", supplier_id) supplier = Supplier.find_or_404(supplier_id) results = supplier.products products = [product.serialize() for product in results] return make_response(jsonify(products), status.HTTP_200_OK)
def test_find_or_404_found(self): """ Find or return 404 found """ suppliers = SupplierFactory.create_batch(3) for supplier in suppliers: supplier.create() supplier = Supplier.find_or_404(suppliers[1].id) self.assertIsNot(supplier, None) self.assertEqual(supplier.id, suppliers[1].id) self.assertEqual(supplier.name, suppliers[1].name)
def test_find_or_404(self): """ Find or throw 404 error """ supplier = self._create_supplier() supplier.create() # Assert that it was assigned an id and shows up in the database self.assertEqual(supplier.id, 1) # Fetch it back supplier = Supplier.find_or_404(supplier.id) self.assertEqual(supplier.id, 1)
def create_products(supplier_id): """ Create a Product on a Supplier This endpoint will add a product to a supplier """ app.logger.info("Request to add an product to an supplier") check_content_type("application/json") supplier = Supplier.find_or_404(supplier_id) product = Product() product.deserialize(request.get_json()) supplier.products.append(product) supplier.save() message = product.serialize() return make_response(jsonify(message), status.HTTP_201_CREATED)