Beispiel #1
0
        public async Task <OperationResult> DeleteUser(string factory_ID, string user_Account)
        {
            if (!await IsUserExists(factory_ID, user_Account))
            {
                operationResult = new OperationResult {
                    Success = false, Message = "User not found."
                };
                return(operationResult);
            }

            // Remove user's roles
            var userRoles = _roleUserRepository.FindAll(r => r.user_account == user_Account).ToList();

            if (userRoles.Count > 0)
            {
                _roleUserRepository.RemoveMultiple(userRoles);
                try
                {
                    await _roleUserRepository.Save();
                }
                catch (System.Exception)
                {
                    operationResult = new OperationResult {
                        Success = false, Message = "Cannot delete user's roles"
                    };
                }
            }

            // Remove user
            var user = await _userRepository.FindSingle(x => x.Factory_ID == factory_ID && x.User_Account == user_Account);

            _userRepository.Remove(user);
            try
            {
                await _userRepository.Save();

                operationResult = new OperationResult {
                    Success = true, Message = "User was successfully deleted."
                };
            }
            catch (System.Exception)
            {
                operationResult = new OperationResult {
                    Success = false, Message = "Deleting user failed on save."
                };
            }

            return(operationResult);
        }