def __init__(self): self.__category_repo = CategoryRepository() self.__sub_category_repo = SubCategoryRepository() self.__product_repo = ProductRepository() self.__product_factory = ProductFactory(self.__category_repo, self.__sub_category_repo, self.__product_repo)
class ProductMaintenance: # This layer is responsible to accept the UI/APIs params and apply the Repos to get and persist data def __init__(self): self.__category_repo = CategoryRepository() self.__sub_category_repo = SubCategoryRepository() self.__product_repo = ProductRepository() self.__product_factory = ProductFactory(self.__category_repo, self.__sub_category_repo, self.__product_repo) def get_products_list(self): return self.__product_repo.get_all() def get_product(self, _id): return self.__product_repo.get_by_id(_id) def create_product(self, name, gtin, price, sub_category_id): new_product = self.__product_factory.default_product_with(name, gtin, price, sub_category_id) self.__product_repo.save(new_product) return new_product def update_product(self, _id, name, gtin, price, sub_category_id): product = self.__product_factory.default_product_to_update(_id, name, gtin, price, sub_category_id) self.__product_repo.save(product)
def __init__(self, product_repo=None, supermarket_dao=None, product_supermarket_repo=None): self.__product_repo = product_repo if product_repo else ProductRepository( ) self.__supermarket_dao = supermarket_dao if supermarket_dao else SupermarketDAO( ) self.__product_supermarket_repo = product_supermarket_repo if product_supermarket_repo else \ ProductSupermarketRepository() self.__product_supermarket_factory = ProductSupermarketFactory( self.__product_repo, self.__supermarket_dao, self.__product_supermarket_repo)
def setUp(self): with app.app_context(): db.create_all() self.category_repo = CategoryRepository() self.sub_category_repo = SubCategoryRepository() self.product_repo = ProductRepository() self.supermarket_dao = SupermarketDAO() self.product_supermarket_repo = ProductSupermarketRepository() self.category = Category(_id='bd932a425cf040509a6071ea9b4bc138', name='Breads') self.sub_category = SubCategory(_id='5bcd3cca9a3e4065a248c821cd2f1f32', name='Gluten Free Breads', category=self.category) self.category_repo.save(self.category) self.sub_category_repo.save(self.sub_category) self.app_context = app.app_context
def get_supermarkets_to_tracking_product(self, product_id): product = ProductRepository().get_by_id(product_id) if not product: raise ProductNotFoundException(product_id) active_supermarkets = SupermarketDAO().get_actives() product_supermarkets = super().session.query( ProductSupermarket).filter_by(product_id=product_id).all() supermarkets_to_track = [] for active_supermarket in active_supermarkets: found = False for product_supermarket in product_supermarkets: if product_supermarket.supermarket == active_supermarket: supermarkets_to_track.append(product_supermarket) found = True if not found: supermarkets_to_track.append( ProductSupermarket(product=product, supermarket=active_supermarket, price_update_url=None)) return supermarkets_to_track
print('after delete the Jelly:') for product in prod_repo.get_all(): print(product) print('deleting the remaining products...') for product in prod_repo.get_all(): prod_repo.delete(product) prod_repo.commit() print('Quantity of products after all deleting:', len(prod_repo.get_all())) if __name__ == '__main__': category_repo = CategoryRepository() sub_category_repo = SubCategoryRepository() prod_repo = ProductRepository() product_supermarket_repo = ProductSupermarketRepository() # make_load() # print('consulting all Categories') # for category in category_repo.get_all(): # print(category) # # print('consulting all SubCategories') # for sub_category in sub_category_repo.get_all(): # print(sub_category) # # print('consulting all Products') # for product in prod_repo.get_all(): # print(product)
def create_db(): db.create_all() category_repo = CategoryRepository() sub_category_repo = SubCategoryRepository() prod_repo = ProductRepository() supermarket_dao = SupermarketDAO() # Categories if not category_repo.get_all(): drinks = Category(name='Drinks') jellies = Category(name='Breads') breads = Category(name='Jellies') categories = [drinks, breads, jellies] category_repo.save_many(categories) category_repo.commit() else: drinks = category_repo.get_by_name(name='Drinks') breads = category_repo.get_by_name(name='Breads') jellies = category_repo.get_by_name(name='Jellies') # SubCategories if not sub_category_repo.get_all(): milks = SubCategory(name='Milks', category=drinks) low_calorie_jellies = SubCategory(name='Low Calories Jellies', category=jellies) white_breads = SubCategory(name='White Breads', category=breads) subcategories = [milks, low_calorie_jellies, white_breads] sub_category_repo.save_many(subcategories) sub_category_repo.commit() else: milks = sub_category_repo.get_by_name(name='Milks') low_calorie_jellies = sub_category_repo.get_by_name( name='Low Calories Jellies') white_breads = sub_category_repo.get_by_name(name='White Breads') # Products if not prod_repo.get_all(): products = [ Product(name='Milk A', price=3.99, gtin='1234', sub_category=milks), Product(name='Milk B', price=4.01, gtin='1235', sub_category=milks), Product(name='Bread', price=0.99, gtin='4321', sub_category=white_breads), Product(name='Jelly', price=5.99, gtin='1111', sub_category=low_calorie_jellies) ] prod_repo.save_many(products) prod_repo.commit() # Supermarkets if not supermarket_dao.get_all(): supermarkets = [ SupermarketVO( _id='d441a91ce8cc4d838eb3ccf69de3f932', name='Supermercados Boa', is_active=True, module_name='src.domain.pricing_updates.boa_scraping', class_name='BoaScraping'), SupermarketVO( _id='73dadb6c0d164e6b8a48c52a8aa1091f', name='Carrefour', is_active=True, module_name='src.domain.pricing_updates.carrefour_scraping', class_name='CarrefourScraping') ] supermarket_dao.save_many(supermarkets)