Skip to content

Commit

Permalink
DumpUseCaseの改修
Browse files Browse the repository at this point in the history
  • Loading branch information
r-koubou committed Nov 29, 2023
1 parent 552fa03 commit 8a1e05a
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@

using KeySwitchManager.Domain.KeySwitches;
using KeySwitchManager.Interactors.KeySwitches;
using KeySwitchManager.UseCase.Commons;
using KeySwitchManager.UseCase.KeySwitches.Dump;
using KeySwitchManager.UseCase.KeySwitches.Export;

using RkHelper.System;

namespace KeySwitchManager.Applications.Standalone.Core.Controllers.Dump
{
public class DumpFileController : IController
public class DumpController : IController
{
private IKeySwitchRepository SourceRepository { get; }
private IExportStrategy Strategy { get; }
private IDumpFilePresenter Presenter { get; }
private IDumpPresenter Presenter { get; }

public DumpFileController(
public DumpController(
IKeySwitchRepository sourceRepository,
IExportStrategy strategy,
IDumpFilePresenter presenter )
IDumpPresenter presenter )
{
SourceRepository = sourceRepository;
Strategy = strategy;
Expand All @@ -33,14 +34,13 @@ public void Dispose()

public async Task ExecuteAsync( CancellationToken cancellationToken )
{
IDumpFileUseCase interactor = new DumpFileInteractor(
IDumpUseCase interactor = new DumpInteractor(
SourceRepository,
Strategy,
Presenter
);

var response = await interactor.ExecuteAsync( new DumpFileRequest(), cancellationToken );
Presenter.Complete( response );
await interactor.HandleAsync( UnitInputData.Default, cancellationToken );
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.IO;

using KeySwitchManager.Applications.Standalone.Core.Helpers;
using KeySwitchManager.Applications.Standalone.Core.Presenters;
using KeySwitchManager.Applications.Standalone.Core.Views.LogView;
using KeySwitchManager.Commons.Data;
using KeySwitchManager.Infrastructures.Storage.Yaml.KeySwitches.Export;
using KeySwitchManager.UseCase.KeySwitches.Dump;
using KeySwitchManager.UseCase.KeySwitches.Export;

namespace KeySwitchManager.Applications.Standalone.Core.Controllers.Dump
Expand All @@ -20,9 +20,9 @@ IController IDumpControllerFactory.Create( string databasePath, string outputFil
var contentWriterFactory = new YamlExportContentFileWriterFactory( outputDirectory );
var strategy = new SingleExportStrategy( contentWriterFactory, contentFactory );

var presenter = new IDumpFilePresenter.Console();
var presenter = new DumpPresenter( logTextView );

return new DumpFileController(
return new DumpController(
database,
strategy,
presenter
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Threading;
using System.Threading.Tasks;

using KeySwitchManager.Applications.Standalone.Core.Views.LogView;
using KeySwitchManager.UseCase.KeySwitches.Delete;
using KeySwitchManager.UseCase.KeySwitches.Dump;

namespace KeySwitchManager.Applications.Standalone.Core.Presenters
{
public sealed class DumpPresenter : IDumpPresenter
{
public static readonly IDeletePresenter Null = new NullImpl();

private ILogTextView TextView { get; }

public DumpPresenter( ILogTextView textView )
{
TextView = textView;
}

public async Task HandleAsync( DumpOutputData outputData, CancellationToken cancellationToken = default )
{
TextView.Append( $"{outputData.Value.DumpDataCount} record(s) dumped" );
await Task.CompletedTask;
}

#region NullObject
private class NullImpl : IDeletePresenter
{
public async Task HandleDeleteBeginAsync( DeleteInputData inputData, CancellationToken cancellationToken = default )
{
await Task.CompletedTask;
}

public async Task HandleAsync( DeleteOutputData outputData, CancellationToken cancellationToken = default )
{
await Task.CompletedTask;
}
}
#endregion
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,46 @@
using System.Threading.Tasks;

using KeySwitchManager.Domain.KeySwitches;
using KeySwitchManager.UseCase.Commons;
using KeySwitchManager.UseCase.KeySwitches.Dump;
using KeySwitchManager.UseCase.KeySwitches.Export;

namespace KeySwitchManager.Interactors.KeySwitches
{
public class DumpFileInteractor : IDumpFileUseCase
public class DumpInteractor : IDumpUseCase
{
private IKeySwitchRepository Repository { get; }
private IExportStrategy Strategy { get; }
private IDumpFilePresenter Presenter { get; }
private IDumpPresenter Presenter { get; }

public DumpFileInteractor(
IKeySwitchRepository repository, IExportStrategy strategy ) :
this( repository, strategy, new IDumpFilePresenter.Null() ) {}

public DumpFileInteractor(
public DumpInteractor(
IKeySwitchRepository repository,
IExportStrategy strategy,
IDumpFilePresenter presenter )
IDumpPresenter presenter )
{
Repository = repository;
Strategy = strategy;
Strategy = strategy;
Presenter = presenter;
}

public async Task<DumpFileResponse> ExecuteAsync( DumpFileRequest request, CancellationToken cancellationToken )
public async Task HandleAsync( UnitInputData inputData, CancellationToken cancellationToken = default )
{
var all = await Repository.FindAllAsync( cancellationToken );

var sorted = all.OrderBy( x => x.DeveloperName.Value )
.ThenBy( x => x.ProductName.Value )
.ThenBy( x => x.InstrumentName.Value ).ToList();

if( sorted.Count > 0 )
var dumpedCount = sorted.Count;

if( dumpedCount > 0 )
{
await Strategy.ExportAsync( sorted, cancellationToken );
return new DumpFileResponse( sorted.Count );
}

Presenter.Present( $"No keyswitch(es) found." );
return new DumpFileResponse( 0 );
var output = new DumpOutputData( true, new DumpOutputValue( dumpedCount ) );

await Presenter.HandleAsync( output, cancellationToken );
}
}
}
14 changes: 14 additions & 0 deletions KeySwitchManager/Sources/Runtime/UseCases/Commons/IInputData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,22 @@ public interface IInputData<out TData>
TData Value { get; }
}

public abstract class InputData<TData> : IInputData<TData>
{
public virtual TData Value { get; }

protected InputData( TData value )
{
Value = value;
}
}

public sealed class UnitInputData : IInputData<Unit>
{
public static readonly UnitInputData Default = new();

public Unit Value => Unit.Default;

private UnitInputData() {}
}
}
18 changes: 18 additions & 0 deletions KeySwitchManager/Sources/Runtime/UseCases/Commons/IOutputData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,28 @@ public interface IOutputData<out TData>
public Exception? Error { get; }
}

public abstract class OutputData<TData> : IOutputData<TData>
{
public virtual bool Result { get; }
public virtual TData Value { get; }
public virtual Exception? Error { get; }

protected OutputData( bool result, TData value, Exception? error = null )
{
Result = result;
Value = value;
Error = error;
}
}

public sealed class UnitOutputData : IOutputData<Unit>
{
public static readonly UnitOutputData Default = new();

public bool Result => true;
public Unit Value => Unit.Default;
public Exception? Error => null;

private UnitOutputData() {}
}
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 8a1e05a

Please sign in to comment.