Exemplo n.º 1
0
        public IActionResult Editar(Guid id, Tarefa tarefa)
        {
            try
            {
                //Pega o valor do usuário que esta logado
                var usuarioid = HttpContext.User.Claims.FirstOrDefault(
                    c => c.Type == JwtRegisteredClaimNames.Jti
                    );

                //Busca uma tarefa pelo seu Id
                var tarefaexiste = _tarefaRepository.BuscarPorId(id);

                //Verifica se a tarefa existe
                if (tarefaexiste == null)
                {
                    return(NotFound());
                }

                //Verifica se a tarefa é do usuário logado
                if (tarefaexiste.UsuarioId != new Guid(usuarioid.Value))
                {
                    return(Unauthorized("Usuário não tem permissão"));
                }

                //Atribui o valor do Id da tarefa ao id recebido como parametro na url
                tarefa.Id = id;
                _tarefaRepository.Editar(tarefa);

                return(Ok(tarefa));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Exemplo n.º 2
0
 public IActionResult Editar([FromForm] Tarefa tarefa, int projetoId)
 {
     if (ModelState.IsValid)
     {
         if (tarefa.PessoaId != null)
         {
             tarefa.Pessoa = _usuarioRepository.GetId(tarefa.PessoaId.GetValueOrDefault());
         }
         _tarefaRepository.Editar(tarefa);
         return(RedirectToAction("Todas", "Tarefa", new { id = tarefa.ProjetoId }));
     }
     ViewBag.Projeto      = _projetoRepository.ObterPorId(projetoId);
     ViewBag.Tarefa       = _tarefaRepository.ObterPorId(tarefa.Id);
     ViewBag.Projetos     = _projetoRepository.ObterTodos();
     ViewBag.MensagemErro = "Preencha todos os campos!";
     return(View());
 }