public IActionResult AtribuirTrofeu([FromBody] AtribuirTrofeuRequest request)
 {
     try
     {
         _entregaDeTrofeuService.Atribuir(request, _usuarioLogado.Obter());
         return(NoContent());
     }
     catch
     {
         return(BadRequest());
     }
 }
Esempio n. 2
0
        public void Atribuir(AtribuirTrofeuRequest request, Usuario usuario)
        {
            Trofeu trofeu = _trofeuRepository.GetById(request.IdTrofeu);

            if (trofeu == null)
            {
                throw new Exception("Troféu não localizado.");
            }

            CaseDeNegocio caseDeNegocio = _caseDeNegocioService.ObterPorId(trofeu.IdCase);

            if (!_caseDeNegocioService.UsuarioEstaAssociadoAoCaseDeNegocioComoProfessor(usuario, caseDeNegocio))
            {
                throw new Exception("Usuário não possui permissão para esta solicitação.");
            }

            EntregaDeLicao entregaDeLicao = _entregaDeLicaoRepository.GetById(request.IdEntregaDeLicao);

            if (entregaDeLicao == null)
            {
                throw new Exception("Lição não localizada.");
            }
            else if (entregaDeLicao.Licao.IdCase != caseDeNegocio.Id)
            {
                throw new Exception("Lição não pode ser associada à este troféu.");
            }

            Resposta resposta = null;

            if (request.IdResposta != null && request.IdResposta > 0)
            {
                resposta = _respostaRepository.GetById(request.IdResposta.Value);
                if (resposta == null)
                {
                    throw new Exception("Questão não localizada.");
                }
                else if (resposta.IdEntregaDeLicao != entregaDeLicao.Id)
                {
                    throw new Exception("Resposta não associada à lição.");
                }
            }

            EntregaDeTrofeu entregaDeTrofeu = new EntregaDeTrofeu(trofeu, entregaDeLicao, resposta);

            Adicionar(entregaDeTrofeu);
        }