Files
AobaV2/AobaCore/Extensions.cs
Amatsugu cc64675c9c metrics service
fixes to auth info
2025-07-05 23:55:40 -04:00

42 lines
1.1 KiB
C#

global using MaybeError;
using AobaCore.Services;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.Driver;
using MongoDB.Driver.Core.Extensions.DiagnosticSources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AobaCore;
public static class Extensions
{
public static IServiceCollection AddAoba(this IServiceCollection services)
{
services.AddSingleton<AobaService>();
services.AddSingleton<ThumbnailService>();
services.AddSingleton<AccountsService>();
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);
}
}
}