Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

なろうServiceのリファクタリング #24

Merged
merged 18 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Epub/KoeBook.Epub/Models/Chapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

public class Chapter
{
public List<Section> Sections = [];
public List<Section> Sections { get; init; } = [];
public string? Title { get; set; }
}
316 changes: 145 additions & 171 deletions Epub/KoeBook.Epub/Services/ScrapingNaroService.cs

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions Epub/KoeBook.Epub/TagNames.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KoeBook.Epub
{
internal static class TagNames
{
public const string Anchor = "A";
public const string Ruby = "RUBY";
public const string BreakRow = "BR";
}
}
11 changes: 9 additions & 2 deletions KoeBook.Core/EbookException.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Serialization;
using FastEnumUtility;

namespace KoeBook.Core;

Expand All @@ -13,7 +14,7 @@ public class EbookException : Exception

public EbookException(ExceptionType exceptionType, string? message = null, Exception? innerException = null) : base(null, innerException)
{
Message = message;
Message = message ?? exceptionType.GetEnumMemberValue();
ExceptionType = exceptionType;
}

Expand Down Expand Up @@ -54,5 +55,11 @@ public enum ExceptionType
UnexpectedStructure,

[EnumMember(Value = "HTTPリクエストエラー")]
HttpResponseError
HttpResponseError,

/// <summary>
/// 無効なURLです
/// </summary>
[EnumMember(Value = "無効なURLです")]
InvalidUrl,
}
10 changes: 10 additions & 0 deletions KoeBook.Core/Utilities/JsonOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Text.Json;

namespace KoeBook.Core.Utilities;

public static class JsonOptions
{
public static JsonSerializerOptions Default => JsonSerializerOptions.Default;

public static JsonSerializerOptions Web { get; } = new JsonSerializerOptions(JsonSerializerDefaults.Web);
}
9 changes: 9 additions & 0 deletions KoeBook.Core/Utilities/UriCreationOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace KoeBook.Core.Utilities;

public static class UriOptions
{
/// <summary>
/// 正規化を行わないでUriを作成します
/// </summary>
public static UriCreationOptions RawUri => new() { DangerousDisablePathAndQueryCanonicalization = true };
}
22 changes: 22 additions & 0 deletions KoeBook.Test/DiTestBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using KoeBook.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace KoeBook.Test;

public class DiTestBase
{
protected IHost Host { get; } = CreateDefaultBuilder().Build();

protected T GetService<T>() where T : notnull
{
return Host.Services.GetRequiredService<T>();
}

protected static IHostBuilder CreateDefaultBuilder()
{
return Microsoft.Extensions.Hosting.Host
.CreateDefaultBuilder()
.UseCoreStartup();
}
}
47 changes: 47 additions & 0 deletions KoeBook.Test/Epub/ScrapingNaroServiceTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using KoeBook.Core;
using KoeBook.Epub.Contracts.Services;
using KoeBook.Epub.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace KoeBook.Test.Epub;

public class ScrapingNaroServiceTest : DiTestBase
{
private readonly ScrapingNaroService _scrapingNaroService;

public ScrapingNaroServiceTest()
{
_scrapingNaroService = Host.Services
.GetServices<IScrapingService>()
.OfType<ScrapingNaroService>()
.Single();
}

[Theory]
[InlineData("https://ncode.syosetu.com/n0000a", "n0000a")]
[InlineData("https://ncode.syosetu.com/n0000a/", "n0000a")]
[InlineData("https://ncode.syosetu.com/n0000a/123", "n0000a")]
[InlineData("https://ncode.syosetu.com/n0000a/123/", "n0000a")]
[InlineData("https://ncode.syosetu.com/novelview/infotop/ncode/n0000a", "n0000a")]
[InlineData("https://ncode.syosetu.com/novelview/infotop/ncode/n0000a/", "n0000a")]
public void GetNcode_Success(string url, string? expected)
{
var result = ScrapingNaroService.GetNcode(url);

Assert.Equal(expected, result);
}

[Theory]
[InlineData("https://ncode.syosetu.com/n0000あ")]
[InlineData("https://ncode.syosetu.com/n0000あ/")]
[InlineData("https://ncode.syosetu.com/n0000aあ/123")]
[InlineData("https://ncode.syosetu.com/n0000aあ/123/")]
public void GetNcode_Error(string url)
{
var exception = Record.Exception(() => ScrapingNaroService.GetNcode(url));

var ebookException = Assert.IsType<EbookException>(exception);
Assert.Equal(ExceptionType.InvalidUrl, ebookException.ExceptionType);
}
}
4 changes: 4 additions & 0 deletions KoeBook.Test/KoeBook.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
Expand All @@ -25,4 +26,7 @@
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<Compile Include="../KoeBook/Startup.cs" />
</ItemGroup>
</Project>
32 changes: 1 addition & 31 deletions KoeBook/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,9 @@ public App()
Host = Microsoft.Extensions.Hosting.Host
.CreateDefaultBuilder()
.UseContentRoot(AppContext.BaseDirectory)
.UseCoreStartup()
.ConfigureServices((context, services) =>
{
// System
services.AddSingleton(TimeProvider.System);

// Default Activation Handler
services.AddTransient<ActivationHandler<LaunchActivatedEventArgs>, DefaultActivationHandler>();

Expand All @@ -85,34 +83,6 @@ public App()
services.AddSingleton<IDialogService, DialogService>();
services.AddSingleton<IDisplayStateChangeService, DisplayStateChangeService>();

// Core Services
services.AddHttpClient()
.ConfigureHttpClientDefaults(builder =>
{
builder.SetHandlerLifetime(TimeSpan.FromMinutes(5));
});
services.AddSingleton<IFileService, FileService>();
services.AddSingleton<ISecretSettingsService, SecretSettingsService>();
services.AddSingleton<IStyleBertVitsClientService, StyleBertVitsClientService>();
services.AddSingleton<ISoundGenerationSelectorService, SoundGenerationSelectorService>();
services.AddSingleton<ISoundGenerationService, SoundGenerationService>();
services.AddSingleton<IEpubGenerateService, EpubGenerateService>();
services.AddSingleton<IEpubDocumentStoreService, EpubDocumentStoreService>();
services.AddSingleton<IAnalyzerService, AnalyzerService>();
services.AddSingleton<ILlmAnalyzerService, ChatGptAnalyzerService>();
services.AddSingleton<OpenAI.Interfaces.IOpenAIService, MyOpenAiService>();

// Epub Services
services
.AddKeyedSingleton<IScrapingClientService, ScrapingClientService>(nameof(ScrapingAozoraService))
.AddKeyedSingleton<IScrapingClientService, ScrapingClientService>(nameof(ScrapingNaroService))
.AddSingleton<IScraperSelectorService, ScraperSelectorService>()
.AddSingleton<IScrapingService, ScrapingAozoraService>()
.AddSingleton<IScrapingService, ScrapingNaroService>();
services.AddSingleton<IEpubCreateService, EpubCreateService>();
services.AddSingleton<ISplitBraceService, SplitBraceService>();
services.AddSingleton<IFileExtensionService, FileExtensionService>();

// Views and ViewModels
services.AddTransient<SettingsViewModel>();
services.AddTransient<SettingsPage>();
Expand Down
53 changes: 53 additions & 0 deletions KoeBook/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using KoeBook.Core.Contracts.Services;
using KoeBook.Core.Services;
using KoeBook.Epub.Contracts.Services;
using KoeBook.Epub.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace KoeBook.Core;

internal static class Startup
{
/// <summary>
/// System, Core, Epub のDIを登録します
/// </summary>
public static IHostBuilder UseCoreStartup(this IHostBuilder builder)
{
builder.ConfigureServices((context, services) =>
{
// System
services.AddSingleton(TimeProvider.System);

// Core Services
services.AddHttpClient()
.ConfigureHttpClientDefaults(builder =>
{
builder.SetHandlerLifetime(TimeSpan.FromMinutes(5));
});
services.AddSingleton<IFileService, FileService>();
services.AddSingleton<ISecretSettingsService, SecretSettingsService>();
services.AddSingleton<IStyleBertVitsClientService, StyleBertVitsClientService>();
services.AddSingleton<ISoundGenerationSelectorService, SoundGenerationSelectorService>();
services.AddSingleton<ISoundGenerationService, SoundGenerationService>();
services.AddSingleton<IEpubGenerateService, EpubGenerateService>();
services.AddSingleton<IEpubDocumentStoreService, EpubDocumentStoreService>();
services.AddSingleton<IAnalyzerService, AnalyzerService>();
services.AddSingleton<ILlmAnalyzerService, ChatGptAnalyzerService>();
services.AddSingleton<OpenAI.Interfaces.IOpenAIService, MyOpenAiService>();

// Epub Services
services
.AddKeyedSingleton<IScrapingClientService, ScrapingClientService>(nameof(ScrapingAozoraService))
.AddKeyedSingleton<IScrapingClientService, ScrapingClientService>(nameof(ScrapingNaroService))
.AddSingleton<IScraperSelectorService, ScraperSelectorService>()
.AddSingleton<IScrapingService, ScrapingAozoraService>()
.AddSingleton<IScrapingService, ScrapingNaroService>();
services.AddSingleton<IEpubCreateService, EpubCreateService>();
services.AddSingleton<ISplitBraceService, SplitBraceService>();
services.AddSingleton<IFileExtensionService, FileExtensionService>();
});

return builder;
}
}
Loading