Exemplo n.º 1
0
        public ContaCorrenteMutation(ContaBusiness business)
        {
            _business = business;

            Name = "Mutation";
            Field <ContaCorrenteType>("sacar",
                                      arguments: new QueryArguments(new QueryArgument[]
            {
                new QueryArgument <IntGraphType> {
                    Name = "conta"
                },
                new QueryArgument <FloatGraphType> {
                    Name = "valor"
                }
            }),
                                      resolve: contexto =>
            {
                int numeroConta = contexto.GetArgument <int>("conta");
                double valor    = contexto.GetArgument <int>("valor");

                Conta conta = _business.ObtemContaPorNumero(numeroConta);
                if (conta.Saldo > valor)
                {
                    conta.Saldo -= valor;
                    _business.AtualizarConta(conta);
                    return(conta);
                }
                else
                {
                    contexto.Errors.Add(new ExecutionError("Saldo insuficiente"));
                    return(null);
                }
            }
                                      );

            Field <ContaCorrenteType>("depositar",
                                      arguments: new QueryArguments(new QueryArgument[]
            {
                new QueryArgument <IntGraphType> {
                    Name = "conta"
                },
                new QueryArgument <FloatGraphType> {
                    Name = "valor"
                }
            }),
                                      resolve: contexto =>
            {
                int numeroConta = contexto.GetArgument <int>("conta");
                double valor    = contexto.GetArgument <int>("valor");

                Conta conta = _business.ObtemContaPorNumero(numeroConta);

                conta.Saldo += valor;
                _business.AtualizarConta(conta);
                return(conta);
            }
                                      );
        }