Auth rpc
Search Bar Search requests
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.4" />
|
||||
<PackageReference Include="MaybeError" Version="1.1.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.4" />
|
||||
<PackageReference Include="MongoDB.Analyzer" Version="1.5.0" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="3.3.0" />
|
||||
<PackageReference Include="MongoDB.Driver.Core.Extensions.DiagnosticSources" Version="2.0.0" />
|
||||
|
||||
26
AobaCore/AobaIndexCreationService.cs
Normal file
26
AobaCore/AobaIndexCreationService.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using AobaCore.Models;
|
||||
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
using MongoDB.Driver;
|
||||
|
||||
namespace AobaCore;
|
||||
|
||||
public class AobaIndexCreationService(IMongoDatabase db): BackgroundService
|
||||
{
|
||||
private readonly IMongoCollection<Media> _media = db.GetCollection<Media>("media");
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
var textKeys = Builders<Media>.IndexKeys
|
||||
.Text(m => m.Filename);
|
||||
|
||||
var textModel = new CreateIndexModel<Media>(textKeys, new CreateIndexOptions
|
||||
{
|
||||
Name = "Text",
|
||||
Background = true
|
||||
});
|
||||
|
||||
await _media.EnsureIndexAsync(textModel);
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,20 @@ public static class Extensions
|
||||
services.AddSingleton(dbClient);
|
||||
services.AddSingleton<IMongoDatabase>(db);
|
||||
services.AddSingleton<AobaService>();
|
||||
services.AddHostedService<AobaIndexCreationService>();
|
||||
return services;
|
||||
}
|
||||
|
||||
public static async Task EnsureIndexAsync<T>(this IMongoCollection<T> collection, CreateIndexModel<T> indexModel)
|
||||
{
|
||||
try
|
||||
{
|
||||
await collection.Indexes.CreateOneAsync(indexModel);
|
||||
}
|
||||
catch (MongoCommandException e) when (e.Code == 85 || e.Code == 86) //CodeName "IndexOptionsConflict" or "NameConflict"
|
||||
{
|
||||
await collection.Indexes.DropOneAsync(indexModel.Options.Name);
|
||||
await collection.Indexes.CreateOneAsync(indexModel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user