Delete command
This commit is contained in:
@@ -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!);
|
||||
}
|
||||
}
|
||||
}
|
||||
12
src/Core/FileTime.Core.Command/Delete/DeleteStrategy.cs
Normal file
12
src/Core/FileTime.Core.Command/Delete/DeleteStrategy.cs
Normal 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!);
|
||||
}
|
||||
}
|
||||
9
src/Core/FileTime.Core.Command/Delete/IDeleteStrategy.cs
Normal file
9
src/Core/FileTime.Core.Command/Delete/IDeleteStrategy.cs
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user