public async Task <ActionResult <Perfils> > Post([FromBody] Perfils _perfil)
        {
            try
            {
                await _repository.CadastrarPerfil(_perfil);

                return(Ok(_perfil));
            }catch (Exception _e) {
                return(BadRequest(_e.Message));
            }
        }
        public async Task <Perfils> AlterarPerfil(Perfils _perfil)
        {
            try
            {
                _contexto.Entry(_perfil).State = EntityState.Modified;
                await _contexto.SaveChangesAsync();

                return(_perfil);
            }catch (Exception _e) {
                throw new Exception(_e.Message);
            }
        }
        public async Task <Perfils> CadastrarPerfil(Perfils _perfil)
        {
            try
            {
                await _contexto.Perfils.AddAsync(_perfil);

                await _contexto.SaveChangesAsync();

                return(_perfil);
            }catch (Exception _e) {
                throw new Exception(_e.Message);
            }
        }
        public async Task <Perfils> ExcluirPerfil(Perfils _perfil)
        {
            try
            {
                _contexto.Usuarios.RemoveRange(_contexto.Usuarios.Where(usr => usr.IdPerfil == _perfil.Id));
                _contexto.Perfils.Remove(_perfil);

                await _contexto.SaveChangesAsync();

                return(_perfil);
            }catch (Exception _e) {
                throw new Exception(_e.Message);
            }
        }
        public async Task <ActionResult <Perfils> > Get(int id)
        {
            try
            {
                Perfils _perfil = await _repository.BuscarPerfilPorId(id);

                if (_perfil == null)
                {
                    return(NotFound("Nenhum perfil encontrado"));
                }

                return(Ok(_perfil));
            }catch (Exception _e) {
                return(BadRequest(_e.Message));
            }
        }
        public async Task <ActionResult <Perfils> > Delete(int id)
        {
            try
            {
                Perfils _perfilProcurado = await _repository.BuscarPerfilPorId(id);

                if (_perfilProcurado == null)
                {
                    return(NotFound("Perfil não encontrado"));
                }

                await _repository.ExcluirPerfil(_perfilProcurado);

                return(Ok(_perfilProcurado));
            }catch (Exception _e) {
                return(BadRequest(_e.Message));
            }
        }
        public async Task <ActionResult <Perfils> > Put(int id, [FromBody] Perfils _perfil)
        {
            try
            {
                await _repository.AlterarPerfil(_perfil);

                return(Ok(_perfil));
            }catch (DbUpdateConcurrencyException) {
                var _validarPerfil = _repository.BuscarPerfilPorId(id);

                if (_validarPerfil == null)
                {
                    return(NotFound());
                }

                return(BadRequest());
            }
        }
Exemple #8
0
 public bool TemPermissao(Usuario usuario)
 {
     return(Perfils.Any(perfil => perfil.Codigo == usuario.Perfil.Codigo));
 }