Esempio n. 1
0
        public async Task <RetornoBaseDto> Handle(ExcluirOcorrenciaCommand request, CancellationToken cancellationToken)
        {
            var retorno = new RetornoBaseDto();

            try
            {
                var ocorrencia = await repositorioOcorrencia.ObterPorIdAsync(request.Id);

                if (ocorrencia is null)
                {
                    retorno.Mensagens.Add($"Não possível localizar a ocorrência {request.Id}.");
                    return(retorno);
                }

                if (ocorrencia.Excluido)
                {
                    return(retorno);
                }

                ocorrencia.Excluir();
                await repositorioOcorrencia.SalvarAsync(ocorrencia);
            }
            catch (Exception ex)
            {
                retorno.Mensagens.Add(ex.InnerException?.Message ?? ex.Message);
            }

            return(retorno);
        }
        public async Task <OcorrenciaDto> Handle(ObterOcorrenciaPorIdQuery request, CancellationToken cancellationToken)
        {
            var ocorrencia = await repositorioOcorrencia.ObterPorIdAsync(request.Id);

            if (ocorrencia is null)
            {
                throw new NegocioException($"Não foi possível localizar a ocorrência {request.Id}.");
            }

            var turma = await mediator.Send(new ObterTurmaPorIdQuery(ocorrencia.TurmaId));

            var alunos = await mediator.Send(new ObterAlunosPorTurmaQuery(turma.CodigoTurma));

            return(MapearParaDto(ocorrencia, alunos));
        }
        public async Task <AuditoriaDto> Handle(AlterarOcorrenciaCommand request, CancellationToken cancellationToken)
        {
            using (var transacao = unitOfWork.IniciarTransacao())
            {
                try
                {
                    var ocorrencia = await repositorioOcorrencia.ObterPorIdAsync(request.Id);

                    if (ocorrencia == null)
                    {
                        throw new NegocioException($"Ocorrencia {request.Id} não encontrada!");
                    }

                    var ocorrenciaTipo = await repositorioOcorrenciaTipo.ObterPorIdAsync(request.OcorrenciaTipoId);

                    if (ocorrenciaTipo is null)
                    {
                        throw new NegocioException("O tipo da ocorrência informado não foi encontrado.");
                    }

                    MapearAlteracoes(ocorrencia, request, ocorrenciaTipo);
                    await repositorioOcorrencia.SalvarAsync(ocorrencia);

                    var alunosParaSeremDeletados = ocorrencia.Alunos.Where(x => !request.CodigosAlunos.Contains(x.CodigoAluno)).Select(x => x.Id);
                    await repositorioOcorrenciaAluno.ExcluirAsync(alunosParaSeremDeletados);

                    var novosAlunos = request.CodigosAlunos.Where(x => !ocorrencia.Alunos.Any(y => y.CodigoAluno == x)).ToList();
                    ocorrencia.AdiconarAlunos(novosAlunos);
                    foreach (var novoAluno in novosAlunos)
                    {
                        var ocorrenciaAluno = ocorrencia.Alunos.FirstOrDefault(x => x.CodigoAluno == novoAluno);
                        await repositorioOcorrenciaAluno.SalvarAsync(ocorrenciaAluno);
                    }

                    unitOfWork.PersistirTransacao();
                    return((AuditoriaDto)ocorrencia);
                }
                catch
                {
                    unitOfWork.Rollback();
                    throw;
                }
            }
        }