Exemplo n.º 1
0
    def get(self, brand_id):
        """" Endpoint to get a single brand """

        brand_schema = BrandSchema()
        brand = brand_schema.dump(Brand.find_by_id(brand_id))

        if not brand:
            error_response['message'] = 'Brand not found'
            return error_response, 404

        success_response['message'] = 'Brand successfully fetched'
        success_response['data'] = {'brand': brand}

        return success_response, 200
Exemplo n.º 2
0
    def delete(self, brand_id):
        """" Endpoint to delete a brand """

        brand_schema = BrandSchema()
        brand = Brand.find_by_id(brand_id)

        if not brand:
            error_response['message'] = 'Brand not found'
            return error_response, 404

        brand.delete()

        success_response['message'] = 'Brand successfully deleted'
        success_response['data'] = {'brand': brand_schema.dump(brand)}

        return success_response, 200
Exemplo n.º 3
0
    def validate_brand(cls, brand_id):
        """
        Checks if the provided brand ID is valid

        Args:
            brand_id (int): brand ID
        Raises:
            (ValidationError): raise an exception if the brand ID doesn't exist in the database
        """

        if brand_id is not None:
            if not is_positive_integer(brand_id):
                raise_validation_error(
                    'The brand ID should be a positive integer')

            if not Brand.find_by_id(brand_id):
                raise_validation_error('The brand ID provided doesn\'t exist')
Exemplo n.º 4
0
    def put(self, brand_id):
        """" Endpoint to update brand """

        brand_schema = BrandSchema()
        brand = Brand.find_by_id(brand_id)

        if not brand:
            error_response['message'] = 'Brand not found'
            return error_response, 404

        request_data = request.get_json()
        BrandValidators.validate(request_data, brand_id=brand_id)
        request_data = request_data_strip(request_data)
        request_data['name'] = request_data['name'].lower()

        brand.update(request_data)

        success_response['message'] = 'Brand successfully updated'
        success_response['data'] = {'brand': brand_schema.dump(brand)}

        return success_response, 200