Added API project.
This commit is contained in:
19
JSMR.Api/JSMR.Api.csproj
Normal file
19
JSMR.Api/JSMR.Api.csproj
Normal file
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<UserSecretsId>35cebc06-af6a-44cf-aa71-ecdaf1edc82b</UserSecretsId>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.8" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\JSMR.Application\JSMR.Application.csproj" />
|
||||
<ProjectReference Include="..\JSMR.Infrastructure\JSMR.Infrastructure.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
56
JSMR.Api/JSMR.Api.http
Normal file
56
JSMR.Api/JSMR.Api.http
Normal file
@@ -0,0 +1,56 @@
|
||||
@host = http://localhost:5226
|
||||
@contentType = application/json
|
||||
|
||||
### Search tags by name
|
||||
POST {{host}}/api/tags/search
|
||||
Content-Type: {{contentType}}
|
||||
|
||||
{
|
||||
"options": {
|
||||
"criteria": {
|
||||
"name": "Heart"
|
||||
},
|
||||
"pageNumber": 1,
|
||||
"pageSize": 10
|
||||
}
|
||||
}
|
||||
|
||||
###
|
||||
|
||||
### Search voice works with circle filter
|
||||
POST {{host}}/api/voiceworks/search
|
||||
Content-Type: {{contentType}}
|
||||
|
||||
{
|
||||
"options": {
|
||||
"criteria": {
|
||||
"circleStatus": "Favorited",
|
||||
"releaseDateStart": "2023-01-01",
|
||||
"releaseDateEnd": "2024-12-31",
|
||||
"locale": "English",
|
||||
"supportedLanguages": ["English"]
|
||||
},
|
||||
"pageNumber": 1,
|
||||
"pageSize": 20,
|
||||
"sortOptions": [
|
||||
{ "field": "Downloads", "direction": "Descending" }
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
### Search voice works with keywords
|
||||
POST {{host}}/api/voiceworks/search
|
||||
Content-Type: {{contentType}}
|
||||
|
||||
{
|
||||
"options": {
|
||||
"criteria": {
|
||||
"keywords": "tsundere maid"
|
||||
},
|
||||
"pageNumber": 1,
|
||||
"pageSize": 20,
|
||||
"sortOptions": [
|
||||
{ "field": "Downloads", "direction": "Descending" }
|
||||
]
|
||||
}
|
||||
}
|
||||
103
JSMR.Api/Program.cs
Normal file
103
JSMR.Api/Program.cs
Normal file
@@ -0,0 +1,103 @@
|
||||
using JSMR.Application.Circles.Queries.Search;
|
||||
using JSMR.Application.Creators.Queries.Search;
|
||||
using JSMR.Application.DI;
|
||||
using JSMR.Application.Tags.Queries.Search;
|
||||
using JSMR.Application.VoiceWorks.Queries.Search;
|
||||
using JSMR.Infrastructure.Data;
|
||||
using JSMR.Infrastructure.DI;
|
||||
using Microsoft.AspNetCore.Http.Json;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services
|
||||
.AddMemoryCache() // TODO
|
||||
.AddApplication()
|
||||
.AddInfrastructure();
|
||||
|
||||
// DbContext (MySQL here; swap to Npgsql when you migrate)
|
||||
var cs = builder.Configuration.GetConnectionString("AppDb")
|
||||
?? throw new InvalidOperationException("Missing ConnectionStrings:AppDb");
|
||||
|
||||
builder.Services.AddDbContext<AppDbContext>(opt =>
|
||||
opt.UseMySql(cs, ServerVersion.AutoDetect(cs))
|
||||
.EnableSensitiveDataLogging(false));
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
builder.Services.Configure<JsonOptions>(options =>
|
||||
{
|
||||
options.SerializerOptions.PropertyNameCaseInsensitive = true;
|
||||
options.SerializerOptions.Converters.Add(
|
||||
new JsonStringEnumConverter(JsonNamingPolicy.CamelCase)); // or null for exact names
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.MapOpenApi();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
// Health check
|
||||
app.MapGet("/health", () => Results.Ok(new { status = "ok" }));
|
||||
|
||||
// ---- Endpoints ----
|
||||
|
||||
// Circles Search
|
||||
app.MapPost("/api/circles/search", async (
|
||||
SearchCirclesRequest request,
|
||||
SearchCirclesHandler handler,
|
||||
CancellationToken cancallationToken) =>
|
||||
{
|
||||
var result = await handler.HandleAsync(request, cancallationToken);
|
||||
|
||||
return Results.Ok(result);
|
||||
});
|
||||
|
||||
// Voice Works Search
|
||||
app.MapPost("/api/voiceworks/search", async (
|
||||
SearchVoiceWorksRequest request,
|
||||
SearchVoiceWorksHandler handler,
|
||||
CancellationToken cancallationToken) =>
|
||||
{
|
||||
var result = await handler.HandleAsync(request, cancallationToken);
|
||||
|
||||
return Results.Ok(result);
|
||||
});
|
||||
|
||||
// Tags Search
|
||||
app.MapPost("/api/tags/search", async (
|
||||
SearchTagsRequest request,
|
||||
SearchTagsHandler handler,
|
||||
CancellationToken cancallationToken) =>
|
||||
{
|
||||
var result = await handler.HandleAsync(request, cancallationToken);
|
||||
|
||||
return Results.Ok(result);
|
||||
});
|
||||
|
||||
// Creators Search
|
||||
app.MapPost("/api/creators/search", async (
|
||||
SearchCreatorsRequest request,
|
||||
SearchCreatorsHandler handler,
|
||||
CancellationToken cancallationToken) =>
|
||||
{
|
||||
var result = await handler.HandleAsync(request, cancallationToken);
|
||||
|
||||
return Results.Ok(result);
|
||||
});
|
||||
|
||||
app.Run();
|
||||
23
JSMR.Api/Properties/launchSettings.json
Normal file
23
JSMR.Api/Properties/launchSettings.json
Normal file
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "http://localhost:5226",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "https://localhost:7277;http://localhost:5226",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
8
JSMR.Api/appsettings.Development.json
Normal file
8
JSMR.Api/appsettings.Development.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
9
JSMR.Api/appsettings.json
Normal file
9
JSMR.Api/appsettings.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
public enum Language
|
||||
{
|
||||
Unknown,
|
||||
Japanese,
|
||||
English,
|
||||
ChineseSimplified,
|
||||
ChineseTraditional,
|
||||
Korean
|
||||
Unknown = -1,
|
||||
Japanese = 0,
|
||||
English = 1,
|
||||
ChineseSimplified = 2,
|
||||
ChineseTraditional = 3,
|
||||
Korean = 4
|
||||
}
|
||||
@@ -1,5 +1,9 @@
|
||||
using JSMR.Application.Tags.Commands.SetEnglishName;
|
||||
using JSMR.Application.Circles.Queries.Search;
|
||||
using JSMR.Application.Creators.Queries.Search;
|
||||
using JSMR.Application.Tags.Commands.SetEnglishName;
|
||||
using JSMR.Application.Tags.Commands.UpdateTagStatus;
|
||||
using JSMR.Application.Tags.Queries.Search;
|
||||
using JSMR.Application.VoiceWorks.Queries.Search;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace JSMR.Application.DI;
|
||||
@@ -9,12 +13,17 @@ public static class ApplicationServiceCollectionExtensions
|
||||
public static IServiceCollection AddApplication(this IServiceCollection services)
|
||||
{
|
||||
// Handlers / Use-cases
|
||||
//services.AddScoped<SearchVoiceWorksHandler>();
|
||||
services.AddScoped<SearchCirclesHandler>();
|
||||
|
||||
services.AddScoped<SearchVoiceWorksHandler>();
|
||||
//services.AddScoped<ScanVoiceWorksHandler>();
|
||||
|
||||
services.AddScoped<SearchTagsHandler>();
|
||||
services.AddScoped<SetTagEnglishNameHandler>();
|
||||
services.AddScoped<UpdateTagStatusHandler>();
|
||||
|
||||
services.AddScoped<SearchCreatorsHandler>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -5,27 +5,27 @@ using JSMR.Application.Tags.Queries.Search.Ports;
|
||||
|
||||
namespace JSMR.Application.Tags.Queries.Search;
|
||||
|
||||
public sealed class SearchTagsHandler(ITagSearchProvider searchProvider, ICache cache)
|
||||
public sealed class SearchTagsHandler(ITagSearchProvider searchProvider)
|
||||
{
|
||||
public async Task<SearchTagsResponse> HandleAsync(SearchTagsRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
SearchOptions<TagSearchCriteria, TagSortField> searchOptions = request.Options;
|
||||
|
||||
string cacheKey = $"tag:{searchOptions.GetHashCode()}";
|
||||
//string cacheKey = $"tag:{searchOptions.GetHashCode()}";
|
||||
|
||||
TagSearchResults? cachedResults = await cache.GetAsync<TagSearchResults>(cacheKey, cancellationToken);
|
||||
//TagSearchResults? cachedResults = await cache.GetAsync<TagSearchResults>(cacheKey, cancellationToken);
|
||||
|
||||
if (cachedResults != null)
|
||||
return new SearchTagsResponse(cachedResults);
|
||||
//if (cachedResults != null)
|
||||
// return new SearchTagsResponse(cachedResults);
|
||||
|
||||
SearchResult<TagSearchItem> results = await searchProvider.SearchAsync(searchOptions, cancellationToken);
|
||||
|
||||
CacheEntryOptions cacheEntryOptions = new()
|
||||
{
|
||||
SlidingExpiration = TimeSpan.FromMinutes(10)
|
||||
};
|
||||
//CacheEntryOptions cacheEntryOptions = new()
|
||||
//{
|
||||
// SlidingExpiration = TimeSpan.FromMinutes(10)
|
||||
//};
|
||||
|
||||
await cache.SetAsync(cacheKey, results, cacheEntryOptions, cancellationToken);
|
||||
//await cache.SetAsync(cacheKey, results, cacheEntryOptions, cancellationToken);
|
||||
|
||||
return new SearchTagsResponse(results);
|
||||
}
|
||||
|
||||
@@ -3,28 +3,29 @@ using JSMR.Application.VoiceWorks.Ports;
|
||||
|
||||
namespace JSMR.Application.VoiceWorks.Queries.Search;
|
||||
|
||||
//public sealed class SearchVoiceWorksHandler(IVoiceWorkReader reader, ICache cache)
|
||||
//{
|
||||
// //public async Task<SearchVoiceWorksResponse> HandleAsync(SearchVoiceWorksRequest request, CancellationToken cancellationToken)
|
||||
// //{
|
||||
// // VoiceWorkSearchOptions searchOptions = request.Options;
|
||||
// TODO: Caching?
|
||||
public sealed class SearchVoiceWorksHandler(IVoiceWorkSearchProvider provider)
|
||||
{
|
||||
public async Task<SearchVoiceWorksResponse> HandleAsync(SearchVoiceWorksRequest request, CancellationToken cancellationToken)
|
||||
{
|
||||
var searchOptions = request.Options;
|
||||
|
||||
// // string cacheKey = $"vw:{searchOptions.GetHashCode()}";
|
||||
//string cacheKey = $"vw:{searchOptions.GetHashCode()}";
|
||||
|
||||
// // VoiceWorkSearchResults? cachedResults = await cache.GetAsync<VoiceWorkSearchResults>(cacheKey, cancellationToken);
|
||||
//VoiceWorkSearchResults? cachedResults = await cache.GetAsync<VoiceWorkSearchResults>(cacheKey, cancellationToken);
|
||||
|
||||
// // if (cachedResults != null)
|
||||
// // return new SearchVoiceWorksResponse(cachedResults);
|
||||
//if (cachedResults != null)
|
||||
// return new SearchVoiceWorksResponse(cachedResults);
|
||||
|
||||
// // VoiceWorkSearchResults results = await reader.SearchAsync(searchOptions, cancellationToken);
|
||||
var results = await provider.SearchAsync(searchOptions, cancellationToken);
|
||||
|
||||
// // CacheEntryOptions cacheEntryOptions = new()
|
||||
// // {
|
||||
// // SlidingExpiration = TimeSpan.FromMinutes(10)
|
||||
// // };
|
||||
//CacheEntryOptions cacheEntryOptions = new()
|
||||
//{
|
||||
// SlidingExpiration = TimeSpan.FromMinutes(10)
|
||||
//};
|
||||
|
||||
// // await cache.SetAsync(cacheKey, results, cacheEntryOptions, cancellationToken);
|
||||
//await cache.SetAsync(cacheKey, results, cacheEntryOptions, cancellationToken);
|
||||
|
||||
// // return new SearchVoiceWorksResponse(results);
|
||||
// //}
|
||||
//}
|
||||
return new SearchVoiceWorksResponse(results);
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
namespace JSMR.Application.VoiceWorks.Queries.Search;
|
||||
using JSMR.Application.Common.Search;
|
||||
|
||||
public sealed record SearchVoiceWorksResponse(VoiceWorkSearchResults Results);
|
||||
namespace JSMR.Application.VoiceWorks.Queries.Search;
|
||||
|
||||
public sealed record SearchVoiceWorksResponse(SearchResult<VoiceWorkSearchResult> Results);
|
||||
@@ -1,7 +1,17 @@
|
||||
using JSMR.Application.Circles.Queries.GetCreators;
|
||||
using JSMR.Application.Circles.Queries.GetTags;
|
||||
using JSMR.Application.Circles.Queries.Search;
|
||||
using JSMR.Application.Common.Caching;
|
||||
using JSMR.Application.Creators.Ports;
|
||||
using JSMR.Application.Creators.Queries.Search.Ports;
|
||||
using JSMR.Application.Tags.Ports;
|
||||
using JSMR.Application.Tags.Queries.Search.Ports;
|
||||
using JSMR.Application.VoiceWorks.Queries.Search;
|
||||
using JSMR.Infrastructure.Caching;
|
||||
using JSMR.Infrastructure.Data.Repositories.Circles;
|
||||
using JSMR.Infrastructure.Data.Repositories.Creators;
|
||||
using JSMR.Infrastructure.Data.Repositories.Tags;
|
||||
using JSMR.Infrastructure.Data.Repositories.VoiceWorks;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace JSMR.Infrastructure.DI;
|
||||
@@ -14,6 +24,17 @@ public static class InfrastructureServiceCollectionExtensions
|
||||
services.AddScoped<ICircleTagsProvider, CircleTagsProvider>();
|
||||
services.AddScoped<ICircleCreatorsProvider, CircleCreatorsProvider>();
|
||||
|
||||
services.AddScoped<IVoiceWorkSearchProvider, VoiceWorkSearchProvider>();
|
||||
services.AddScoped<IVoiceWorkFullTextSearch, MySqlVoiceWorkFullTextSearch>();
|
||||
|
||||
services.AddScoped<ITagSearchProvider, TagSearchProvider>();
|
||||
services.AddScoped<ITagWriter, TagWriter>();
|
||||
|
||||
services.AddScoped<ICreatorSearchProvider, CreatorSearchProvider>();
|
||||
services.AddScoped<ICreatorWriter, CreatorWriter>();
|
||||
|
||||
services.AddSingleton<ICache, MemoryCacheAdapter>();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ public sealed class CircleConfiguration : IEntityTypeConfiguration<Circle>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Circle> builder)
|
||||
{
|
||||
builder.ToTable("circles");
|
||||
builder.ToTable("Circles");
|
||||
builder.HasKey(x => x.CircleId);
|
||||
|
||||
builder.Property(x => x.Name).IsRequired().HasMaxLength(256);
|
||||
|
||||
@@ -8,7 +8,7 @@ public sealed class CreatorConfiguration : IEntityTypeConfiguration<Creator>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Creator> builder)
|
||||
{
|
||||
builder.ToTable("creators");
|
||||
builder.ToTable("Creators");
|
||||
builder.HasKey(x => x.CreatorId);
|
||||
|
||||
builder.Property(x => x.Name).IsRequired().HasMaxLength(128);
|
||||
|
||||
@@ -8,7 +8,7 @@ public sealed class EnglishTagConfiguration : IEntityTypeConfiguration<EnglishTa
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<EnglishTag> builder)
|
||||
{
|
||||
builder.ToTable("english_tags");
|
||||
builder.ToTable("EnglishTags");
|
||||
builder.HasKey(x => x.EnglishTagId);
|
||||
|
||||
builder.Property(x => x.Name).IsRequired().HasMaxLength(128);
|
||||
|
||||
@@ -8,7 +8,7 @@ public sealed class EnglishVoiceWorkConfiguration : IEntityTypeConfiguration<Eng
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<EnglishVoiceWork> builder)
|
||||
{
|
||||
builder.ToTable("english_voice_works");
|
||||
builder.ToTable("EnglishVoiceWorks");
|
||||
builder.HasKey(x => x.EnglishVoiceWorkId);
|
||||
|
||||
builder.Property(x => x.ProductName).HasMaxLength(256);
|
||||
|
||||
@@ -8,7 +8,7 @@ public sealed class TagConfiguration : IEntityTypeConfiguration<Tag>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Tag> builder)
|
||||
{
|
||||
builder.ToTable("tags");
|
||||
builder.ToTable("Tags");
|
||||
builder.HasKey(x => x.TagId);
|
||||
|
||||
builder.Property(x => x.Name).IsRequired().HasMaxLength(128);
|
||||
|
||||
@@ -8,7 +8,7 @@ public sealed class VoiceWorkConfiguration : IEntityTypeConfiguration<VoiceWork>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<VoiceWork> builder)
|
||||
{
|
||||
builder.ToTable("voice_works");
|
||||
builder.ToTable("VoiceWorks");
|
||||
builder.HasKey(x => x.VoiceWorkId);
|
||||
|
||||
builder.Property(x => x.ProductId)
|
||||
|
||||
@@ -8,7 +8,7 @@ public sealed class VoiceWorkCreatorConfiguration : IEntityTypeConfiguration<Voi
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<VoiceWorkCreator> builder)
|
||||
{
|
||||
builder.ToTable("voice_work_creators");
|
||||
builder.ToTable("VoiceWorkCreators");
|
||||
builder.HasKey(x => new { x.VoiceWorkId, x.CreatorId });
|
||||
|
||||
builder.HasOne(x => x.VoiceWork)
|
||||
|
||||
@@ -8,7 +8,7 @@ public sealed class VoiceWorkSearchConfiguration : IEntityTypeConfiguration<Voic
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<VoiceWorkSearch> builder)
|
||||
{
|
||||
builder.ToTable("voice_work_searches");
|
||||
builder.ToTable("VoiceWorkSearches");
|
||||
builder.HasKey(x => x.VoiceWorkId); // also the FK
|
||||
|
||||
builder.Property(x => x.SearchText).IsRequired(); // TEXT/LONGTEXT
|
||||
|
||||
@@ -8,7 +8,7 @@ public sealed class VoiceWorkTagConfiguration : IEntityTypeConfiguration<VoiceWo
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<VoiceWorkTag> builder)
|
||||
{
|
||||
builder.ToTable("voice_work_tags");
|
||||
builder.ToTable("VoiceWorkTags");
|
||||
builder.HasKey(x => new { x.VoiceWorkId, x.TagId });
|
||||
|
||||
builder.HasOne(x => x.VoiceWork)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Text;
|
||||
|
||||
namespace JSMR.Infrastructure.Data.Repositories.VoiceWorks;
|
||||
|
||||
@@ -6,6 +7,173 @@ public class MySqlVoiceWorkFullTextSearch : IVoiceWorkFullTextSearch
|
||||
{
|
||||
public IQueryable<int> MatchingIds(AppDbContext context, string searchText) =>
|
||||
context.VoiceWorkSearches
|
||||
.Where(v => EF.Functions.Match(v.SearchText, searchText, MySqlMatchSearchMode.Boolean) > 0)
|
||||
.Where(v => EF.Functions.Match(v.SearchText, MySqlBooleanQuery.Normalize(searchText), MySqlMatchSearchMode.Boolean) > 0)
|
||||
.Select(v => v.VoiceWorkId);
|
||||
}
|
||||
|
||||
public static class MySqlBooleanQuery
|
||||
{
|
||||
// Main entry
|
||||
public static string Normalize(string input)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(input)) return string.Empty;
|
||||
|
||||
// Split into top-level tokens by spaces (not inside quotes/parentheses)
|
||||
var tokens = SplitTopLevel(input.Trim(), ' ');
|
||||
var parts = new List<string>(tokens.Count);
|
||||
|
||||
foreach (var raw in tokens)
|
||||
{
|
||||
var t = raw.Trim();
|
||||
if (t.Length == 0) continue;
|
||||
|
||||
// Preserve explicit boolean operators user may already supply
|
||||
if (t[0] == '-' || t[0] == '+')
|
||||
{
|
||||
// Token already has a sign; normalize rest
|
||||
var sign = t[0];
|
||||
var body = t.Substring(1).Trim();
|
||||
parts.Add(sign + NormalizePositive(body));
|
||||
continue;
|
||||
}
|
||||
|
||||
// Default: required term
|
||||
parts.Add("+" + NormalizePositive(t));
|
||||
}
|
||||
|
||||
return string.Join(' ', parts.Where(p => p.Length > 1 || p == "+" || p == "-")).Trim();
|
||||
}
|
||||
|
||||
// Normalize a positive (non-signed) token: handles ORs, phrases, grouping
|
||||
private static string NormalizePositive(string token)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(token)) return string.Empty;
|
||||
|
||||
// If token is a quoted phrase already -> keep quotes
|
||||
if (IsQuoted(token))
|
||||
{
|
||||
return EnsureBalancedQuotes(token);
|
||||
}
|
||||
|
||||
// If token starts/ends with parentheses, leave as-is (user grouping)
|
||||
if (token.StartsWith("(") && token.EndsWith(")") && token.Length > 2)
|
||||
{
|
||||
// Optionally normalize inside the group if you want;
|
||||
// here we trust user's grouping.
|
||||
return token;
|
||||
}
|
||||
|
||||
// If token contains OR '|' at top level, convert to (a|b|...)
|
||||
if (ContainsTopLevel(token, '|'))
|
||||
{
|
||||
var orParts = SplitTopLevel(token, '|')
|
||||
.Select(p => NormalizeOrAtom(p.Trim()))
|
||||
.Where(p => p.Length > 0);
|
||||
return "(" + string.Join("|", orParts) + ")";
|
||||
}
|
||||
|
||||
// Plain atom -> as-is
|
||||
return token;
|
||||
}
|
||||
|
||||
// Normalize one OR-side atom (could be phrase or bare word)
|
||||
private static string NormalizeOrAtom(string atom)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(atom)) return string.Empty;
|
||||
|
||||
// Allow nested quotes inside OR
|
||||
if (IsQuoted(atom)) return EnsureBalancedQuotes(atom);
|
||||
|
||||
// If it contains whitespace, quote it to become a phrase
|
||||
if (atom.Any(char.IsWhiteSpace))
|
||||
return $"\"{EscapeQuotes(atom)}\"";
|
||||
|
||||
return atom;
|
||||
}
|
||||
|
||||
// -------------- helpers --------------
|
||||
|
||||
private static bool IsQuoted(string s)
|
||||
=> s.Length >= 2 && s[0] == '"' && s[^1] == '"';
|
||||
|
||||
private static string EnsureBalancedQuotes(string s)
|
||||
{
|
||||
// If user typed starting quote but forgot closing, close it.
|
||||
if (s.Length >= 1 && s[0] == '"' && (s.Length == 1 || s[^1] != '"'))
|
||||
return s + "\"";
|
||||
|
||||
// If user typed closing quote but no opening, open it.
|
||||
if (s.Length >= 1 && s[^1] == '"' && (s.Length == 1 || s[0] != '"'))
|
||||
return "\"" + s;
|
||||
|
||||
// Also escape any embedded quotes (rare)
|
||||
if (IsQuoted(s))
|
||||
{
|
||||
var inner = s.Substring(1, s.Length - 2);
|
||||
return $"\"{EscapeQuotes(inner)}\"";
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
private static string EscapeQuotes(string s) => s.Replace("\"", "\\\"");
|
||||
|
||||
private static bool ContainsTopLevel(string s, char sep)
|
||||
{
|
||||
int depth = 0;
|
||||
bool inQuotes = false;
|
||||
|
||||
foreach (var ch in s)
|
||||
{
|
||||
if (ch == '"' && depth == 0) inQuotes = !inQuotes;
|
||||
else if (!inQuotes)
|
||||
{
|
||||
if (ch == '(') depth++;
|
||||
else if (ch == ')' && depth > 0) depth--;
|
||||
else if (ch == sep && depth == 0) return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static List<string> SplitTopLevel(string s, char sep)
|
||||
{
|
||||
var list = new List<string>();
|
||||
var sb = new StringBuilder();
|
||||
int depth = 0;
|
||||
bool inQuotes = false;
|
||||
|
||||
for (int i = 0; i < s.Length; i++)
|
||||
{
|
||||
var ch = s[i];
|
||||
|
||||
if (ch == '"' && depth == 0)
|
||||
{
|
||||
inQuotes = !inQuotes;
|
||||
sb.Append(ch);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!inQuotes)
|
||||
{
|
||||
if (ch == '(') { depth++; sb.Append(ch); continue; }
|
||||
if (ch == ')') { depth = Math.Max(0, depth - 1); sb.Append(ch); continue; }
|
||||
|
||||
if (ch == sep && depth == 0)
|
||||
{
|
||||
// split
|
||||
var piece = sb.ToString().Trim();
|
||||
if (piece.Length > 0) list.Add(piece);
|
||||
sb.Clear();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
sb.Append(ch);
|
||||
}
|
||||
|
||||
var tail = sb.ToString().Trim();
|
||||
if (tail.Length > 0) list.Add(tail);
|
||||
return list;
|
||||
}
|
||||
}
|
||||
8
JSMR.sln
8
JSMR.sln
@@ -1,7 +1,7 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.14.36408.4 d17.14
|
||||
VisualStudioVersion = 17.14.36408.4
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JSMR.Domain", "JSMR.Domain\JSMR.Domain.csproj", "{BC16F228-63B0-4EE6-9B96-19A38A31C125}"
|
||||
EndProject
|
||||
@@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JSMR.Infrastructure", "JSMR
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JSMR.Tests", "JSMR.Tests\JSMR.Tests.csproj", "{9C17C2F2-B43E-48F9-960E-E8AEA9F7763E}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JSMR.Api", "JSMR.Api\JSMR.Api.csproj", "{C60ECDB4-0EBF-4682-AC07-199A1051D9AE}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -33,6 +35,10 @@ Global
|
||||
{9C17C2F2-B43E-48F9-960E-E8AEA9F7763E}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{9C17C2F2-B43E-48F9-960E-E8AEA9F7763E}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{9C17C2F2-B43E-48F9-960E-E8AEA9F7763E}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{C60ECDB4-0EBF-4682-AC07-199A1051D9AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{C60ECDB4-0EBF-4682-AC07-199A1051D9AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{C60ECDB4-0EBF-4682-AC07-199A1051D9AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{C60ECDB4-0EBF-4682-AC07-199A1051D9AE}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
Reference in New Issue
Block a user