Added API project.

This commit is contained in:
2025-09-02 23:44:31 -04:00
parent cb15940d34
commit 429252e61f
23 changed files with 474 additions and 49 deletions

View File

@@ -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;
}
}

View File

@@ -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);

View File

@@ -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);

View File

@@ -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);

View File

@@ -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);

View File

@@ -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);

View File

@@ -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)

View File

@@ -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)

View File

@@ -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

View File

@@ -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)

View File

@@ -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;
}
}