ContentAccess, CreateContainer

This commit is contained in:
2022-05-24 09:19:57 +02:00
parent 4ac99480fa
commit 60ab7f94ea
12 changed files with 209 additions and 62 deletions

View File

@@ -0,0 +1,7 @@
namespace FileTime.Core.ContentAccess;
public interface IContentAccessorFactory
{
IItemCreator<TContentProvider> GetItemCreator<TContentProvider>() where TContentProvider : IContentProvider;
IItemCreator GetItemCreator(IContentProvider provider);
}

View File

@@ -0,0 +1,15 @@
using FileTime.Core.Models;
namespace FileTime.Core.ContentAccess;
public interface IItemCreator
{
Task CreateContainerAsync(IContentProvider contentProvider, FullName fullName);
Task CreateElementAsync(IContentProvider contentProvider, FullName fullName);
}
public interface IItemCreator<in TContentProvider> : IItemCreator where TContentProvider : IContentProvider
{
Task CreateContainerAsync(TContentProvider contentProvider, FullName fullName);
Task CreateElementAsync(TContentProvider contentProvider, FullName fullName);
}

View File

@@ -0,0 +1,17 @@
using FileTime.Core.Models;
namespace FileTime.Core.ContentAccess;
public abstract class ItemCreatorBase<TContentProvider> : IItemCreator<TContentProvider>
where TContentProvider : IContentProvider
{
public async Task CreateContainerAsync(IContentProvider contentProvider, FullName fullName)
=> await CreateContainerAsync((TContentProvider)contentProvider, fullName);
public async Task CreateElementAsync(IContentProvider contentProvider, FullName fullName)
=> await CreateElementAsync((TContentProvider)contentProvider, fullName);
public abstract Task CreateContainerAsync(TContentProvider contentProvider, FullName fullName);
public abstract Task CreateElementAsync(TContentProvider contentProvider, FullName fullName);
}

View File

@@ -1,28 +1,36 @@
using FileTime.Core.ContentAccess;
using FileTime.Core.Enums;
using FileTime.Core.Extensions;
using FileTime.Core.Models;
using FileTime.Core.Timeline;
using InitableService;
namespace FileTime.Core.Command.CreateContainer;
public class CreateContainerCommand : IExecutableCommand
public class CreateContainerCommand : IExecutableCommand, IInitable<FullName, string>
{
private readonly ITimelessContentProvider _timelessContentProvider;
public FullName Parent { get; }
public string NewContainerName { get; }
private readonly IContentAccessorFactory _contentAccessorFactory;
public FullName? Parent { get; private set; }
public string? NewContainerName { get; private set; }
public CreateContainerCommand(
FullName parent,
string newContainerName,
ITimelessContentProvider timelessContentProvider)
ITimelessContentProvider timelessContentProvider,
IContentAccessorFactory contentAccessorFactory)
{
_timelessContentProvider = timelessContentProvider;
Parent = parent;
NewContainerName = newContainerName;
_contentAccessorFactory = contentAccessorFactory;
}
public async Task<CanCommandRun> CanRun(PointInTime currentTime)
{
if (Parent is null)
throw new ArgumentNullException(nameof(Parent),
$"Property {nameof(Parent)} is not initialized. Call the {nameof(Init)} method before using the command.");
if (NewContainerName is null)
throw new ArgumentNullException(nameof(NewContainerName),
$"Property {nameof(NewContainerName)} is not initialized. Call the {nameof(Init)} method before using the command.");
try
{
var parent = await ResolveParentAsync();
@@ -49,6 +57,13 @@ public class CreateContainerCommand : IExecutableCommand
public Task<PointInTime> SimulateCommand(PointInTime currentTime)
{
if (Parent is null)
throw new ArgumentNullException(nameof(Parent),
$"Property {nameof(Parent)} is not initialized. Call the {nameof(Init)} method before using the command.");
if (NewContainerName is null)
throw new ArgumentNullException(nameof(NewContainerName),
$"Property {nameof(NewContainerName)} is not initialized. Call the {nameof(Init)} method before using the command.");
return Task.FromResult(
currentTime.WithDifferences(newPointInTime =>
new List<Difference>()
@@ -66,11 +81,35 @@ public class CreateContainerCommand : IExecutableCommand
);
}
public Task Execute()
public async Task Execute()
{
return Task.CompletedTask;
if (Parent is null)
throw new ArgumentNullException(nameof(Parent),
$"Property {nameof(Parent)} is not initialized. Call the {nameof(Init)} method before using the command.");
if (NewContainerName is null)
throw new ArgumentNullException(nameof(NewContainerName),
$"Property {nameof(NewContainerName)} is not initialized. Call the {nameof(Init)} method before using the command.");
var resolvedParent = await _timelessContentProvider.GetItemByFullNameAsync(Parent, PointInTime.Present);
var itemCreator = _contentAccessorFactory.GetItemCreator(resolvedParent.Provider);
await itemCreator.CreateContainerAsync(resolvedParent.Provider, Parent.GetChild(NewContainerName));
}
private async Task<IItem> ResolveParentAsync()
=> await _timelessContentProvider.GetItemByFullNameAsync(Parent, PointInTime.Present);
{
if (Parent is null)
throw new ArgumentNullException(nameof(Parent),
$"Property {nameof(Parent)} is not initialized. Call the {nameof(Init)} method before using the command.");
if (NewContainerName is null)
throw new ArgumentNullException(nameof(NewContainerName),
$"Property {nameof(NewContainerName)} is not initialized. Call the {nameof(Init)} method before using the command.");
return await _timelessContentProvider.GetItemByFullNameAsync(Parent, PointInTime.Present);
}
public void Init(FullName parent, string newContainerName)
{
Parent = parent;
NewContainerName = newContainerName;
}
}

View File

@@ -0,0 +1,27 @@
using Microsoft.Extensions.DependencyInjection;
namespace FileTime.Core.ContentAccess;
public class ContentAccessorFactory : IContentAccessorFactory
{
private readonly IServiceProvider _serviceProvider;
public ContentAccessorFactory(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public IItemCreator<TContentProvider> GetItemCreator<TContentProvider>() where TContentProvider : IContentProvider
{
var genericType = typeof(IItemCreator<>).MakeGenericType(typeof(TContentProvider));
return (IItemCreator<TContentProvider>)_serviceProvider.GetRequiredService(genericType);
}
public IItemCreator GetItemCreator(IContentProvider provider)
{
var genericType = typeof(IItemCreator<>).MakeGenericType(provider.GetType());
return (IItemCreator)_serviceProvider.GetRequiredService(genericType);
}
}