Beispiel #1
0
        public async Task <Guid> CreateOrderAsync(NewOrder newOrder)
        {
            if (!newOrder.CustomerId.HasValue)
            {
                throw new ArgumentException("Customer Id is invalid", "Order.CustomerId");
            }

            if (!newOrder.ResellerId.HasValue)
            {
                throw new ArgumentException("Reseller Id is invalid", "Order.ResellerId");
            }

            if (newOrder.Items == null || !newOrder.Items.Any())
            {
                throw new ArgumentException("An order must have at least one order line", "Order.Items");
            }

            var orderId = Guid.NewGuid();

            //Get the "Created" status
            var status = await _orderStatusRepository.GetStatusByName("Created");

            var order = new Data.Entities.Order
            {
                Id          = orderId.ToByteArray(),
                CreatedDate = DateTime.Now,
                CustomerId  = newOrder.CustomerId.Value.ToByteArray(),
                ResellerId  = newOrder.ResellerId.Value.ToByteArray(),
                StatusId    = status.Id,
                Items       = new List <Data.Entities.OrderItem>()
            };

            foreach (var item in newOrder.Items)
            {
                if (!item.ProductId.HasValue)
                {
                    throw new ArgumentException($"Product Id must be supplied ", "Order.Item.ProductId");
                }
                else
                {
                    var product = await _orderProductRepository.GetProductById(item.ProductId.Value);

                    if (product == null)
                    {
                        throw new ArgumentException($"Invalid Order Product Id : {item.ProductId}", "Order.Item.ProductId");
                    }

                    order.Items.Add(new Data.Entities.OrderItem
                    {
                        Id        = Guid.NewGuid().ToByteArray(),
                        ProductId = product.Id,
                        Quantity  = item.Quantity,
                        ServiceId = product.ServiceId
                    });
                }
            }

            await _orderRepository.AddOrder(order);

            return(orderId);
        }