// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
namespace Aspire.Hosting.ApplicationModel;
///
/// A resource that represents a MongoDB database. This is a child resource of a .
///
/// The name of the resource.
/// The database name.
/// The MongoDB server resource associated with this database.
/// A resource that represents a MongoDB database. This is a child resource of a .
[DebuggerDisplay("Type = {GetType().Name,nq}, Name = {Name}, Database = {DatabaseName}")]
[AspireExport(ExposeProperties = true)]
public class MongoDBDatabaseResource(string name, string databaseName, MongoDBServerResource parent)
: Resource(name), IResourceWithParent, IResourceWithConnectionString
{
///
/// Gets the connection string expression for the MongoDB database.
///
public ReferenceExpression ConnectionStringExpression => Parent.BuildConnectionString(DatabaseName);
///
/// Gets the connection URI expression for the MongoDB database.
///
///
/// Format: mongodb://[user:password@]{host}:{port}/{database}[?authSource=admin&authMechanism=SCRAM-SHA-256]. The credential and query segments are included only when a password is configured.
///
public ReferenceExpression UriExpression => Parent.BuildConnectionString(DatabaseName);
///
/// Gets the parent MongoDB container resource.
///
public MongoDBServerResource Parent { get; } = parent ?? throw new ArgumentNullException(nameof(parent));
///
/// Gets the database name.
///
public string DatabaseName { get; } = ThrowIfNullOrEmpty(databaseName);
private static string ThrowIfNullOrEmpty([NotNull] string? argument, [CallerArgumentExpression(nameof(argument))] string? paramName = null)
{
ArgumentException.ThrowIfNullOrEmpty(argument, paramName);
return argument;
}
IEnumerable> IResourceWithConnectionString.GetConnectionProperties() =>
Parent.CombineProperties([
new("DatabaseName", ReferenceExpression.Create($"{DatabaseName}")),
new("Uri", UriExpression),
]);
}