Delete command

This commit is contained in:
2023-01-28 14:34:23 +01:00
parent ebbc3f6fc0
commit b3755f4ceb
13 changed files with 200 additions and 56 deletions

View File

@@ -1,3 +1,5 @@
using FileTime.Core.ContentAccess;
using FileTime.Core.Extensions;
using FileTime.Core.Models;
using FileTime.Core.Timeline;
@@ -5,21 +7,76 @@ namespace FileTime.Core.Command.Delete;
public class DeleteCommand : IExecutableCommand
{
public bool HardDelete { get; init; }
public List<FullName> ItemsToDelete { get; } = new List<FullName>();
private readonly IContentAccessorFactory _contentAccessorFactory;
private readonly ITimelessContentProvider _timelessContentProvider;
public bool HardDelete { get; set; }
public List<FullName> ItemsToDelete { get; } = new();
public DeleteCommand(
IContentAccessorFactory contentAccessorFactory,
ITimelessContentProvider timelessContentProvider)
{
_contentAccessorFactory = contentAccessorFactory;
_timelessContentProvider = timelessContentProvider;
}
public Task<CanCommandRun> CanRun(PointInTime currentTime)
{
throw new NotImplementedException();
//TODO
return Task.FromResult(CanCommandRun.True);
}
public Task<PointInTime> SimulateCommand(PointInTime currentTime)
{
throw new NotImplementedException();
//TODO
return Task.FromResult(currentTime);
}
public Task Execute()
public async Task Execute()
{
throw new NotImplementedException();
//Calculate
//Delete
await TraverseTree(
PointInTime.Present,
ItemsToDelete,
new Dictionary<string, IItemDeleter>(),
new DeleteStrategy()
);
}
private async Task TraverseTree(
PointInTime currentTime,
IEnumerable<FullName> itemsToDelete,
Dictionary<string, IItemDeleter> itemDeleters,
IDeleteStrategy deleteStrategy)
{
foreach (var itemToDeleteName in itemsToDelete)
{
var itemToDelete = await _timelessContentProvider.GetItemByFullNameAsync(itemToDeleteName, currentTime);
IItemDeleter itemDeleter;
if (itemDeleters.ContainsKey(itemToDelete.Provider.Name))
{
itemDeleter = itemDeleters[itemToDelete.Provider.Name];
}
else
{
itemDeleter = _contentAccessorFactory.GetItemDeleter(itemToDelete.Provider);
itemDeleters.Add(itemToDelete.Provider.Name, itemDeleter);
}
if (itemToDelete is IContainer container)
{
await TraverseTree(
currentTime,
(await container.Items.GetItemsAsync())?.Select(i => i.Path) ?? Enumerable.Empty<FullName>(),
itemDeleters,
deleteStrategy
);
}
await itemDeleter.DeleteAsync(itemToDelete.Provider, itemToDelete.FullName!);
}
}
}

View File

@@ -0,0 +1,12 @@
using FileTime.Core.ContentAccess;
using FileTime.Core.Models;
namespace FileTime.Core.Command.Delete;
public class DeleteStrategy : IDeleteStrategy
{
public async Task DeleteItem(IItem item, IItemDeleter deleter)
{
await deleter.DeleteAsync(item.Provider, item.FullName!);
}
}

View File

@@ -0,0 +1,9 @@
using FileTime.Core.ContentAccess;
using FileTime.Core.Models;
namespace FileTime.Core.Command.Delete;
public interface IDeleteStrategy
{
Task DeleteItem(IItem item, IItemDeleter deleter);
}