Exemplo n.º 1
0
        public async Task <bool> UpdateOrderStatusAsync(OrderStatusChangeDto statusChangeDto)
        {
            var order = await _repository.GetAll()
                        .Include(p => p.OrderDetails)
                        .Where(x => x.Id == statusChangeDto.OrderId)
                        .FirstOrDefaultAsync();

            //update order status
            order.OrderStatus = statusChangeDto.OrderStatus;

            return(true);
        }
 public IActionResult ChangeStatus([FromBody] OrderStatusChangeDto dto, [FromServices] IOrderChangeStatusCommand command)
 {
     executor.ExecuteCommand(command, dto);
     return(NoContent());
 }
Exemplo n.º 3
0
        public void Execute(OrderStatusChangeDto request)
        {
            _validator.ValidateAndThrow(request);
            var order = _context.Orders
                        .Include(o => o.OrderLines)
                        .ThenInclude(ol => ol.Product)
                        .FirstOrDefault(x => x.Id == request.OrderId);

            if (order == null)
            {
                throw new EntityNotFoundException(request.OrderId, typeof(Order));
            }
            if (order.OrderStatus == OrderStatus.Delivered)
            {
                throw new EntityConflictException("Can't change status of delivered orders");
            }

            if (order.OrderStatus == OrderStatus.Recieved)
            {
                if (request.Status == OrderStatus.Delivered)
                {
                    throw new EntityConflictException("Must be in state shipped before choosing delivered");
                }
                if (request.Status == OrderStatus.Canceled)
                {
                    foreach (var line in order.OrderLines)
                    {
                        line.Product.Quantity += line.Quantity;
                    }
                }
            }
            if (order.OrderStatus == OrderStatus.Shipped)
            {
                if (request.Status == OrderStatus.Canceled)
                {
                    throw new EntityConflictException("Order is in shipping progress - can't be canceled");
                }
            }
            if (order.OrderStatus == OrderStatus.Canceled)
            {
                if (request.Status == OrderStatus.Shipped || request.Status == OrderStatus.Delivered)
                {
                    throw new EntityConflictException("Canceled order can be changed in received only");
                }
                if (request.Status == OrderStatus.Recieved)
                {
                    foreach (var line in order.OrderLines)
                    {
                        if (line.Product != null)
                        {
                            throw new EntityConflictException("One of the products doesn't exist - you can't retrive order");
                        }
                        line.Product.Quantity -= line.Quantity;
                        if (line.Product.Quantity < 0)
                        {
                            throw new EntityConflictException("One of the products isn't available at the moment - you can't retrive order");
                        }
                    }
                }
            }
            order.OrderStatus = request.Status;
            _context.SaveChanges();
        }