Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into feature/blazor_import
Browse files Browse the repository at this point in the history
  • Loading branch information
r-koubou committed Nov 5, 2023
2 parents 1ed9167 + 9cfd4c4 commit f9fc103
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 203 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static IKeySwitchRepository CreateFileRepository(string filePath)

if( path.EndsWith( ".yaml" ) || path.EndsWith( ".yml" ) )
{
return new YamlRepository( new FilePath( filePath ) );
return new YamlFileRepository( new FilePath( filePath ) );
}

throw new ArgumentException( $"{filePath} is unknown file format" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static UtcDateTime Now
}

public static DateTime NowAsDateTime
=> TimeZoneInfo.ConvertTimeToUtc( DateTime.Now );
=> DateTime.UtcNow;

public int Year { get; }
public int Month { get; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;

using KeySwitchManager.Commons.Data;
using KeySwitchManager.Domain.KeySwitches.Models;
using KeySwitchManager.Domain.KeySwitches.Models.Aggregations;
using KeySwitchManager.Domain.KeySwitches.Models.Factory;
Expand Down Expand Up @@ -30,8 +31,8 @@ public static KeySwitch CreateTemplate(
Guid.NewGuid(),
"Author",
"Description",
DateTime.Now,
DateTime.Now,
UtcDateTime.NowAsDateTime,
UtcDateTime.NowAsDateTime,
"Developer Name",
"Product name",
"Instrument name",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class KeySwitch : IEquatable<KeySwitch>
public KeySwitchId Id { get; }
public Author Author { get; }
public Description Description { get; }
public UtcDateTime Created { get; }
public UtcDateTime LastUpdated { get; }
public UtcDateTime Created { get; private set; }
public UtcDateTime LastUpdated { get; private set; }
public DeveloperName DeveloperName { get; }
public ProductName ProductName { get; }
public InstrumentName InstrumentName { get; }
Expand Down Expand Up @@ -47,6 +47,16 @@ public KeySwitch(
ExtraData = extraData;
}

public void UpdateCreatedDate( UtcDateTime newCreatedDateTime )
{
Created = newCreatedDateTime;
}

public void UpdateLastUpdatedDate( UtcDateTime newLastUpdatedDateTime )
{
LastUpdated = newLastUpdatedDateTime;
}

public override string ToString()
=> $"{DeveloperName} | {ProductName} | {InstrumentName}";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

using KeySwitchManager.Commons.Data;
using KeySwitchManager.Domain.KeySwitches.Helpers;
using KeySwitchManager.Domain.KeySwitches.Models;
using KeySwitchManager.Domain.KeySwitches.Models.Values;

namespace KeySwitchManager.Domain.KeySwitches
{
public class OnMemoryKeySwitchRepository : IKeySwitchRepository
public abstract class OnMemoryKeySwitchRepository : IKeySwitchRepository
{
protected List<KeySwitch> KeySwitches { get; }
protected List<KeySwitch> KeySwitches { get; } = new();

public OnMemoryKeySwitchRepository()
{
KeySwitches = new List<KeySwitch>();
}
protected OnMemoryKeySwitchRepository() {}

public OnMemoryKeySwitchRepository( IReadOnlyCollection<KeySwitch> source )
protected OnMemoryKeySwitchRepository( IReadOnlyCollection<KeySwitch> source )
{
KeySwitches = new List<KeySwitch>( source );
KeySwitches.AddRange( source );
}

public virtual void Dispose()
Expand All @@ -31,6 +30,11 @@ public virtual void Dispose()
public int Count()
=> KeySwitches.Count;

public void WriteBinaryTo( Stream stream )
=> WriteBinaryToAsync( stream ).GetAwaiter().GetResult();

public abstract Task WriteBinaryToAsync( Stream stream, CancellationToken cancellationToken = default );

#region Save
public async Task<IKeySwitchRepository.SaveResult> SaveAsync( KeySwitch keySwitch, CancellationToken cancellationToken = default )
{
Expand All @@ -39,6 +43,7 @@ public int Count()
if( exist != null )
{
var index = KeySwitches.IndexOf( exist );
keySwitch.UpdateLastUpdatedDate( UtcDateTime.Now );
KeySwitches[ index ] = keySwitch;
return await Task.FromResult( new IKeySwitchRepository.SaveResult( 0, 1 ) );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Threading;
using System.Threading.Tasks;

using KeySwitchManager.Commons.Data;

namespace KeySwitchManager.Infrastructures.Storage.Yaml.KeySwitches
{
public sealed class YamlFileRepository : YamlMemoryRepository
{
private FilePath YamlFilePath { get; }
public YamlFileRepository( FilePath filePath ) : base( filePath.OpenReadStream() )
{
YamlFilePath = filePath;
}

public override async void Dispose()
{
await FlushAsync( default );
}

public override async Task<int> FlushAsync( CancellationToken cancellationToken = default )
{
await using var stream = YamlFilePath.OpenWriteStream();
await WriteBinaryToAsync( stream, cancellationToken );
return Count();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using KeySwitchManager.Commons.Data;
using KeySwitchManager.Domain.KeySwitches;
using KeySwitchManager.Infrastructures.Storage.Yaml.KeySwitches.Translators;

using RkHelper.System;

namespace KeySwitchManager.Infrastructures.Storage.Yaml.KeySwitches
{
public abstract class YamlMemoryRepository : OnMemoryKeySwitchRepository
{
protected YamlMemoryRepository( Stream stream, bool leaveOpen = false ) : base()
{
StreamReader? reader = null;

try
{
reader = new StreamReader( stream, Encoding.UTF8 );
var yaml = reader.ReadToEnd();
KeySwitches.AddRange( new YamlKeySwitchImportTranslator().Translate( new PlainText( yaml ) ) );
}
finally
{
if( !leaveOpen && reader is not null )
{
Disposer.Dispose( reader );
Disposer.Dispose( stream );
}
}
}

public override async Task WriteBinaryToAsync( Stream stream, CancellationToken cancellationToken = default )
{
await using var writer = new StreamWriter( stream, Encoding.UTF8 );

var yaml = new ReadOnlyMemory<char>(
new YamlKeySwitchExportTranslator().Translate( KeySwitches ).Value.ToCharArray()
);

await writer.WriteAsync( yaml, cancellationToken );
await writer.FlushAsync();
}
}
}

This file was deleted.

0 comments on commit f9fc103

Please sign in to comment.