Exemplo n.º 1
0
        public async Task <Guid> Handle(CriarPedidoCommand request, CancellationToken cancellationToken)
        {
            try
            {
                if (!request.IsValid())
                {
                    ValidationErrors(request);
                    return(await Task.FromResult(Guid.Empty));
                }

                var comanda = _comandaRepository.GetById(request.IDComanda);
                var garcom  = _garcomRepository.GetById(request.IDGarcom);

                var pedido = new Pedido
                {
                    Id             = GeradorGuidService.GetNexGuid(),
                    DataPedido     = DateTime.Now,
                    IDComanda      = comanda.Id,
                    Comanda        = comanda,
                    IDGarcom       = garcom.Id,
                    Garcom         = garcom,
                    Situacao       = SituacaoPedido.Aberto,
                    PedidoProdutos = new List <PedidoProduto>()
                };

                request.Produtos.ForEach(c =>
                {
                    var pedidoProduto = new PedidoProduto
                    {
                        Id         = GeradorGuidService.GetNexGuid(),
                        Produto    = _produtoRepository.GetById(c.IDProduto),
                        IDProduto  = c.IDProduto,
                        Quantidade = c.Quantidade,
                        IDPedido   = c.IDPedido,
                    };

                    pedidoProduto.CalcularValorTotal();

                    pedido.PedidoProdutos.Add(pedidoProduto);
                });

                _pedidoRepository.Add(pedido);

                return(await Task.FromResult(pedido.Id));
            }
            catch (Exception ex)
            {
                await Mediator.Publish(new DomainNotification(request.MessageType, $"Erro: {ex.Message}"), cancellationToken);

                return(await Task.FromResult(Guid.Empty));
            }
        }
        public async Task <ComandaDto> Handle(ObterComandaQuery request, CancellationToken cancellationToken)
        {
            if (request.Id == null || request.Id == Guid.Empty)
            {
                request.AddNotification("ObterComandaQuery.Id", "Id é obrigatório.");
            }

            if (request.Invalid)
            {
                await _mediator.Publish(new DomainNotification
                {
                    Erros = request.Notifications
                }, cancellationToken);

                ComandaDto comandaNull = null;

                return(await Task.FromResult(comandaNull));
            }

            return(_comandaRepository.GetById(request.Id));
        }
 public object GetById(long id)
 {
     return _mapper.Map<ComandaDTO>(_comandaRepository.GetById(id));
 }