Skip to content

Commit

Permalink
Merge pull request #72 from r-koubou/refactor/import_usecase
Browse files Browse the repository at this point in the history
Refactor/import usecase
  • Loading branch information
r-koubou authored Nov 30, 2023
2 parents bd04e3b + dec5786 commit bfba259
Show file tree
Hide file tree
Showing 18 changed files with 146 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace KeySwitchManager.Applications.Standalone.Core.Controllers.Import
{
public sealed class ImportFileController : IController
public sealed class ImportController : IController
{
private IKeySwitchRepository DatabaseRepository { get; }
private IImportContentReader ContentContentReader { get; }
Expand All @@ -18,7 +18,7 @@ public sealed class ImportFileController : IController
private bool LeaveOpen { get; }

#region Ctor
public ImportFileController(
public ImportController(
IKeySwitchRepository databaseRepository,
IImportContentReader contentReader,
IContent content,
Expand All @@ -45,12 +45,11 @@ public void Dispose()

public async Task ExecuteAsync( CancellationToken cancellationToken )
{
IImportFileUseCase interactor = new ImportFileInteractor( DatabaseRepository, Presenter );
var request = new ImportFileRequest( ContentContentReader, Content );
var response = await interactor.ExecuteAsync( request, cancellationToken );
await DatabaseRepository.FlushAsync( cancellationToken );
Presenter.Complete( response );
}
IImportUseCase interactor = new ImportInteractor( DatabaseRepository, Presenter );
var inputValue = new ImportInputValue( ContentContentReader, Content );
var inputData = new ImportInputData( inputValue );

await interactor.HandleAsync( inputData, cancellationToken );
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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.KeySwitches.Import;
Expand All @@ -22,7 +23,7 @@ public ImportControllerFactory( ILogTextView logTextView )
public IController Create( string databasePath, string importFilePath )
{
var databaseRepository = KeySwitchRepositoryFactory.CreateFileRepository( databasePath );
var presenter = new ImportFilePresenter( LogTextView );
var presenter = new ImportPresenter( LogTextView );

IImportContentReader contentReader;
var fileExtension = Path.GetExtension( importFilePath ).ToLower();
Expand All @@ -42,7 +43,7 @@ public IController Create( string databasePath, string importFilePath )

var content = new FileContent( new FilePath( importFilePath ) );

return new ImportFileController( databaseRepository, contentReader, content, presenter );
return new ImportController( databaseRepository, contentReader, content, presenter );
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ public CreatePresenter( ILogTextView textView )
await Task.CompletedTask;
}

#region NullObject
private class NullImpl : ICreatePresenter
{
public async Task HandleAsync( CreateOutputData outputData, CancellationToken cancellationToken = default )
{
await Task.CompletedTask;
}
=> await Task.CompletedTask;
}
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,10 @@ public DeletePresenter( ILogTextView textView )
private class NullImpl : IDeletePresenter
{
public async Task HandleDeleteBeginAsync( DeleteInputData inputData, CancellationToken cancellationToken = default )
{
await Task.CompletedTask;
}
=> await Task.CompletedTask;

public async Task HandleAsync( DeleteOutputData outputData, CancellationToken cancellationToken = default )
{
await Task.CompletedTask;
}
=> await Task.CompletedTask;
}
#endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
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();
public static readonly IDumpPresenter Null = new NullImpl();

private ILogTextView TextView { get; }

Expand All @@ -25,17 +24,10 @@ public DumpPresenter( ILogTextView textView )
}

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

public async Task HandleAsync( DeleteOutputData outputData, CancellationToken cancellationToken = default )
{
await Task.CompletedTask;
}
public async Task HandleAsync( DumpOutputData outputData, CancellationToken cancellationToken = default )
=> await Task.CompletedTask;
}
#endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace KeySwitchManager.Applications.Standalone.Core.Presenters
{
public sealed class ExportPresenter : IExportPresenter
{
public static readonly IExportPresenter Null = new NullImpl();

private ILogTextView TextView { get; }

public ExportPresenter( ILogTextView textView )
Expand Down Expand Up @@ -41,5 +43,16 @@ public ExportPresenter( ILogTextView textView )
TextView.Append( $"Exported: {exported}" );
await Task.CompletedTask;
}

#region NullObject
private class NullImpl : IExportPresenter
{
public async Task HandleAsync( ExportOutputData outputData, CancellationToken cancellationToken = default )
=> await Task.CompletedTask;

public async Task HandleExportedAsync( KeySwitch exported, CancellationToken cancellationToken = default )
=> await Task.CompletedTask;
}
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace KeySwitchManager.Applications.Standalone.Core.Presenters
{
public sealed class FindPresenter : IFindPresenter
{
public static readonly IFindPresenter Null = new NullImpl();

private ILogTextView TextView { get; }

public FindPresenter( ILogTextView textView )
Expand Down Expand Up @@ -39,5 +41,13 @@ public FindPresenter( ILogTextView textView )

await Task.CompletedTask;
}

#region NullObject
private class NullImpl : IFindPresenter
{
public async Task HandleAsync( FindOutputData outputData, CancellationToken cancellationToken = default )
=> await Task.CompletedTask;
}
#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Threading;
using System.Threading.Tasks;

using KeySwitchManager.Applications.Standalone.Core.Views.LogView;
using KeySwitchManager.Domain.KeySwitches.Models;
using KeySwitchManager.UseCase.KeySwitches.Import;

namespace KeySwitchManager.Applications.Standalone.Core.Presenters
{
public class ImportPresenter : IImportFilePresenter
{
public static readonly IImportFilePresenter Null = new NullImpl();

private ILogTextView TextView { get; }

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

public async Task HandleAsync( ImportOutputData outputData, CancellationToken cancellationToken = default )
{
var insertedCount = outputData.Value.InsertedCount;
var updatedCount = outputData.Value.UpdatedCount;

TextView.Append( $"{insertedCount} record(s) inserted, {updatedCount} record(s) updated" );
TextView.Append( "Complete" );
await Task.CompletedTask;
}

public async Task HandleImportedAsync( KeySwitch keySwitch, CancellationToken cancellationToken = default )
{
TextView.Append( $"{keySwitch}" );
await Task.CompletedTask;
}

#region NullObject
private class NullImpl : IImportFilePresenter
{
public async Task HandleAsync( ImportOutputData outputData, CancellationToken cancellationToken = default )
{
await Task.CompletedTask;
}

public async Task HandleImportedAsync( KeySwitch keySwitch, CancellationToken cancellationToken = default )
{
await Task.CompletedTask;
}
}
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,26 @@

namespace KeySwitchManager.Interactors.KeySwitches
{
public class ImportFileInteractor : IImportFileUseCase
public class ImportInteractor : IImportUseCase
{
private IKeySwitchRepository Repository { get; }
private IImportFilePresenter Presenter { get; }

public ImportFileInteractor(
IKeySwitchRepository repository ) :
this( repository, new IImportFilePresenter.Null() )
{}

public ImportFileInteractor(
public ImportInteractor(
IKeySwitchRepository repository,
IImportFilePresenter presenter )
{
Repository = repository;
Presenter = presenter;
}

public async Task<ImportFileResponse> ExecuteAsync( ImportFileRequest request, CancellationToken cancellationToken )
public async Task HandleAsync( ImportInputData inputData, CancellationToken cancellationToken = default )
{
var inputValue = inputData.Value;
var insertedCount = 0;
var updatedCount = 0;

var keySwitches = await request.ContentReader.ReadAsync( request.Content, cancellationToken );
var keySwitches = await inputValue.ContentReader.ReadAsync( inputValue.Content, cancellationToken );

foreach( var i in keySwitches )
{
Expand All @@ -41,12 +37,18 @@ public async Task<ImportFileResponse> ExecuteAsync( ImportFileRequest request, C
var r = await Repository.SaveAsync( i, cancellationToken );
updatedCount += r.Updated;
insertedCount += r.Inserted;

await Presenter.HandleImportedAsync( i, cancellationToken );
}

Presenter.Present( $"{insertedCount} record(s) inserted, {updatedCount} record(s) updated" );
if( insertedCount > 0 || updatedCount > 0 )
{
await Repository.FlushAsync( cancellationToken );
}

return new ImportFileResponse( insertedCount, updatedCount );
var outputData = new ImportOutputData( true, new ImportOutputValue( insertedCount, updatedCount ) );

await Presenter.HandleAsync( outputData, cancellationToken );
}
}
}
17 changes: 0 additions & 17 deletions KeySwitchManager/Sources/Runtime/UseCases/Commons/IPresenter.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using KeySwitchManager.UseCase.Commons;

namespace KeySwitchManager.UseCase.KeySwitches.Import
{
public interface IImportUseCase : IInputPort<ImportInputData> {}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
using System.Threading;
using System.Threading.Tasks;

using KeySwitchManager.Domain.KeySwitches.Models;
using KeySwitchManager.UseCase.Commons;

namespace KeySwitchManager.UseCase.KeySwitches.Import
{
public interface IImportFilePresenter : IPresenter<ImportFileResponse>
public interface IImportFilePresenter : IOutputPort<ImportOutputData>
{
public class Null : IImportFilePresenter
{
public void Complete( ImportFileResponse response )
{}
}

public class Console : IImportFilePresenter
{
public void Present<T>( T param )
{
System.Console.WriteLine( param );
}
void HandleImported( KeySwitch keySwitch )
=> HandleImportedAsync( keySwitch ).GetAwaiter().GetResult();

public void Message( string message )
{
System.Console.WriteLine( message );
}
}
Task HandleImportedAsync( KeySwitch keySwitch, CancellationToken cancellationToken = default );
}
}
}
Loading

0 comments on commit bfba259

Please sign in to comment.