using adas_core.Application.Repositories.Interfaces;
using adas_core.Domain.Models.AppSettings;
using adas_core.Domain.Models.MongoModels;
using Microsoft.Extensions.Options;
using MongoDB.Bson;
using MongoDB.Driver;
namespace adas_core.Infrastructure.Repositories;
///
/// Represents a MongoDB repository for entities, implementing the contract to provide data access operations.
///
public class ServiceConfigRepository : MongoRepository, IServiceConfigRepository
{
private readonly ApiSettings _apiSettings;
public ServiceConfigRepository(IOptions apiSettings, IMongoDatabase database) : base(database)
{
if (apiSettings == null) throw new ArgumentNullException(nameof(apiSettings));
_apiSettings = apiSettings.Value;
} //For testing
///
/// Retrieves the service configuration collection name from API settings, falling back to the default "service_config" when no value is configured.
///
/// The configured service collection name, or the default "service_config" when the API setting is null.
public override string GetCollectionName()
{
return _apiSettings.ServiceConfig ?? "service_config";
}
///
/// Finds a document by matching its string identifier (StrId) with the provided value.
/// Returns null when no matching configuration exists in the collection.
///
/// The string identifier used to look up the service configuration.
/// A task that represents the asynchronous operation, containing the matching or null if not found.
public async Task FindById(string id)
{
var result = await Collection.FindAsync(Builders.Filter.Eq(x => x.StrId, id));
return await result.FirstOrDefaultAsync();
}
///
/// Asynchronously retrieves a from the collection that matches the specified identifier, returning when no matching document exists.
///
/// The of the to locate.
/// A instance if a document with the given identifier is found; otherwise, .
public async Task FindById(ObjectId oid)
{
var result = await Collection.FindAsync(Builders.Filter.Eq(x => x.Id, oid));
return await result.FirstOrDefaultAsync();
}
}