private CredentialEntity AddRole(CredentialEntity entity, Roles role)
        {
            var credentialRoleEntity = new CredentialRoleEntity()
            {
                Credential = entity,
                Role       = new RoleEntity()
                {
                    Name            = role.ToString(),
                    CredentialRoles = new HashSet <CredentialRoleEntity>()
                }
            };

            credentialRoleEntity.Credential.CredentialRoles.Add(credentialRoleEntity);
            credentialRoleEntity.Role.CredentialRoles.Add(credentialRoleEntity);

            _credentialDataMapper.Update(entity);
            return(entity);
        }
Ejemplo n.º 2
0
        public bool AddRole(AddRoleCommand request)
        {
            var roleRequest = request.RoleRequest;

            try
            {
                var user = _credentialDataMapper.Find(x => x.Email.ToLower().CompareTo(roleRequest.Email.ToLower()) == 0).FirstOrDefault();
                if (user == null)
                {
                    throw new NullReferenceException("Email not found in the database");
                }
                else
                {
                    user.CredentialRoles.Add(new CredentialRoleEntity()
                    {
                        Credential = user,
                        Role       = new RoleEntity()
                        {
                            Name            = roleRequest.Role.ToString(),
                            CredentialRoles = new HashSet <CredentialRoleEntity>()
                        }
                    });

                    _credentialDataMapper.Update(user);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("DB exception occured with email: {0}", roleRequest?.Email);
                _logger.LogDebug(
                    "DB exception occured with email {}, it threw exception: {}. Inner exception: {}",
                    roleRequest?.Email, ex.Message, ex.InnerException?.Message
                    );

                throw new DatabaseException("Something unexpected happened while updating in the database");
            }

            return(true);
        }