def get(self):
        """ Returns all of the Recommendations """
        #app.logger.info('Listing recommendations')
        recommendations = []
        categoryId = request.args.get('categoryId')
        productId = request.args.get('productId')
        suggestionId = request.args.get('suggestionId')
        if categoryId:
            recommendations = Recommendation.find_by_categoryId(categoryId)
        elif productId:
            recommendations = Recommendation.find_by_productId(productId)
        elif suggestionId:
            recommendations = Recommendation.find_by_suggestionId(suggestionId)
        else:
            recommendations = Recommendation.all()

        #app.logger.info('[%s] Recommendations returned', len(recommendations))
        results = [
            recommendation.serialize() for recommendation in recommendations
        ]
        return results, status.HTTP_200_OK
    def put(self, categoryId):
        """
        Update a recommendation category
        This end point will update a recommendation category for all RELEVANT RECOMMENDATIONS
        based on the data in the body
        """
        results = Recommendation.find_by_categoryId(categoryId)
        if (len(results) == 0):
            # message = {'error' : 'Recommendation with categoryId: %s was not found' % str(categoryId)}
            return_code = status.HTTP_404_NOT_FOUND
            return '', return_code

        data = request.get_json()
        i = 0
        sizeOfResults = len(results)
        while i < sizeOfResults:
            recommendation = Recommendation.find(results[i].id)
            recommendation.categoryId = data['categoryId']
            recommendation.update()
            i += 1
        # message = {'success' : 'RECOMMENDATIONS category updated'}
        return '', status.HTTP_200_OK
예제 #3
0
 def test_find_by_categoryId(self):
     Recommendation(1, "productId1", "recommended1", "categoryId1").save()
     Recommendation(2, "productId2", "recommended2", "categoryId2").save()
     recommendations = Recommendation.find_by_categoryId("categoryId1")
     self.assertEqual(len(recommendations), 1)
     self.assertEqual(recommendations[0].categoryId, "categoryId1")