public async Task <ISaida> CadastrarAgendamento(CadastrarAgendamentoEntrada cadastroEntrada) { // Verifica se as informações para cadastro foram informadas corretamente if (cadastroEntrada.Invalido) { return(new Saida(false, cadastroEntrada.Mensagens, null)); } // Verifica se a categoria existe a partir do ID informado. this.NotificarSeFalso(await _categoriaRepositorio.VerificarExistenciaPorId(cadastroEntrada.IdUsuario, cadastroEntrada.IdCategoria), CategoriaMensagem.Id_Categoria_Nao_Existe); // Verifica se a conta ou cartão de crédito existem a partir do ID informado. if (cadastroEntrada.IdConta.HasValue) { this.NotificarSeFalso(await _contaRepositorio.VerificarExistenciaPorId(cadastroEntrada.IdUsuario, cadastroEntrada.IdConta.Value), ContaMensagem.Id_Conta_Nao_Existe); } else { this.NotificarSeFalso(await _cartaoCreditoRepositorio.VerificarExistenciaPorId(cadastroEntrada.IdUsuario, cadastroEntrada.IdCartaoCredito.Value), CartaoCreditoMensagem.Id_Cartao_Nao_Existe); } // Verifica se a pessoa existe a partir do ID informado. if (cadastroEntrada.IdPessoa.HasValue) { this.NotificarSeFalso(await _pessoaRepositorio.VerificarExistenciaPorId(cadastroEntrada.IdUsuario, cadastroEntrada.IdPessoa.Value), PessoaMensagem.Id_Pessoa_Nao_Existe); } if (this.Invalido) { return(new Saida(false, this.Mensagens, null)); } var agendamento = new Agendamento(cadastroEntrada); await _agendamentoRepositorio.Inserir(agendamento); await _uow.Commit(); return(_uow.Invalido ? new Saida(false, _uow.Mensagens, null) : new Saida(true, new[] { AgendamentoMensagem.Agendamento_Cadastrado_Com_Sucesso }, new AgendamentoSaida(await _agendamentoRepositorio.ObterPorId(agendamento.Id)))); }
public async Task <ISaida> AlterarAgendamento(int idAgendamento, AgendamentoEntrada entrada) { // Verifica se as informações para alteração foram informadas corretamente if (entrada.Invalido) { return(new Saida(false, entrada.Mensagens, null)); } var agendamento = await _agendamentoRepositorio.ObterPorId(idAgendamento); // Verifica se o agendamento existe this.NotificarSeNulo(agendamento, string.Format(AgendamentoMensagem.Id_Agendamento_Nao_Existe, idAgendamento)); if (this.Invalido) { return(new Saida(false, this.Mensagens, null)); } // Verifica se o agendamento pertece ao usuário informado. this.NotificarSeDiferentes(agendamento.IdUsuario, entrada.IdUsuario, AgendamentoMensagem.Agendamento_Alterar_Nao_Pertence_Usuario); if (this.Invalido) { return(new Saida(false, this.Mensagens, null)); } // Verifica se a categoria existe a partir do ID informado. if (agendamento.IdCategoria != entrada.IdCategoria) { this.NotificarSeFalso(await _categoriaRepositorio.VerificarExistenciaPorId(entrada.IdUsuario, entrada.IdCategoria), CategoriaMensagem.Id_Categoria_Nao_Existe); } // Verifica se a conta ou cartão de crédito existem a partir do ID informado. if (agendamento.IdConta != entrada.IdConta && entrada.IdConta.HasValue) { this.NotificarSeFalso(await _contaRepositorio.VerificarExistenciaPorId(entrada.IdUsuario, entrada.IdConta.Value), ContaMensagem.Id_Conta_Nao_Existe); } else if (entrada.IdCartaoCredito.HasValue && agendamento.IdCartaoCredito != entrada.IdCartaoCredito) { this.NotificarSeFalso(await _cartaoCreditoRepositorio.VerificarExistenciaPorId(entrada.IdUsuario, entrada.IdCartaoCredito.Value), CartaoCreditoMensagem.Id_Cartao_Nao_Existe); } // Verifica se a pessoa existe a partir do ID informado. if (agendamento.IdPessoa != entrada.IdPessoa && entrada.IdPessoa.HasValue) { this.NotificarSeFalso(await _pessoaRepositorio.VerificarExistenciaPorId(entrada.IdUsuario, entrada.IdPessoa.Value), PessoaMensagem.Id_Pessoa_Nao_Existe); } if (this.Invalido) { return(new Saida(false, this.Mensagens, null)); } // Exclui todas as parcelas abertas do agendamento. foreach (var parcelaAberta in agendamento.Parcelas.Where(x => x.ObterStatus() == StatusParcela.Aberta)) { _parcelaRepositorio.Deletar(parcelaAberta); } agendamento.Alterar(entrada); _agendamentoRepositorio.Atualizar(agendamento); await _uow.Commit(); return(new Saida(true, new[] { AgendamentoMensagem.Agendamento_Alterado_Com_Sucesso }, new AgendamentoSaida(agendamento))); }