public void ShowCurrentKey(APIKeyEntity key) { if (key != null) { currentKeyLabel.Content = key.KeyValue; } }
public bool AddAPIKey(APIKeyEntity key, bool useIsCurrentValue = false) { try { _repository = XDocument.Load(_repositoryPath); XElement rootElement = _repository.Element("APIKeys"); XElement keyElement = new XElement("APIKey"); keyElement.SetValue(key.KeyValue); if (useIsCurrentValue) { keyElement.SetAttributeValue("isCurrent", key.IsCurrent); } else { keyElement.SetAttributeValue("isCurrent", false); } rootElement.Add(keyElement); _repository.Save(_repositoryPath); return(true); } catch (Exception e) { return(false); throw new Exception(e.Message); } }
public void OnSelectKey(Object sender, String key) { if (CurrentKey != null) { APIKeyEntity temp = CurrentKey; _keysRepository.RemoveAPIKey(CurrentKey); _keysRepository.AddAPIKey(new APIKeyEntity(temp.KeyValue, false)); temp = _keysRepository.FindAPIKey(key); if (temp != null) { _keysRepository.RemoveAPIKey(temp); CurrentKey = new APIKeyEntity(temp.KeyValue, true); _keysRepository.AddAPIKey(CurrentKey, true); } } else { APIKeyEntity newKey = _keysRepository.FindAPIKey(key); if (newKey != null) { _keysRepository.RemoveAPIKey(newKey); CurrentKey = new APIKeyEntity(newKey.KeyValue, true); _keysRepository.AddAPIKey(CurrentKey, true); } } _view.ShowCurrentKey(CurrentKey); _view.ShowAvailableKeys <APIKeyEntity>(_keysRepository.ReadAPIKeys()); }
public void FindCurrentKeyTest() { APIKeyEntity key = new APIKeyEntity("1234", true); _apiKeyModel.AddAPIKey(key, true); var writtenKey = _apiKeyModel.FindCurrentAPIKey(); Assert.AreEqual(key.IsCurrent, writtenKey.IsCurrent); Assert.AreEqual(key.KeyValue, writtenKey.KeyValue); }
public void RemoveAPIKey(APIKeyEntity key) { _repository = XDocument.Load(_repositoryPath); XElement rootElement = _repository.Element("APIKeys"); foreach (var item in rootElement.Elements("APIKey")) { if (item.Value == key.KeyValue && Convert.ToBoolean(item.Attribute("isCurrent").Value) == key.IsCurrent) { item.Remove(); } } _repository.Save(_repositoryPath); }
public void Add_Find_Remove_APIKeyTest() { APIKeyEntity key = new APIKeyEntity("1234"); _apiKeyModel.AddAPIKey(key); var writtenKey = _apiKeyModel.FindAPIKey(key.KeyValue); Assert.IsNotNull(writtenKey); _apiKeyModel.RemoveAPIKey(key); var keysCount = _apiKeyModel.ReadAPIKeys().Count; Assert.AreEqual(0, keysCount); }
public APIKeyEntity FindAPIKey(string key) { APIKeyEntity result = null; _repository = XDocument.Load(_repositoryPath); XElement rootElement = _repository.Element("APIKeys"); foreach (var item in rootElement.Elements("APIKey")) { if (item.Value == key) { result = new APIKeyEntity(item.Value, Convert.ToBoolean(item.Attribute("isCurrent").Value)); } } _repository.Save(_repositoryPath); return(result); }
public ApiKeyController(IKeysView view) { APIKeysFileExistingChecker checker = new APIKeysFileExistingChecker(Environment.CurrentDirectory); if (!checker.IsAPIKeysFileExists) { checker.CreateKeysConfingFile(); } _view = view; _view.SetInputHandler(this); _keysRepository = new APIKeysRepository(checker.ConfigFilePath); CurrentKey = _keysRepository.FindCurrentAPIKey(); _view.ShowCurrentKey(CurrentKey); _view.ShowAvailableKeys <APIKeyEntity>(_keysRepository.ReadAPIKeys()); }
public void OnRemoveKey(Object sender, String key) { var removingKey = _keysRepository.FindAPIKey(key); if (removingKey != null) { if (CurrentKey != null) { if (removingKey.KeyValue == CurrentKey.KeyValue && removingKey.IsCurrent == CurrentKey.IsCurrent) { CurrentKey = null; _view.ShowCurrentKey(new APIKeyEntity("-")); } } _keysRepository.RemoveAPIKey(removingKey); _view.ShowAvailableKeys <APIKeyEntity>(_keysRepository.ReadAPIKeys()); } }
public void OnAddKey(object sender, string key) { bool done; APIKeyEntity newKey; if (CurrentKey == null) { newKey = new APIKeyEntity(key, true); CurrentKey = newKey; done = _keysRepository.AddAPIKey(CurrentKey, true); _view.ShowCurrentKey(newKey); } else { newKey = new APIKeyEntity(key, false); done = _keysRepository.AddAPIKey(newKey, false); } if (done) { _view.ShowAvailableKeys <APIKeyEntity>(_keysRepository.ReadAPIKeys()); } }
private void contextMenuItem_PreviewMouseUp(object sender, MouseButtonEventArgs e) { APIKeyEntity removingKey = keysList.SelectedItem as APIKeyEntity; RemoveKey?.Invoke(sender, removingKey.KeyValue); }