예제 #1
0
        public async Task <IActionResult> CreateItem(Guid orderId, [FromBody] DataContracts.OrderItem itemDto, CancellationToken cancellationToken)
        {
            var order = await repository.GetOrder(orderId, cancellationToken);

            if (order == null)
            {
                return(NotFound());
            }

            OrderItem item;

            try
            {
                item = order.Items.SingleOrDefault(x => x.ProductId == itemDto.ProductId);
                if (item != null)
                {
                    item.Amount += itemDto.Amount;
                }
                else
                {
                    item = order.AddItem(itemDto.ProductId, itemDto.Amount);
                }
            }
            catch (DataValidationException ex)
            {
                return(BadRequest(new DataContracts.Error(ex.Message)));
            }

            await repository.StoreOrder(order, cancellationToken);

            var orderUrl = CreateItemUrl(order, item);

            return(Created(orderUrl, item.ToDto(orderUrl)));
        }
예제 #2
0
        public async Task <IActionResult> CreateOrder([FromBody] DataContracts.Order orderDto, CancellationToken cancellationToken)
        {
            Order newOrder;

            try
            {
                newOrder = new Order(Guid.NewGuid(), orderDto.CustomerId);
            }
            catch (DataValidationException ex)
            {
                return(BadRequest(new DataContracts.Error(ex.Message)));
            }

            await repository.StoreOrder(newOrder, cancellationToken);

            var newOrderUrl = CreateOrderUrl(newOrder);

            return(Created(newOrderUrl, newOrder.ToDto(newOrderUrl)));
        }