Example #1
0
    def put(self, beer_id):
        beer_name = self.beer_args['beer_name']
        description = self.beer_args['description']
        harmonization = self.beer_args['harmonization']
        color = self.beer_args['color']
        alcohol = self.beer_args['alcohol']
        temperature = self.beer_args['temperature']
        beer_ingredients = self.ingredients_args['ingredients']

        query_beer = BeerModel.get_beer_id(beer_id)
        if not query_beer:
            return error_does_not_exist(None, beer_id)

        for ingredient in beer_ingredients['list']:
            query_ingredient = IngredientsModel.get_ingredient_id(
                ingredient['id'])
            if not query_ingredient:
                return error_does_not_exist(None, ingredient['id'])

            data_ingredient = {
                'ingredient_name': ingredient['ingredient_name'],
                'beer_id': beer_id
            }
            IngredientsModel.update(query_ingredient, data_ingredient)

        data_beer = {
            'beer_name': beer_name.lower(),
            'description': description,
            'harmonization': harmonization,
            'color': color,
            'alcohol': alcohol,
            'temperature': temperature
        }
        BeerModel.update(query_beer, data_beer)
        return resp_updated_successfully('Beer')
Example #2
0
    def get(beer_id):
        query_beer = BeerModel.get_beer_id(beer_id)
        if not query_beer:
            error_does_not_exist(None, beer_id)

        serialized = beers_serializer(query_beer, False)
        query_filter = IngredientsModel.filter_beer_id(beer_id)

        count = 0
        ingredients_list = []
        for item in query_filter:
            ingredients_list.append(ingredients_serializer(item, False))
            serialized['ingredients'] = ingredients_list
            count += 1
        return resp_content_successfully(serialized)
Example #3
0
    def delete(ingredient_id):
        query_ingredient = IngredientsModel.get_ingredient_id(ingredient_id)
        if not query_ingredient:
            return error_does_not_exist(None, ingredient_id)

        query_ingredient.delete()
        return resp_successfully('Ingredient', 202)
Example #4
0
    def post(self):
        ingredient_name = self.ingredients_args['ingredient_name']
        beer_id = self.ingredients_args['beer_id']

        query_filter = IngredientsModel.filter_beer_id(beer_id)
        if not query_filter:
            error_does_not_exist(None, beer_id)

        serialized = ingredients_serializer(query_filter, True)
        for item in serialized:
            if item['ingredient_name'] == ingredient_name:
                return error_already_exists()

        ingredients = IngredientsModel(ingredient_name=ingredient_name,
                                       beer_id=beer_id)
        ingredients.save()
        return resp_successfully('Ingredient', 201)
Example #5
0
    def get(self):
        beer_name = self.beer_args['beer_name']
        color = self.beer_args['color']
        alcohol = self.beer_args['alcohol']
        temperature = self.beer_args['temperature']
        ingredient = self.ingredients_args['ingredient']

        query_beers = BeerModel.get_beers()
        serialized = beers_serializer(query_beers, True)
        if not query_beers:
            return resp_empty_data_base()

        if beer_name:
            query_name = BeerModel.filter_beer_name(beer_name)
            error_does_not_exist(query_name, beer_name)
            serialized = serializer(query_name)
            return resp_content_successfully(serialized)

        if color:
            query_color = BeerModel.filter_beer_color(color)
            error_does_not_exist(query_color, color)
            serialized = serializer(query_color)
            return resp_content_successfully(serialized)

        if alcohol:
            query_alcohol = BeerModel.filter_beer_alcohol(alcohol)
            error_does_not_exist(query_alcohol, alcohol)
            serialized = serializer(query_alcohol)
            return resp_content_successfully(serialized)

        if temperature:
            query_temperature = BeerModel.filter_beer_temperature(temperature)
            error_does_not_exist(query_temperature, temperature)
            serialized = serializer(query_temperature)
            return resp_content_successfully(serialized)

        if ingredient:
            query_ingredient = IngredientsModel.filter_ingredient_name(
                ingredient)
            error_does_not_exist(query_ingredient, ingredient)
            serialized = add_ingredients(query_beers, serialized)
            response_parser = parser_beers(serialized, ingredient)
            return resp_content_successfully(response_parser)

        serialized = add_ingredients(query_beers, serialized)
        return resp_content_successfully(serialized)
Example #6
0
    def delete(beer_id):
        query_beer = BeerModel.get_beer_id(beer_id)
        if not query_beer:
            return error_does_not_exist(None, beer_id)

        query_ingredients = IngredientsModel.filter_beer_id(beer_id)
        for query in query_ingredients:
            ingredient = IngredientsModel.get_ingredient_id(query.id)
            ingredient.delete()
        query_beer.delete()
        return resp_deleted_successfully('Beer')
Example #7
0
    def put(self, ingredient_id):
        ingredient_name = self.ingredients_args['ingredient_name']
        beer_id = self.ingredients_args['beer_id']

        query_ingredient = IngredientsModel.get_ingredient_id(ingredient_id)
        if not query_ingredient:
            return error_does_not_exist(None, ingredient_id)

        query_filter = IngredientsModel.filter_beer_id(beer_id)
        serialized = ingredients_serializer(query_filter, True)
        for item in serialized:
            if item['ingredient_name'] == ingredient_name:
                return error_already_exists()

        data = {'ingredient_name': ingredient_name, 'beer_id': beer_id}
        IngredientsModel.update(query_ingredient, data)
        return resp_successfully('Ingredient', 201)