Command execution, CreateContainer command WIP

This commit is contained in:
2022-05-23 18:12:22 +02:00
parent 6b3a8f7127
commit d4bd9d3ba1
29 changed files with 499 additions and 26 deletions

View File

@@ -0,0 +1,37 @@
using Microsoft.Extensions.Logging;
namespace FileTime.Core.Command;
public class CommandRunner : ICommandRunner
{
private readonly List<ICommandHandler> _commandHandlers;
private readonly ILogger<CommandRunner> _logger;
public CommandRunner(
IEnumerable<ICommandHandler> commandHandlers,
ILogger<CommandRunner> logger)
{
_commandHandlers = commandHandlers.ToList();
_logger = logger;
}
public async Task RunCommandAsync(ICommand command)
{
if (command is IExecutableCommand executableCommand)
{
await executableCommand.Execute();
}
else
{
var commandHandler = _commandHandlers.Find(c => c.CanHandle(command));
if (commandHandler != null)
{
await commandHandler.ExecuteAsync(command);
}
else
{
_logger.LogError("No command handler for command {Command}", command.GetType().Name);
}
}
}
}

View File

@@ -9,7 +9,7 @@ public class CopyCommand : ITransportationCommand
throw new NotImplementedException();
}
public Task<PointInTime?> SimulateCommand(PointInTime? currentTime)
public Task<PointInTime> SimulateCommand(PointInTime currentTime)
{
throw new NotImplementedException();
}

View File

@@ -1,21 +1,76 @@
using FileTime.Core.Enums;
using FileTime.Core.Extensions;
using FileTime.Core.Models;
using FileTime.Core.Timeline;
namespace FileTime.Core.Command.CreateContainer;
public class CreateContainerCommand : IExecutableCommand
{
public Task<CanCommandRun> CanRun(PointInTime currentTime)
private readonly ITimelessContentProvider _timelessContentProvider;
public FullName Parent { get; }
public string NewContainerName { get; }
public CreateContainerCommand(
FullName parent,
string newContainerName,
ITimelessContentProvider timelessContentProvider)
{
throw new NotImplementedException();
_timelessContentProvider = timelessContentProvider;
Parent = parent;
NewContainerName = newContainerName;
}
public Task<PointInTime?> SimulateCommand(PointInTime? currentTime)
public async Task<CanCommandRun> CanRun(PointInTime currentTime)
{
throw new NotImplementedException();
try
{
var parent = await ResolveParentAsync();
if (parent is not IContainer parentContainer) return CanCommandRun.False;
var items = await parentContainer.Items.GetItemsAsync();
if (items is null) return CanCommandRun.Forcable;
var existingItem = items.FirstOrDefault(i => i.Path.GetName() == NewContainerName);
return existingItem switch
{
null => CanCommandRun.True,
{ Type: AbsolutePathType.Container } => CanCommandRun.Forcable,
_ => CanCommandRun.False
};
}
catch
{
}
return CanCommandRun.False;
}
public Task Execute(ICommandScheduler commandScheduler)
public Task<PointInTime> SimulateCommand(PointInTime currentTime)
{
throw new NotImplementedException();
return Task.FromResult(
currentTime.WithDifferences(newPointInTime =>
new List<Difference>()
{
new(
DifferenceActionType.Create,
new AbsolutePath(_timelessContentProvider,
newPointInTime,
Parent.GetChild(NewContainerName),
AbsolutePathType.Container
)
)
}
)
);
}
public Task Execute()
{
return Task.CompletedTask;
}
private async Task<IItem> ResolveParentAsync()
=> await _timelessContentProvider.GetItemByFullNameAsync(Parent, PointInTime.Present);
}

View File

@@ -4,6 +4,10 @@
<ProjectReference Include="..\FileTime.Core.Abstraction\FileTime.Core.Abstraction.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />
</ItemGroup>
<PropertyGroup>
<EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
</PropertyGroup>