Skip to content

Commit

Permalink
Merge pull request #70 from r-koubou/refavtor/find_usecase
Browse files Browse the repository at this point in the history
FindUseCaseの改修
  • Loading branch information
r-koubou authored Nov 30, 2023
2 parents 542d098 + 5d1ce37 commit 3fc2838
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public void Dispose()
public async Task ExecuteAsync( CancellationToken cancellationToken )
{
IFindUseCase interactor = new FindInteractor( DatabaseRepository, Presenter );
var request = new FindRequest( DeveloperName, ProductName, InstrumentName );
var response = await interactor.ExecuteAsync( request, cancellationToken );
var inputValue = new FindInputValue( DeveloperName, ProductName, InstrumentName );
var inputData = new FindInputData( inputValue );

Presenter.Complete( response );
await interactor.HandleAsync( inputData, cancellationToken );
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using KeySwitchManager.Applications.Standalone.Core.Helpers;
using KeySwitchManager.Applications.Standalone.Core.Presenters;
using KeySwitchManager.Applications.Standalone.Core.Views.LogView;

namespace KeySwitchManager.Applications.Standalone.Core.Controllers.Find
Expand Down

This file was deleted.

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

using KeySwitchManager.Applications.Standalone.Core.Views.LogView;
using KeySwitchManager.UseCase.KeySwitches.Find;

namespace KeySwitchManager.Applications.Standalone.Core.Presenters
{
public sealed class FindPresenter : IFindPresenter
{
private ILogTextView TextView { get; }

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

public async Task HandleAsync( FindOutputData outputData, CancellationToken cancellationToken = default )
{
var sb = new StringBuilder( 128 );
var keySwitches = outputData.Value.Result;

foreach( var k in keySwitches.OrderBy( x => x.DeveloperName.Value )
.ThenBy( x => x.ProductName.Value )
.ThenBy( x => x.InstrumentName.Value ) )
{
sb.Clear();
sb.Append( k );
TextView.Append( sb.ToString() );
}

TextView.Append( "---------------------------------" );
TextView.Append( $"{keySwitches.Count} record(s) found" );
TextView.Append( "Complete" );

TextView.ScrollToEnd();

await Task.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

using KeySwitchManager.Domain.KeySwitches;
using KeySwitchManager.Domain.KeySwitches.Models;
using KeySwitchManager.Domain.KeySwitches.Models.Values;
using KeySwitchManager.UseCase.KeySwitches.Find;

using RkHelper.Primitives;

namespace KeySwitchManager.Interactors.KeySwitches
{
public class FindInteractor : IFindUseCase
public sealed class FindInteractor : IFindUseCase
{
private IKeySwitchRepository Repository { get; }
private IFindPresenter Presenter { get; }

public FindInteractor(
IKeySwitchRepository repository ) :
this( repository, new IFindPresenter.Null() )
{}

public FindInteractor(
IKeySwitchRepository repository,
IFindPresenter presenter )
Expand All @@ -29,52 +22,59 @@ public FindInteractor(
Presenter = presenter;
}

public async Task<FindResponse> ExecuteAsync( FindRequest request, CancellationToken cancellationToken )
public async Task HandleAsync( FindInputData inputData, CancellationToken cancellationToken = default )
{
var developerName = request.DeveloperName;
var productName = request.ProductName;
var instrumentName = request.InstrumentName;
var developerName = inputData.Value.DeveloperName;
var productName = inputData.Value.ProductName;
var instrumentName = inputData.Value.InstrumentName;

#region By Developer, Product, Instrument
if( !StringHelper.IsEmpty( developerName, productName, instrumentName ) )
{
var keySwitches = await Repository.FindAsync(
new DeveloperName( request.DeveloperName ),
new ProductName( request.ProductName ),
new InstrumentName( request.InstrumentName ),
new DeveloperName( developerName ),
new ProductName( productName ),
new InstrumentName( instrumentName ),
cancellationToken
);

return new FindResponse( keySwitches );
var output = new FindOutputData( new FindOutputValue( keySwitches ) );

await Presenter.HandleAsync( output, cancellationToken );
return;
}
#endregion

#region By Developer, Product
if( !StringHelper.IsEmpty( developerName, productName ) )
{
var keySwitches = await Repository.FindAsync(
new DeveloperName( request.DeveloperName ),
new ProductName( request.ProductName ),
new DeveloperName( developerName ),
new ProductName( productName ),
cancellationToken
);

return new FindResponse( keySwitches );
var output = new FindOutputData( new FindOutputValue( keySwitches ) );

await Presenter.HandleAsync( output, cancellationToken );
return;
}
#endregion

#region By Developer
if( !StringHelper.IsEmpty( developerName ) )
{
var keySwitches = await Repository.FindAsync(
new DeveloperName( request.DeveloperName ),
new DeveloperName( developerName ),
cancellationToken
);

return new FindResponse( keySwitches );
var output = new FindOutputData( new FindOutputValue( keySwitches ) );

await Presenter.HandleAsync( output, cancellationToken );
return;
}
#endregion

return new FindResponse( new List<KeySwitch>() );
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using KeySwitchManager.UseCase.Commons;

namespace KeySwitchManager.UseCase.KeySwitches.Find
{
public class FindRequest
public sealed class FindInputValue
{
public string DeveloperName { get; }
public string ProductName { get; }
public string InstrumentName { get; }

public FindRequest(
public FindInputValue(
string developerName = "",
string productName = "",
string instrumentName = "" )
Expand All @@ -16,4 +18,9 @@ public FindRequest(
InstrumentName = instrumentName;
}
}
}

public sealed class FindInputData : InputData<FindInputValue>
{
public FindInputData( FindInputValue value ) : base( value ) {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using KeySwitchManager.UseCase.Commons;

namespace KeySwitchManager.UseCase.KeySwitches.Find
{
public sealed class FindOutputData : InputData<FindOutputValue>
{
public FindOutputData( FindOutputValue value ) : base( value ) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

namespace KeySwitchManager.UseCase.KeySwitches.Find
{
public class FindResponse
public sealed class FindOutputValue
{
public IReadOnlyCollection<KeySwitch> Result { get; }
public int FoundCount { get; }

public FindResponse( IReadOnlyCollection<KeySwitch> result )
public FindOutputValue( IReadOnlyCollection<KeySwitch> result )
{
Result = result;
FoundCount = Result.Count;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,5 @@

namespace KeySwitchManager.UseCase.KeySwitches.Find
{
public interface IFindPresenter : IPresenter<FindResponse>
{
public class Null : IFindPresenter
{}

public class Console : IFindPresenter
{
public void Present<T>( T param )
{
System.Console.WriteLine( param );
}

public void Message( string message )
{
System.Console.WriteLine( message );
}
}
}
}
public interface IFindPresenter : IOutputPort<FindOutputData> {}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
using System.Threading;
using System.Threading.Tasks;
using KeySwitchManager.UseCase.Commons;

namespace KeySwitchManager.UseCase.KeySwitches.Find
{
public interface IFindUseCase
{
public FindResponse Execute( FindRequest request )
=> ExecuteAsync( request ).GetAwaiter().GetResult();

public Task<FindResponse> ExecuteAsync( FindRequest request, CancellationToken cancellationToken = default );
}
public interface IFindUseCase : IInputPort<FindInputData> {}
}

0 comments on commit 3fc2838

Please sign in to comment.