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

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

            var item = order.Items.SingleOrDefault(x => x.Id == itemId);

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

            if (item.ProductId != itemDto.ProductId)
            {
                return(BadRequest(new DataContracts.Error("Product cannot be changed.")));
            }

            try
            {
                item.Amount = itemDto.Amount;
            }
            catch (DataValidationException ex)
            {
                return(BadRequest(new DataContracts.Error(ex.Message)));
            }

            await repository.StoreOrder(order, cancellationToken);

            return(Ok(item.ToDto(CreateItemUrl(order, item))));
        }
Example #2
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)));
        }