diff --git a/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Controllers/Import/ImportFileController.cs b/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Controllers/Import/ImportController.cs similarity index 74% rename from KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Controllers/Import/ImportFileController.cs rename to KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Controllers/Import/ImportController.cs index de30fabf..c51f20c9 100644 --- a/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Controllers/Import/ImportFileController.cs +++ b/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Controllers/Import/ImportController.cs @@ -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; } @@ -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, @@ -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 ); + } } } diff --git a/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Controllers/Import/ImportControllerFactory.cs b/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Controllers/Import/ImportControllerFactory.cs index 29d75776..62d45efc 100644 --- a/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Controllers/Import/ImportControllerFactory.cs +++ b/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Controllers/Import/ImportControllerFactory.cs @@ -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; @@ -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(); @@ -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 ); } } } diff --git a/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Controllers/Import/ImportFilePresenter.cs b/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Controllers/Import/ImportFilePresenter.cs deleted file mode 100644 index e8eaf9a9..00000000 --- a/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Controllers/Import/ImportFilePresenter.cs +++ /dev/null @@ -1,28 +0,0 @@ -using KeySwitchManager.Applications.Standalone.Core.Views.LogView; -using KeySwitchManager.UseCase.KeySwitches.Import; - -namespace KeySwitchManager.Applications.Standalone.Core.Controllers.Import -{ - public class ImportFilePresenter : IImportFilePresenter - { - private ILogTextView TextView { get; } - - public ImportFilePresenter( ILogTextView textView ) - { - TextView = textView; - } - - public void Present( T param ) - { - if( param != null ) - { - TextView.Append( param.ToString() ?? string.Empty ); - } - } - - public void Complete( ImportFileResponse response ) - { - TextView.Append( "Complete" ); - } - } -} diff --git a/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/CreatePresenter.cs b/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/CreatePresenter.cs index dffe3934..c28ebe0e 100644 --- a/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/CreatePresenter.cs +++ b/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/CreatePresenter.cs @@ -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 } } diff --git a/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/DeletePresenter.cs b/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/DeletePresenter.cs index 8f2a2c08..7b97273a 100644 --- a/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/DeletePresenter.cs +++ b/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/DeletePresenter.cs @@ -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 } diff --git a/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/DumpPresenter.cs b/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/DumpPresenter.cs index 4de89356..37c21007 100644 --- a/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/DumpPresenter.cs +++ b/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/DumpPresenter.cs @@ -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; } @@ -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 } diff --git a/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/ExportFilePresenter.cs b/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/ExportPresenter.cs similarity index 75% rename from KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/ExportFilePresenter.cs rename to KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/ExportPresenter.cs index 9e53d648..d8cbb2e8 100644 --- a/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/ExportFilePresenter.cs +++ b/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/ExportPresenter.cs @@ -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 ) @@ -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 } } diff --git a/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/FindPresenter.cs b/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/FindPresenter.cs index 221d002d..077a6433 100644 --- a/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/FindPresenter.cs +++ b/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/FindPresenter.cs @@ -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 ) @@ -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 } } diff --git a/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/ImportPresenter.cs b/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/ImportPresenter.cs new file mode 100644 index 00000000..ad90df1e --- /dev/null +++ b/KeySwitchManager/Sources/Runtime/Applications/Standalone.Core/Presenters/ImportPresenter.cs @@ -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 + } +} diff --git a/KeySwitchManager/Sources/Runtime/Interactors/KeySwitches/ImportFileInteractor.cs b/KeySwitchManager/Sources/Runtime/Interactors/KeySwitches/ImportInteractor.cs similarity index 55% rename from KeySwitchManager/Sources/Runtime/Interactors/KeySwitches/ImportFileInteractor.cs rename to KeySwitchManager/Sources/Runtime/Interactors/KeySwitches/ImportInteractor.cs index e4e72c09..22394b80 100644 --- a/KeySwitchManager/Sources/Runtime/Interactors/KeySwitches/ImportFileInteractor.cs +++ b/KeySwitchManager/Sources/Runtime/Interactors/KeySwitches/ImportInteractor.cs @@ -6,17 +6,12 @@ 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 ) { @@ -24,12 +19,13 @@ public ImportFileInteractor( Presenter = presenter; } - public async Task 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 ) { @@ -41,12 +37,18 @@ public async Task 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 ); } } } diff --git a/KeySwitchManager/Sources/Runtime/UseCases/Commons/IPresenter.cs b/KeySwitchManager/Sources/Runtime/UseCases/Commons/IPresenter.cs deleted file mode 100644 index ca83bf26..00000000 --- a/KeySwitchManager/Sources/Runtime/UseCases/Commons/IPresenter.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; - -namespace KeySwitchManager.UseCase.Commons -{ - [Obsolete( "Use IOutputPort instead" )] - public interface IPresenter - { - public void Progress( float progress ) - {} - - public void Present( T param ) - {} - - public void Complete( TResponse response ) - {} - } -} diff --git a/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/IImportFileUseCase.cs b/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/IImportFileUseCase.cs deleted file mode 100644 index fc721d92..00000000 --- a/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/IImportFileUseCase.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System.Threading; -using System.Threading.Tasks; - -namespace KeySwitchManager.UseCase.KeySwitches.Import -{ - public interface IImportFileUseCase - { - public ImportFileResponse Execute( ImportFileRequest request ) - => ExecuteAsync( request ).GetAwaiter().GetResult(); - - public Task ExecuteAsync( ImportFileRequest request, CancellationToken cancellationToken = default ); - } -} diff --git a/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/IImportUseCase.cs b/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/IImportUseCase.cs new file mode 100644 index 00000000..6caad356 --- /dev/null +++ b/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/IImportUseCase.cs @@ -0,0 +1,6 @@ +using KeySwitchManager.UseCase.Commons; + +namespace KeySwitchManager.UseCase.KeySwitches.Import +{ + public interface IImportUseCase : IInputPort {} +} diff --git a/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/ImportFilePresenter.cs b/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/ImportFilePresenter.cs index 9afde265..ad2c8a5e 100644 --- a/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/ImportFilePresenter.cs +++ b/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/ImportFilePresenter.cs @@ -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 + public interface IImportFilePresenter : IOutputPort { - public class Null : IImportFilePresenter - { - public void Complete( ImportFileResponse response ) - {} - } - - public class Console : IImportFilePresenter - { - public void Present( 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 ); } -} \ No newline at end of file +} diff --git a/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/ImportInputData.cs b/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/ImportInputData.cs new file mode 100644 index 00000000..d48930f7 --- /dev/null +++ b/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/ImportInputData.cs @@ -0,0 +1,9 @@ +using KeySwitchManager.UseCase.Commons; + +namespace KeySwitchManager.UseCase.KeySwitches.Import +{ + public sealed class ImportInputData : InputData + { + public ImportInputData( ImportInputValue value ) : base( value ) {} + } +} diff --git a/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/ImportFileRequest.cs b/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/ImportInputValue.cs similarity index 67% rename from KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/ImportFileRequest.cs rename to KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/ImportInputValue.cs index 6a7f71cb..17d527a2 100644 --- a/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/ImportFileRequest.cs +++ b/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/ImportInputValue.cs @@ -1,11 +1,11 @@ namespace KeySwitchManager.UseCase.KeySwitches.Import { - public class ImportFileRequest + public sealed class ImportInputValue { public IImportContentReader ContentReader { get; } public IContent Content { get; } - public ImportFileRequest( IImportContentReader contentReader, IContent content ) + public ImportInputValue( IImportContentReader contentReader, IContent content ) { ContentReader = contentReader; Content = content; diff --git a/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/ImportOutputData.cs b/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/ImportOutputData.cs new file mode 100644 index 00000000..ac053197 --- /dev/null +++ b/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/ImportOutputData.cs @@ -0,0 +1,11 @@ +using System; + +using KeySwitchManager.UseCase.Commons; + +namespace KeySwitchManager.UseCase.KeySwitches.Import +{ + public sealed class ImportOutputData : OutputData + { + public ImportOutputData( bool result, ImportOutputValue value, Exception? error = null ) : base( result, value, error ) {} + } +} diff --git a/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/ImportFileResponse.cs b/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/ImportOutputValue.cs similarity index 69% rename from KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/ImportFileResponse.cs rename to KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/ImportOutputValue.cs index 3e4fa5d7..1b8e3c44 100644 --- a/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/ImportFileResponse.cs +++ b/KeySwitchManager/Sources/Runtime/UseCases/KeySwitches/Import/ImportOutputValue.cs @@ -1,11 +1,11 @@ namespace KeySwitchManager.UseCase.KeySwitches.Import { - public class ImportFileResponse + public sealed class ImportOutputValue { public int InsertedCount { get; } public int UpdatedCount { get; } - public ImportFileResponse( int insertedCount, int updatedCount ) + public ImportOutputValue( int insertedCount, int updatedCount ) { InsertedCount = insertedCount; UpdatedCount = updatedCount;