예제 #1
0
        public async Task <OperationResult> UpdateAsync(Guid id, OrderItem item)
        {
            var itemToUpdate = await _orderItemRepository.FindAsync(id);

            await _stockService.IncrementStock(itemToUpdate.ProductId, itemToUpdate.Amount);

            itemToUpdate.Update(item);

            var validationResult = _orderItemValidator.Validate(itemToUpdate);

            if (!validationResult.IsValid)
            {
                var errors = validationResult.Errors.Select(e => e.ErrorMessage).ToList();
                return(new OperationResult(false, itemToUpdate, errors));
            }

            await _orderItemRepository.UpdateAsync(itemToUpdate);

            await _stockService.DecrementStock(itemToUpdate.ProductId, itemToUpdate.Amount);

            await _unitOfWork.CommitAsync();

            await UpdateOrderTotal(item.OrderId);

            return(new OperationResult(true, itemToUpdate, null));
        }
        public async Task <ActionResult <OrderItemDto> > GetOrderItems(string id)
        {
            var orderItem = _mapper.Map <OrderItemDto>(await _orderItemRepository.FindAsync(id));

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

            return(orderItem);
        }