Renamed service
This commit is contained in:
86
src/Logic/CollectionsService.cs
Normal file
86
src/Logic/CollectionsService.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using Models;
|
||||
using Repository;
|
||||
|
||||
namespace Logic;
|
||||
|
||||
public class CollectionsService : ICollectionsService
|
||||
{
|
||||
private readonly IBookmarksRepository repository;
|
||||
private readonly ISecretService secretService;
|
||||
|
||||
public CollectionsService(IBookmarksRepository repository, ISecretService secretService)
|
||||
{
|
||||
this.repository = repository;
|
||||
this.secretService = secretService;
|
||||
}
|
||||
|
||||
public bool Create(PasswordStore item)
|
||||
{
|
||||
if (secretService.CreateCollection(item.DisplayName ?? item.Path))
|
||||
{
|
||||
repository.Create(item);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Delete(uint ID)
|
||||
{
|
||||
secretService.DeleteCollection(Get(ID).DisplayName ?? Get(ID).Path);
|
||||
repository.Delete(ID);
|
||||
}
|
||||
|
||||
public void Delete(PasswordStore item)
|
||||
{
|
||||
Delete(item.ID);
|
||||
}
|
||||
|
||||
public void Edit(uint ID, PasswordStore newItem)
|
||||
{
|
||||
repository.Delete(ID);
|
||||
newItem.ID = ID;
|
||||
repository.Create(newItem);
|
||||
}
|
||||
|
||||
public PasswordStore Get(uint ID)
|
||||
{
|
||||
return repository.GetAll().FirstOrDefault(i => i.ID == ID) ?? throw new Exception("Item not found");
|
||||
}
|
||||
|
||||
public IEnumerable<PasswordStore> GetAll()
|
||||
{
|
||||
List<PasswordStore> jsonResults = repository.GetAll();
|
||||
|
||||
//Validate against actual secret collections
|
||||
var collections = secretService.GetCollections() ?? Enumerable.Empty<Secret.Collection>();
|
||||
|
||||
// Identify bookmarks that do NOT match any existing Secret collection.
|
||||
var toDelete = new List<uint>();
|
||||
|
||||
foreach (var ps in jsonResults)
|
||||
{
|
||||
var key = string.IsNullOrWhiteSpace(ps.DisplayName) ? ps.Path : ps.DisplayName;
|
||||
if (string.IsNullOrWhiteSpace(key))
|
||||
{
|
||||
// No reasonable key to match — schedule for deletion
|
||||
toDelete.Add(ps.ID);
|
||||
continue;
|
||||
}
|
||||
|
||||
var exists = collections.Any(c => string.Equals(c.Label, key, StringComparison.OrdinalIgnoreCase));
|
||||
if (!exists)
|
||||
{
|
||||
toDelete.Add(ps.ID);
|
||||
}
|
||||
}
|
||||
|
||||
// Perform deletions from repository for all missing bookmarks
|
||||
foreach (var id in toDelete)
|
||||
{
|
||||
repository.Delete(id);
|
||||
}
|
||||
|
||||
var remaining = jsonResults.Where(ps => !toDelete.Contains(ps.ID)).ToList();
|
||||
return remaining;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user