public Task <CommandResult <Cliente> > Handle(CadastrarClienteCommand request, CancellationToken cancellationToken)
        {
            var response = new CommandResult <Cliente>("Salvo com sucesso", true, null);

            request.Validate();
            if (request.Valid)
            {
                Nome      nome             = new Nome(request.PrimeiroNome, request.SegundoNome);
                Documento documentocliente = null;

                // validar tipo de pessoa
                switch (request.TipoPessoa)
                {
                case ETipoPessoa.Fisica:
                    documentocliente = new Documento(request.Documento, ETipoDocumento.Cpf);
                    break;

                case ETipoPessoa.Juridica:
                    documentocliente = new Documento(request.Documento, ETipoDocumento.Cnpj);
                    break;
                }

                // validar se tipo de documento foi definido
                if (!(documentocliente == null))
                {
                    Cliente cliente = new Cliente(nome, request.TipoPessoa, documentocliente, request.Rg);
                    if (cliente.Valid)
                    {
                        var xx = _clienteRepositorio.GetList(x => x.TipoPessoa == ETipoPessoa.Fisica);
                        if (_clienteRepositorio.ExisteCliente(cliente))
                        {
                            response.Sucesso  = false;
                            response.Mensagem = "Cliente já foi cadastrado com esse documento.";
                            return(Task.FromResult(response));
                        }
                        if (_clienteRepositorio.Insert(ref cliente))
                        {
                            response.ObjetoResposta = cliente;
                            return(Task.FromResult(response));
                        }
                    }
                    response.Notificacoes.AddRange(cliente.Notifications.Select(x => x.Message).ToList());
                }
                AddNotification("TipoPessoa", "Tipo de documento relátivo ao cliente não foi informado.");
            }
            response.Notificacoes.AddRange(request.Notifications.Select(x => x.Message).ToList());
            response.Sucesso  = false;
            response.Mensagem = "Não foi possível cadastrar cliente.";
            return(Task.FromResult(response));
        }
예제 #2
0
        public async Task <IActionResult> Post([FromBody] CadastrarClienteCommand command)
        {
            command.Validate();
            if (command.Invalid)
            {
                return(BadRequest(JsonConvert.SerializeObject(command.Notifications, Formatting.Indented)));
            }
            var response = await _mediator.Send(command);

            if (!response.Success)
            {
                return(BadRequest(response.Message));
            }
            return(CreatedAtAction(nameof(GetClientePorId), new { id = response.ResourceId }, Guid.Parse(response.ResourceId)));
        }
        public async ValueTask <ICommandResult> Handle(CadastrarClienteCommand command)
        {
            if (!command.Validate())
            {
                AddNotifications(command);
                return(new CommandResult(false, base.Notifications));
            }

            var entity = new Cliente(command.Nome, command.Identidade, command.Email, command.DataNascimento, command.CPF);

            await _clienteRepository.Add(entity);

            var result = await _clienteRepository.SaveChanges().ConfigureAwait(true);

            if (!result)
            {
                return(new CommandResult(false));
            }

            return(new CommandResult(true));
        }