Example #1
0
        public async Task <bool> Handle(UpdateOrderItemCommand request, CancellationToken cancellationToken)
        {
            if (!CheckCommand(request))
            {
                return(false);
            }

            var order = await _orderRepository.GetDraftOrderByClientId(request.ClientId);

            if (order == null)
            {
                await MediatorHandler.PostNotification(new DomainNotification("pedido", "Pedido não encontrado!"));

                return(false);
            }

            var pedidoItem = await _orderRepository.GetOrderItemByOrder(order.Id, request.ProductId);

            if (!order.OrderItemExistent(pedidoItem))
            {
                await MediatorHandler.PostNotification(new DomainNotification("pedido", "Item do pedido não encontrado!"));

                return(false);
            }

            order.UpdateQuantity(pedidoItem, request.Quantity);

            order.AddEvent(new OrderUpdatedEvent(order.ClientId, order.Id, order.TotalValue));
            order.AddEvent(new  OrderItemUpdatedEvent(request.ClientId, order.Id, request.ProductId, request.Quantity));

            _orderRepository.UpdateOrderItem(pedidoItem);
            _orderRepository.Update(order);

            return(await _orderRepository.UnitOfWork.Commit());
        }
Example #2
0
        public async Task <bool> Handle(RemoverOrderItemCommand request, CancellationToken cancellationToken)
        {
            if (!CheckCommand(request))
            {
                return(false);
            }

            var order = await _orderRepository.GetDraftOrderByClientId(request.ClientId);

            if (order == null)
            {
                await MediatorHandler.PostNotification(new DomainNotification("pedido", "Order not found!"));

                return(false);
            }

            var orderItem = await _orderRepository.GetOrderItemByOrder(order.Id, request.ProductId);

            if (orderItem != null && !order.OrderItemExistent(orderItem))
            {
                await MediatorHandler.PostNotification(new DomainNotification("order", "Order Item not found!"));

                return(false);
            }

            order.RemoveOrderItem(orderItem);
            order.AddEvent(new OrderUpdatedEvent(order.ClientId, order.Id, order.TotalValue));
            order.AddEvent(new OrderItemRemovedEvent(request.ClientId, order.Id, request.ProductId));

            _orderRepository.RemoveOrderItem(orderItem);
            _orderRepository.Update(order);

            return(await _orderRepository.UnitOfWork.Commit());
        }
Example #3
0
        public async Task <bool> Handle(CancelProcessOrderReverseStockCommand request, CancellationToken cancellationToken)
        {
            if (!CheckCommand(request))
            {
                return(false);
            }

            var order = await _orderRepository.GetOrderById(request.ClientId);

            if (order == null)
            {
                await MediatorHandler.PostNotification(new DomainNotification("pedido", "Pedido não encontrado!"));

                return(false);
            }

            return(await _orderRepository.UnitOfWork.Commit());
        }
Example #4
0
        public async Task <bool> Handle(ApplyVoucherOrderCommand request, CancellationToken cancellationToken)
        {
            if (!CheckCommand(request))
            {
                return(false);
            }

            var order = await _orderRepository.GetDraftOrderByClientId(request.ClientId);

            if (order == null)
            {
                await MediatorHandler.PostNotification(new DomainNotification("order", "Order not found!"));

                return(false);
            }

            var voucher = await _orderRepository.GetVoucherByCode(request.VoucherCode);

            if (voucher == null)
            {
                await MediatorHandler.PostNotification(new DomainNotification("order", "Voucher not found!"));

                return(false);
            }

            var voucherApplied = order.ApplyVoucher(voucher);

            if (!voucherApplied.IsValid)
            {
                foreach (var error in voucherApplied.Errors)
                {
                    await MediatorHandler.PostNotification(new DomainNotification(error.ErrorCode, error.ErrorMessage));
                }

                return(false);
            }

            order.AddEvent(new OrderUpdatedEvent(order.ClientId, order.Id, order.TotalValue));
            order.AddEvent(new OrderVoucherAppliedEvent(request.ClientId, order.Id, voucher.Id));

            _orderRepository.Update(order);

            return(await _orderRepository.UnitOfWork.Commit());
        }