Simplified Repository, Logic handles more by itself
This commit is contained in:
@@ -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<PasswordStore> GetAll()
|
||||
{
|
||||
return (IEnumerable<PasswordStore>)repository.GetAll();
|
||||
return repository.GetAll();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,9 +6,6 @@ namespace Repository;
|
||||
public interface IRepository
|
||||
{
|
||||
List<PasswordStore> GetAll();
|
||||
PasswordStore? Get(uint id);
|
||||
void Edit(uint ID, PasswordStore newItem);
|
||||
void Create(PasswordStore item);
|
||||
void Delete(uint ID);
|
||||
void Delete(PasswordStore item);
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user