예제 #1
0
    def delete(self, product_id, related_product_id):
        """
        Delete a recommendation
        This endpoint will delete one unique recommendation based on
        the product id and related product id provided in the URI
        """
        app.logger.info(
            "Request to delete a recommendation by product id and related product id"
        )

        find_result = Recommendation.find_by_id_relid(product_id,
                                                      related_product_id)

        if not find_result.first():
            return "", status.HTTP_204_NO_CONTENT

        recommendation = find_result.first()
        app.logger.info(
            "Deleting recommendation with product id %s and related product id %s ...",
            recommendation.product_id,
            recommendation.related_product_id,
        )
        recommendation.delete()
        app.logger.info(
            "Deleted recommendation with product id %s and related product id %s ...",
            recommendation.product_id,
            recommendation.related_product_id,
        )

        return "", status.HTTP_204_NO_CONTENT
    def test_find_by_id_relid(self):
        """ Test find by product_id and related_product_id function """
        test_recommendation = self._create_one_recommendation(by_id=1,
                                                              by_rel_id=2,
                                                              by_type=1)
        find_result = Recommendation.find_by_id_relid(
            by_id=test_recommendation.product_id,
            by_rel_id=test_recommendation.related_product_id,
        )

        self.assertEqual(len(find_result.all()), 1)
        self.assertEqual(find_result.first(), test_recommendation)

        find_result_empty = Recommendation.find_by_id_relid(
            by_id=test_recommendation.product_id, by_rel_id=3)
        self.assertEqual(len(find_result_empty.all()), 0)

        self.assertRaises(TypeError, Recommendation.find_by_id_relid, 1,
                          "not_int")
        self.assertRaises(TypeError, Recommendation.find_by_id_relid,
                          "not_int", 1)
예제 #3
0
    def get(self):
        """
            Search recommendation based on query parameters

            This endpoint will return recommendation based on it's product id, related product id, type, and status.
        """

        args = recommendation_args.parse_args()
        product_id = args["product-id"]
        related_product_id = args["related-product-id"]
        type_id = args["type-id"]
        by_status = args["status"]

        if product_id == related_product_id and product_id is not None:
            raise BadRequest(
                "product_id cannot be the same as related_product_id")

        app.logger.info("Request for all recommendations in the database")

        try:
            if product_id and related_product_id:
                recommendations = Recommendation.find_by_id_relid(
                    product_id, related_product_id)
            elif product_id:
                if type_id and by_status is not None:
                    recommendations = Recommendation.find_by_id_type_status(
                        product_id, type_id, by_status)
                elif type_id:
                    recommendations = Recommendation.find_by_id_type(
                        product_id, type_id)
                elif by_status is not None:
                    recommendations = Recommendation.find_by_id_status(
                        product_id, by_status)
                else:
                    recommendations = Recommendation.find(product_id)
            elif related_product_id:
                if type_id and by_status is not None:
                    recommendations = Recommendation.find_by_relid_type_status(
                        related_product_id, type_id, by_status)
                elif type_id:
                    recommendations = Recommendation.find_by_relid_type(
                        related_product_id, type_id)
                elif by_status is not None:
                    recommendations = Recommendation.find_by_relid_status(
                        related_product_id, by_status)
                else:
                    recommendations = Recommendation.find_by_rel_id(
                        related_product_id)
            elif type_id and by_status is not None:
                recommendations = Recommendation.find_by_type_id_status(
                    type_id, by_status)
            elif type_id:
                recommendations = Recommendation.find_by_type_id(type_id)
            elif by_status is not None:
                recommendations = Recommendation.find_by_status(by_status)
            else:
                recommendations = Recommendation.all()
        except DataValidationError as error:
            raise BadRequest(str(error))
        except ValueError as error:
            raise BadRequest(str(error))

        result = []
        for rec in recommendations:
            record = rec.serialize()
            result.append(record)

        return result, status.HTTP_200_OK