def update_order(self, order_id: int, payload: dict): """ Args: order_id: identification payload: request body Returns: """ LOGGER.info('Updating order') order = {} try: order = Order.get_by_id(order_id) if payload.get('user_id'): self.is_user_valid(payload.get('user_id')) order.user_id = payload.get('user_id', order.user_id) order.item_description = payload.get('item_description', order.item_description) order.item_quantity = payload.get('item_quantity', order.item_quantity) order.item_price = payload.get('item_price', order.item_price) order.updated_at = datetime.now() updated_rows = order.save() except UserException as error: raise error except Exception: return order if updated_rows: order = self.parse_to_json(order) return order
def findById(self, id): conn = ConnectionManagment.getConnection() cursor = conn.cursor() tuple_id = (id, ) sql = 'select * from orders where order_id = %s' cursor.execute(sql, tuple_id) item = cursor.fetchone() conn.commit() conn.close try: order = Order(item[0], item[1], item[2]) except: order = Order(None, None, None) return order
def test_when_has_multiple_orders_with_multiple_tickets_for_the_same_customer(self): orders = [Order(ORDER_ID, CUSTOMER_ID), Order(ORDER_ID_2, CUSTOMER_ID)] tickets = [Ticket(BARCODE_REFERENCE, ORDER_ID), Ticket(BARCODE_REFERENCE_2, ORDER_ID_2)] result = CustomerExtractor.extract(tickets, orders) self.assertEquals(len(result.values()), 1) self.__assert_customer( result.get(CUSTOMER_ID), CUSTOMER_ID, 0, ORDER_ID, BARCODE_REFERENCE ) self.__assert_customer( result.get(CUSTOMER_ID), CUSTOMER_ID, 1, ORDER_ID_2, BARCODE_REFERENCE_2 )
def __read_row(orders, row): if (row[1] in [None, ""]): logging.error("The order " + row[0] + " doesn't have any customer specified.") else: order = Order(row[0], row[1]) if (OrderReader.__order_id_exists(order, orders)): logging.error("The order with ID " + row[0] + " must be unique.") else: orders.append(order)
def test_when_has_one_order_with_one_ticket(self): orders = [Order(ORDER_ID, CUSTOMER_ID)] tickets = [Ticket(BARCODE_REFERENCE, ORDER_ID)] result = CustomerExtractor.extract(tickets, orders) self.assertEquals(len(result.values()), 1) self.__assert_customer( result.get(CUSTOMER_ID), CUSTOMER_ID, 0, ORDER_ID, BARCODE_REFERENCE )
def test_valid_instance_should_assert_correctly(): order = Order( user_id=data.get('user_id'), item_description=data.get('item_description'), item_quantity=data.get('item_quantity'), item_price=data.get('item_price'), total_value=(data.get('item_quantity') * data.get('item_price')) ) assert order.user_id == data.get('user_id') assert order.item_description == data.get('item_description') assert order.item_quantity == data.get('item_quantity') assert order.item_price == data.get('item_price') assert order.total_value == (data.get('item_quantity') * data.get('item_price'))
def delete_order(order_id: int): """ Args: order_id: identification Returns: Response """ LOGGER.info('Deleting an order') was_deleted = False order = Order.delete_by_id(order_id) if order: was_deleted = True return was_deleted
def get_orders(self, query: dict): """ Args: query: request query params Returns: Order list """ LOGGER.info('Getting orders') orders = [] if not query.get('order_id') and not query.get('item_description'): orders = Order.select().execute() orders = [self.parse_to_json(order) for order in orders] elif query.get('order_id') and not query.get('item_description'): ids_to_query = [ int(identify) for identify in query.get('order_id') ] orders = Order.select().where(Order.id.in_(ids_to_query)) orders = [self.parse_to_json(order) for order in orders] elif not query.get('order_id') and query.get('item_description'): orders = make_search(query.get('item_description')) orders = orders.get('hits').get('hits') return orders
def findAll(self): conn = ConnectionManagment.getConnection() cursor = conn.cursor() sql = 'select * from orders' cursor.execute(sql) data = cursor.fetchall() conn.commit() conn.close orderList = [] for item in data: product = Order(item[0], item[1], item[2]) orderList.append(product) return orderList
def get_order(self, order_id: int): """ Args: order_id: identification Returns: Order """ LOGGER.info('Getting an order') order = {} try: order = Order.get_by_id(order_id) except Exception: return order if order: order = self.parse_to_json(order) return order
def create_order(self, payload: dict): """ Args: payload: request body Returns: Order """ LOGGER.info('Creating an order') if payload.get('user_id'): self.is_user_valid(payload.get('user_id')) order = Order.create( user_id=payload.get('user_id'), item_description=payload.get('item_description'), item_quantity=payload.get('item_quantity'), item_price=payload.get('item_price'), total_value=(payload.get('item_quantity') * payload.get('item_price'))) return self.parse_to_json(order)
def test_when_has_one_order_with_any_ticket(self): orders = [ Order("1", "1") ] tickets = [Ticket("1111", "2")] result = CustomerExtractor.extract(tickets, orders) self.assertEquals(len(result.values()), 0)
def __init__(self): try: self.__connection = Mysql().database.connect() Order.create_table() except OperationalError: LOGGER.error(traceback.format_exc())