Пример #1
0
def like_recommendation(id):
    """ Increase the number of likes for a recommendation with a specific id
    ---
    tags:
      - Recommendations
    path:
      - /recommendations/<int:id>/likes
    produces:
      - application/json
    parameters:
      - name: id
        in: path
        description: The unique id of a recommendation
        type: integer
        required: true
    responses:
      200:
        description: Recommendation updated
      404:
        description: Recommendation not found
    """
    recommendation = Recommendation.find(id)
    if not recommendation:
        message = {
            'error':
            'Recommendation with product_id: %s was not found' % str(id)
        }
        return_code = HTTP_404_NOT_FOUND
    else:
        recommendation.likes += 1
        recommendation.save()
        message = recommendation.serialize()
        return_code = HTTP_200_OK

    return jsonify(message), return_code
Пример #2
0
def update_recommendations(id):
    """ Updates a recommendation with a specific id
    ---
    tags:
      - Recommendations
    path:
      - /recommendations/<int:id>
    produces:
      - application/json
    parameters:
      - name: id
        in: path
        description: The unique id of a recommendation
        type: integer
        required: true
    responses:
      200:
        description: Recommendation updated
      404:
        description: Recommendation not found
    """
    recommendation = Recommendation.find(id)
    if recommendation:
        payload = request.get_json()
        recommendation.deserialize(payload)
        recommendation.save()
        message = recommendation.serialize()
        return_code = HTTP_200_OK
    else:
        message = {
            'error': 'Recommendation with id: %s was not found' % str(id)
        }
        return_code = HTTP_404_NOT_FOUND

    return jsonify(message), return_code
Пример #3
0
def get_recommendations(id):
    """ Retrieves a Recommendation with a specific id
    ---
    tags:
      - Recommendations
    path:
      - /recommendations/<int:id>
    parameters:
      - name: id
        in: path
        description: The unique id of a recommendation
        type: integer
        required: true
    responses:
      200:
        description: Recommendation returned
      404:
        description: Recommendation not found
    """
    recommendation = Recommendation.find(id)
    if recommendation:
        message = recommendation.serialize()
        return_code = HTTP_200_OK
    else:
        message = {
            'error': 'Recommendation with id: %s was not found' % str(id)
        }
        return_code = HTTP_404_NOT_FOUND

    return jsonify(message), return_code
Пример #4
0
def delete_recommendations(id):
    """ Removes a recommendation from the database that matches the id
    This endpoint will delete a recommendation based on its id
    ---
    tags:
        - Recommendations
    description: Delete a recommendation from the database
    parameters:
        - name: id
          in: path
          description: ID of a recommendation to delete
          type: integer
          required: true
    responses:
        204:
            description: Recommendation deleted
    """
    recommendation = Recommendation.find(id)
    if recommendation:
        recommendation.delete()
    return make_response('', HTTP_204_NO_CONTENT)
Пример #5
0
 def test_recommend_not_found(self):
     """ Test for a Recommend that doesn't exist """
     Recommendation(id=0, product_id=PS4).save()
     recommendation = Recommendation.find(2)
     self.assertIs(recommendation, None)
Пример #6
0
 def test_find_with_no_recommendation(self):
     """ Find a Recommend with no Recommends """
     recommendation = Recommendation.find(1)
     self.assertIs(recommendation, None)