diff --git a/src/Logic/PasswordStoreService.cs b/src/Logic/PasswordStoreService.cs index df7bb02..9796f82 100644 --- a/src/Logic/PasswordStoreService.cs +++ b/src/Logic/PasswordStoreService.cs @@ -9,12 +9,12 @@ public class PasswordStoreService : IPasswordStoreService public void Create(PasswordStore item) { - Create(item); + repository.Create(item); } public void Delete(uint ID) { - throw new NotImplementedException(); + repository.Delete(ID); } public void Delete(PasswordStore item) @@ -24,16 +24,18 @@ public class PasswordStoreService : IPasswordStoreService public void Edit(uint ID, PasswordStore newItem) { - throw new NotImplementedException(); + repository.Delete(ID); + newItem.ID = ID; + repository.Create(newItem); } public PasswordStore Get(uint ID) { - return repository.Get(ID); + return repository.GetAll().FirstOrDefault(i => i.ID == ID) ?? throw new Exception("Item not found"); } public IEnumerable GetAll() { - return (IEnumerable)repository.GetAll(); + return repository.GetAll(); } } diff --git a/src/Repository/IRepository.cs b/src/Repository/IRepository.cs index 5f2ddd4..a638c1b 100644 --- a/src/Repository/IRepository.cs +++ b/src/Repository/IRepository.cs @@ -6,9 +6,6 @@ namespace Repository; public interface IRepository { List GetAll(); - PasswordStore? Get(uint id); - void Edit(uint ID, PasswordStore newItem); void Create(PasswordStore item); void Delete(uint ID); - void Delete(PasswordStore item); } \ No newline at end of file diff --git a/src/Repository/JsonRepository.cs b/src/Repository/JsonRepository.cs index d28602f..3f4f069 100644 --- a/src/Repository/JsonRepository.cs +++ b/src/Repository/JsonRepository.cs @@ -69,24 +69,6 @@ public class JsonRepository : IRepository, IDisposable return _cache; } - public PasswordStore? Get(uint id) - { - try - { - return _cache.First(item => item.ID.Equals(id)); - } - catch (InvalidOperationException) - { - // Not found - return null; - } - catch (Exception e) - { - WriteToStdErr($"Unexpected error: {e.Message}"); - return null; - } - } - public void Dispose() { if (_cacheAhead) @@ -114,26 +96,6 @@ public class JsonRepository : IRepository, IDisposable } } - public void Edit(uint ID, PasswordStore newItem) - { - try - { - PasswordStore item = _cache.First(item => item.ID.Equals(ID)); - item.DisplayName = newItem.DisplayName; - item.IconName = newItem.IconName; - item.Path = newItem.Path; - _cacheAhead = true; - } - catch (InvalidOperationException) - { - WriteToStdErr($"Edit error: Item with ID {ID} not found."); - } - catch (Exception e) - { - WriteToStdErr($"Unexpected error: {e.Message}"); - } - } - public void Create(PasswordStore item) { item.ID = ++_autoIncrementedId; @@ -141,27 +103,10 @@ public class JsonRepository : IRepository, IDisposable _cacheAhead = true; } - public void Delete(PasswordStore item) - { - Delete(item.ID); - } - public void Delete(uint id) { - try - { - var item = _cache.First(item => item.ID.Equals(id)); - _cache.Remove(item); - _cacheAhead = true; - } - catch (InvalidOperationException) - { - WriteToStdErr($"Delete error: Item with ID {id} not found."); - } - catch (Exception e) - { - WriteToStdErr($"Unexpected error: {e.Message}"); - } + _cache.RemoveAll(i => i.ID == id); + _cacheAhead = true; } private void WriteToStdErr(string message)