public void MudarNome(Regra regra, string novoNome)
        {
            regra = ObterPorNome(regra.Nome);

            regra.MudarNome(novoNome);
            regra.Validar();

            _regraRepository.Atualizar(regra);
        }
        public void Registrar(Regra regra)
        {
            var hasRole = _regraRepository.Obter(regra.Nome);
            if (hasRole != null)
                throw new Exception(Errors.DuplicateRole);
                        
            regra.Validar();

            _regraRepository.Criar(regra);
        }
        public void AssociarRegraModulo(Regra regra, Modulo modulo)
        {
            regra = _regraRepository.Obter(regra.Id);
            modulo = _moduloRepository.Obter(modulo.Id);

            if (regra == null)
                throw new Exception(Errors.NonexistentRole);

            if (modulo == null)
                throw new Exception(Errors.NonexistentModule);

            regra.Modulos.Add(modulo);
            this._regraRepository.Atualizar(regra);
        }
        public Task<HttpResponseMessage> Registrar(Regra model)
        {
            HttpResponseMessage response = new HttpResponseMessage();

            try
            {
                _service.Registrar(model);

                response = Request.CreateResponse(HttpStatusCode.OK, model);
            }
            catch (Exception ex)
            {
                response = Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
            }
            return ApiResponse(response);
        }
        public void AssociarUsuarioRegra(Usuario usuario, Regra regra)
        {
            usuario = Obter(usuario.Email);

            regra = _regraRepository.Obter(regra.Id);


            if (usuario == null)
                throw new Exception(Errors.InvalidUserName);

            if (regra == null)
                throw new Exception(Errors.InvalidRoleName);

            usuario.Regra = regra;

            this._userRepository.Atualizar(usuario);
        }