Exemple #1
0
        protected async Task <List <Order> > CreateSomeOrdersAsync(List <Customer> customers, List <Product> products)
        {
            var customerId = customers[1].Id;
            var productId1 = products[1].Id;
            var productId2 = products[2].Id;

            var orders = new List <Order>();

            for (var i = 0; i < 10; i++)
            {
                var orderItem1 = _orderFactory.CreateNewOrderItem(productId1, 56.92m, 40);
                var orderItem2 = _orderFactory.CreateNewOrderItem(productId2, 72.46m, 50);

                var orderItems = new List <OrderItem>
                {
                    orderItem1,
                    orderItem2,
                };

                var order = _orderFactory.CreateNewOrder(customerId, orderItems);

                await _orderRepository.AddAsync(order);
            }

            return(orders);
        }
Exemple #2
0
        private async Task <OrderItem> GetOrderItem(CreateOrderItemRequest requestOrderDetail)
        {
            var productId = new ProductIdentity(requestOrderDetail.ProductId);
            var product   = await _productReadRepository.FindAsync(productId);

            if (product == null)
            {
                throw new InvalidRequestException($"Product id {requestOrderDetail.ProductId} is not valid because that product does not exist");
            }

            var quantity = requestOrderDetail.Quantity;

            return(_orderFactory.CreateNewOrderItem(product, quantity));
        }
Exemple #3
0
        private async Task UpdateAsync(Order order, UpdateOrderRequest request)
        {
            var currentOrderDetails = order.OrderItems;

            var addedOrderRequestDetails   = request.OrderItems.Where(e => e.Id == null).ToList();
            var updatedOrderRequestDetails = request.OrderItems.Where(e => e.Id != null).ToList();
            var deletedOrderDetails        = order.OrderItems.Where(e => !request.OrderItems.Any(f => f.Id == e.Id.Value)).ToList();

            foreach (var added in addedOrderRequestDetails)
            {
                var productId = new ProductIdentity(added.ProductId);
                var product   = await _productReadRepository.FindAsync(productId);

                if (product == null)
                {
                    throw new InvalidRequestException($"Product {productId} does not exist");
                }

                var orderDetail = _orderFactory.CreateNewOrderItem(product, added.Quantity);
                order.AddOrderItem(orderDetail);
            }

            foreach (var updated in updatedOrderRequestDetails)
            {
                var orderDetailId = new OrderItemIdentity(updated.Id.Value);
                var orderDetail   = order.OrderItems.First(e => e.Id == orderDetailId);

                var productId = new ProductIdentity(updated.ProductId);
                var product   = await _productReadRepository.FindAsync(productId);

                if (product == null)
                {
                    throw new InvalidRequestException($"Product {productId} does not exist");
                }

                orderDetail.SetProduct(product);
                orderDetail.Quantity = updated.Quantity;
            }

            foreach (var deleted in deletedOrderDetails)
            {
                order.RemoveOrderItem(deleted.Id);
            }
        }