class ProductsService: name = 'products' storage = dependencies.Storage() @rpc def get(self, product_id): product = self.storage.get(product_id) return schemas.Product().dump(product).data @rpc def list(self): products = self.storage.list() return schemas.Product(many=True).dump(products).data @rpc def create(self, product): product = schemas.Product(strict=True).load(product).data self.storage.create(product) @event_handler('orders', 'order_created') def handle_order_created(self, payload): for product in payload['order']['order_details']: self.storage.decrement_stock(product['product_id'], product['quantity'])
class ProductsService: name = "products" storage = dependencies.Storage() @grpc def create_product(self, request, context): self.storage.create( { "id": request.id, "title": request.title, "passenger_capacity": request.passenger_capacity, "maximum_speed": request.maximum_speed, "in_stock": request.in_stock, } ) return request @grpc def get_product(self, request, context): product_id = request.id product = self.storage.get(product_id) return Product(**product) @grpc def list_products(self, request, context): products = self.storage.list() return Products(products=[{**product} for product in products]) @event_handler("orders", "order_created") def handle_order_created(self, payload): for product in payload["order"]["order_details"]: self.storage.decrement_stock(product["product_id"], product["quantity"])
class ProductsService: name = 'products' storage = dependencies.Storage() @grpc def get_product(self, request, context): logger.info("------- Get Products -------- %s", request) product = self.storage.get(request.id) return Product(**product) @rpc def list(self): products = self.storage.list() return schemas.Product(many=True).dump(products).data @rpc def create(self, product): product = schemas.Product(strict=True).load(product).data self.storage.create(product) @grpc def update_products_inventory(self, request, context): logger.info("------- Update Product Inventory ------- %s", request) for productDetails in request.updateproductinventorydetails: logger.info(str(productDetails.id) +" "+ str(productDetails.quantity)) self.storage.decrement_stock(productDetails.id, productDetails.quantity) updatedInventory = [ UpdateInventoryResponseDetails( id = str(productDetails.id), isupdated = True, ) ] response = UpdateInventoryResponse( updateproductinventoryresponse = updatedInventory ) return response
class ProductsService(object): name = 'products' storage = dependencies.Storage() def __init__(self): self.logger = init_logger(name=self.name) @rpc def get(self, product_id): try: product = self.storage.get(product_id) except ValueError: raise except Exception as ex: self.logger.info(ex) raise return schemas.Product().dump(product).data @rpc def list(self): products = self.storage.list() return schemas.Product(many=True).dump(products).data @rpc def getSpecificList(self, product_ids_str): products_list = self.storage.getSpecificList(product_ids_str) return schemas.Product(many=True).dump(products_list).data @rpc def checkSpecificList(self, order_data): # Get product ids from the order into a list of String product_id_list = [] for item in order_data['order_details']: product_id_list.append(item['product_id']) product_ids_str = ','.join(product_id_list) products_list = self.storage.getSpecificList(product_ids_str) return schemas.Product(many=True).dump(products_list).data @rpc def create(self, product): validated_product = schemas.Product(strict=True).load(product).data self.storage.create(validated_product) @rpc def createList(self, products): product_list = schemas.ProductList(strict=True).load(products).data self.storage.createList(product_list) @rpc def delete(self, product): try: validated_product = schemas.Product(strict=True).load(product).data self.storage.delete(validated_product) except Exception as ex: self.logger.info(ex) raise # @event_handler('orders', 'order_created') @rpc def handleOrderCreated(self, payload): for product in payload['order']['order_details']: self.storage.decrement_stock(product['product_id'], product['quantity']) @rpc def handleOrderUpdated(self, payload): for product in payload['order']['order_details']: self.storage.decrement_stock(product['product_id'], product['quantity']) @rpc def handleCartUpdated(self, payload): for product in payload['order']['order_details']: self.storage.check_stock(product['product_id'], product['quantity'])