2025-09-22 13:08:08 +02:00
|
|
|
using Models;
|
|
|
|
|
using Repository;
|
|
|
|
|
|
|
|
|
|
namespace Logic;
|
|
|
|
|
|
2025-10-28 12:28:22 +01:00
|
|
|
public class CollectionsService : ICollectionsService
|
2025-09-22 13:08:08 +02:00
|
|
|
{
|
2025-10-28 12:05:35 +01:00
|
|
|
private readonly IBookmarksRepository repository;
|
2025-10-28 11:36:13 +01:00
|
|
|
private readonly ISecretService secretService;
|
2025-09-22 13:08:08 +02:00
|
|
|
|
2025-10-28 12:28:22 +01:00
|
|
|
public CollectionsService(IBookmarksRepository repository, ISecretService secretService)
|
2025-09-30 11:31:38 +02:00
|
|
|
{
|
|
|
|
|
this.repository = repository;
|
2025-10-28 11:36:13 +01:00
|
|
|
this.secretService = secretService;
|
2025-09-30 11:31:38 +02:00
|
|
|
}
|
|
|
|
|
|
2025-10-28 12:28:22 +01:00
|
|
|
public bool Create(PasswordStore item)
|
2025-09-22 13:08:08 +02:00
|
|
|
{
|
2025-10-28 12:28:22 +01:00
|
|
|
if (secretService.CreateCollection(item.DisplayName ?? item.Path))
|
|
|
|
|
{
|
|
|
|
|
repository.Create(item);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2025-09-22 13:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-22 15:35:31 +02:00
|
|
|
public void Delete(uint ID)
|
2025-09-22 13:08:08 +02:00
|
|
|
{
|
2025-10-28 12:05:47 +01:00
|
|
|
secretService.DeleteCollection(Get(ID).DisplayName ?? Get(ID).Path);
|
2025-09-30 10:41:12 +02:00
|
|
|
repository.Delete(ID);
|
2025-09-22 13:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-22 15:35:31 +02:00
|
|
|
public void Delete(PasswordStore item)
|
2025-09-22 13:08:08 +02:00
|
|
|
{
|
2025-09-22 15:35:31 +02:00
|
|
|
Delete(item.ID);
|
2025-09-22 13:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
2025-09-22 15:35:31 +02:00
|
|
|
public void Edit(uint ID, PasswordStore newItem)
|
2025-09-22 13:08:08 +02:00
|
|
|
{
|
2025-09-30 10:41:12 +02:00
|
|
|
repository.Delete(ID);
|
|
|
|
|
newItem.ID = ID;
|
|
|
|
|
repository.Create(newItem);
|
2025-09-22 13:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PasswordStore Get(uint ID)
|
|
|
|
|
{
|
2025-09-30 10:41:12 +02:00
|
|
|
return repository.GetAll().FirstOrDefault(i => i.ID == ID) ?? throw new Exception("Item not found");
|
2025-09-22 13:08:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerable<PasswordStore> GetAll()
|
|
|
|
|
{
|
2025-10-28 11:38:41 +01:00
|
|
|
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;
|
2025-09-22 13:08:08 +02:00
|
|
|
}
|
|
|
|
|
}
|