コード例 #1
0
class FavoritesService:
    def __init__(self):
        self.__database = Database()
        self.__parser = Parser()
        self.__validations = FavoriteValidations()

    def insert_favorite(self, product_id: str, customer_id: str):
        try:
            LOGGER.debug('Inserting product to customer list')
            self.__validations.validate_customer_product_id(product_id=product_id, customer_id=customer_id)
            customer = self.__database.get_customer_by_id(customer_id=customer_id)
            product = self.__database.get_product_by_id(product_id=product_id)
            customer = self.__database.push_product_to_favorites(customer=customer, product=product)
            return self.__parser.parse_customer_favorites(customer=customer)

        except NotUniqueError:
            LOGGER.info('Email already used')
            raise EmailAlreadyUsedException()

        except ValidationError as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.debug('Invalid fields value')
            raise InvalidFieldsValuesException(validations=exception.to_dict())

        except MissingRequiredFieldsException as exception:
            raise exception

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(exception)
            raise exception

    def remove_favorite(self, product_id: str, customer_id: str):
        try:
            LOGGER.debug('Removing product to customer list')
            self.__validations.validate_customer_product_id(product_id=product_id, customer_id=customer_id)
            customer = self.__database.get_customer_by_id(customer_id=customer_id)
            product = self.__database.get_product_by_id(product_id=product_id)
            customer = self.__database.pull_product_to_favorites(customer=customer, product=product)
            return self.__parser.parse_customer_favorites(customer=customer)

        except NotUniqueError:
            LOGGER.info('Email already used')
            raise EmailAlreadyUsedException()

        except ValidationError as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.debug('Invalid fields value')
            raise InvalidFieldsValuesException(validations=exception.to_dict())

        except MissingRequiredFieldsException as exception:
            raise exception

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(exception)
            raise exception
コード例 #2
0
 def __init__(self):
     self.__database = Database()
     self.__parser = Parser()
     self.__validations = FavoriteValidations()
コード例 #3
0
 def test_parse_customer_favorites_successfully(self):
     customer = Customer(**CUSTOMER)
     data_parsed = Parser().parse_customer_favorites(customer=customer)
     self.assertEqual(customer.name, data_parsed.get('name'))
     self.assertEqual(customer.email, data_parsed.get('email'))
     self.assertEqual(customer.favorites, data_parsed.get('favorites'))
コード例 #4
0
 def test_page_successfully_valid_number(self):
     page = Parser.page('2')
     self.assertEqual(2, page)
コード例 #5
0
 def test_page_failure_invalid_number(self):
     with self.assertRaises(Exception):
         Parser.page('2,a')
コード例 #6
0
 def test_raw_to_json_failure_incorrect_data(self):
     with self.assertRaises(Exception):
         customer = Customer(**CUSTOMER)
         Parser.raw_to_json(data=customer)
コード例 #7
0
 def test_page_successfully_none(self):
     page = Parser.page(None)
     self.assertEqual(1, page)
コード例 #8
0
 def test_raw_to_json_successfully(self):
     customer = Customer(**CUSTOMER)
     Parser.raw_to_json(data=customer.to_mongo().to_dict())
コード例 #9
0
 def __init__(self):
     self.__database = Database()
     self.__parser = Parser()
     self.__validations = CustomerValidations()
コード例 #10
0
class CustomerService:
    def __init__(self):
        self.__database = Database()
        self.__parser = Parser()
        self.__validations = CustomerValidations()

    def create_customer(self, data: dict):
        try:
            LOGGER.debug('Creating customer service')
            self.__validations.data(data=data)
            customer = Customer(**data)
            customer = self.__database.save(document=customer)
            return self.__parser.raw_to_json(
                data=customer.to_mongo().to_dict())

        except NotUniqueError:
            LOGGER.info('Email already used')
            raise EmailAlreadyUsedException()

        except ValidationError as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.debug('Invalid fields value')
            raise InvalidFieldsValuesException(validations=exception.to_dict())

        except MissingRequiredFieldsException as exception:
            raise exception

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(exception)
            raise exception

    def get_customer(self, customer_id: str):
        try:
            is_object_id(object_id=customer_id)
            LOGGER.debug(f'Getting customer {customer_id}')
            customer = self.__database.get_customer_by_id(
                customer_id=customer_id)
            return self.__parser.parse_customer_favorites(customer=customer)

        except NotFoundException as exception:
            raise exception

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(exception)
            raise exception

    def delete_customer(self, customer_id: str):
        try:
            is_object_id(object_id=customer_id)
            LOGGER.debug(f'Deleting customer service {customer_id}')
            self.__database.delete_customer_by_id(customer_id=customer_id)

        except NotFoundException as exception:
            raise exception

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(exception)
            raise exception

    def update_customer(self, data: dict, customer_id: str):
        try:
            is_object_id(object_id=customer_id)
            self.__validations.data(data=data, is_update=True)
            LOGGER.debug(f'Updating customer service {customer_id}')
            customer = self.__database.update_customer_by_id(
                customer_id=customer_id, data=data)
            return self.__parser.raw_to_json(
                data=customer.to_mongo().to_dict())

        except NotUniqueError:
            LOGGER.info('Email already used')
            raise EmailAlreadyUsedException()

        except ValueError:
            LOGGER.debug(traceback.format_exc())
            LOGGER.debug('Invalid fields value')
            raise InvalidFieldsValuesException(validations={'field': ''})

        except ValidationError as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.debug('Invalid fields value')
            raise InvalidFieldsValuesException(
                validations={exception.field_name: ''})

        except NotFoundException as exception:
            raise exception

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(exception)
            raise exception
コード例 #11
0
 def __init__(self):
     self.__database = Database()
     self.__parser = Parser()
     self.__validations = ProductValidations()
コード例 #12
0
class ProductService:
    def __init__(self):
        self.__database = Database()
        self.__parser = Parser()
        self.__validations = ProductValidations()

    def create_product(self, data: dict):
        try:
            LOGGER.debug('Creating product service')
            self.__validations.product_data(data=data)
            product = Product(**data)
            product = self.__database.save(document=product)
            return self.__parser.raw_to_json(data=product.to_mongo().to_dict())

        except ValidationError as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.debug('Invalid fields value')
            raise InvalidFieldsValuesException(validations=exception.to_dict())

        except MissingRequiredFieldsException as exception:
            raise exception

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(exception)
            raise exception

    def get_product(self, product_id: str):
        try:
            is_object_id(object_id=product_id)
            LOGGER.debug(f'Getting product {product_id}')
            product = self.__database.get_product_by_id(product_id=product_id)
            return self.__parser.raw_to_json(data=product.to_mongo().to_dict())

        except NotFoundException as exception:
            raise exception

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(exception)
            raise exception

    def delete_product(self, product_id: str):
        try:
            is_object_id(object_id=product_id)
            LOGGER.debug(f'Deleting product service {product_id}')
            self.__database.delete_product_by_id(product_id=product_id)

        except NotFoundException as exception:
            raise exception

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(exception)
            raise exception

    def update_product(self, data: dict, product_id: str):
        try:
            is_object_id(object_id=product_id)
            self.__validations.product_data(data=data, is_update=True)
            LOGGER.debug(f'Updating product service {product_id}')
            product = self.__database.update_product_by_id(
                product_id=product_id, data=data)
            return self.__parser.raw_to_json(data=product.to_mongo().to_dict())

        except ValueError:
            LOGGER.debug(traceback.format_exc())
            LOGGER.debug('Invalid fields value')
            raise InvalidFieldsValuesException(validations={'field': ''})

        except ValidationError as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.debug('Invalid fields value')
            raise InvalidFieldsValuesException(
                validations={exception.field_name: ''})

        except NotFoundException as exception:
            raise exception

        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(exception)
            raise exception

    def get_products(self, page: str):
        try:
            LOGGER.debug('Getting products service')
            parse_page = self.__parser.page(page=page)
            products, products_total = self.__database.get_products(
                page=parse_page)
            return self.__parser.products(data=list(products.as_pymongo()),
                                          total=products_total,
                                          page=parse_page)
        except Exception as exception:
            LOGGER.debug(traceback.format_exc())
            LOGGER.critical(exception)
            raise exception