public CustomerOrderItemView AddItem(Guid id,
                                             CustomerOrderItemDto dto)
        {
            var order = _repository.GetAll().FirstOrDefault(o => o.Id == id);

            if (order == null)
            {
                throw new ArgumentException("Order not found.");
            }

            var product = _productRepository.GetAll().FirstOrDefault(p => p.Id == dto.ProductId);

            if (product == null)
            {
                throw new ArgumentException("Product not found.");
            }

            CustomerOrderItem item = order.AddItem(product, dto.Amount);

            _repository.Update(order);

            return(new CustomerOrderItemView()
            {
                Id = item.Id,
                ProductId = item.ProductId,
                Product = item.Product.Name,
                Amount = item.Amount,
                Price = item.Price,
                TotalValue = item.TotalValue
            });
        }
Exemple #2
0
 public IActionResult AddCustomerOrderItem(Guid id, [FromBody] CustomerOrderItemDto dto)
 {
     try
     {
         CustomerOrderItemView itemView = _customerOrderServices.AddItem(id, dto);
         return(Ok(itemView));
     }
     catch (ArgumentException e)
     {
         return(BadRequest(e.Message));
     }
 }
Exemple #3
0
        public IActionResult UpdateCustomerOrderItem(Guid id, [FromBody] CustomerOrderItemDto dto)
        {
            try
            {
                _customerOrderItemServices.UpdateCustomerOrderItem(id, dto);
                return(Ok());
            }
            catch (ArgumentException e)
            {
                if (e.Message == "Item not found.")
                {
                    return(NotFound());
                }

                return(BadRequest(e.Message));
            }
        }
Exemple #4
0
        public void UpdateCustomerOrderItem(Guid id, CustomerOrderItemDto dto)
        {
            var item = _repository.GetById(id);

            if (item == null)
            {
                throw new ArgumentException("Item not found.");
            }

            if (dto.Amount != 0)
            {
                item.ChangeAmount(dto.Amount);
            }

            if (dto.Price != 0)
            {
                item.ChangePrice(dto.Price);
            }
        }