api v2.2
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Application.Services.Database;
|
||||
using Application.Services.Database.MongoDB;
|
||||
using Application.Services.HashingAlgorithms;
|
||||
using Infrastructure.Data;
|
||||
using Infrastructure.Services.HashingAlgorithms;
|
||||
@@ -7,6 +8,7 @@ using Infrastructure.Services.PostgreSQL;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using MongoDB.Driver;
|
||||
|
||||
namespace Infrastructure;
|
||||
|
||||
@@ -24,8 +26,19 @@ public static class DependencyInjection
|
||||
services.AddScoped<IMedicalHistoryRepository, MedicalHistoryRepository>();
|
||||
|
||||
// MongoDB Service
|
||||
var mongoDbConnection = configuration.GetConnectionString("MongoDBDatabase");
|
||||
services.AddSingleton<IMongoDbService>(serviceProvider => new MongoDbService(mongoDbConnection));
|
||||
var mongoDbConnectionString = configuration.GetValue<string>("ConnectionStrings:MongoDBConnection");
|
||||
|
||||
// Extract MongoDB database names
|
||||
var healthcareManagerDatabaseName = configuration.GetValue<string>("HealthcareManagerDatabase:Name");
|
||||
var medicalHistoryCollectionName = configuration.GetValue<string>("HealthcareManagerDatabase:MedicalRecordCollectionName");
|
||||
var chatCollectionName = configuration.GetValue<string>("HealthcareManagerDatabase:ChatCollectionName");
|
||||
|
||||
// Register MongoDB services
|
||||
services.AddSingleton<IMedicalHistoryMongoDbService>(serviceProvider =>
|
||||
new MedicalHistoryMongoDbService(mongoDbConnectionString, healthcareManagerDatabaseName, medicalHistoryCollectionName));
|
||||
|
||||
services.AddSingleton<IChatMongoDbService>(serviceProvider =>
|
||||
new ChatMongoDbService(mongoDbConnectionString, healthcareManagerDatabaseName, chatCollectionName));
|
||||
|
||||
|
||||
// Other Services
|
||||
@@ -34,4 +47,10 @@ public static class DependencyInjection
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
private static string ExtractMongoDbDatabaseName(string connectionString)
|
||||
{
|
||||
var connectionStringBuilder = new MongoUrlBuilder(connectionString);
|
||||
return connectionStringBuilder.DatabaseName;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using Application.Services.Database.MongoDB;
|
||||
|
||||
namespace Infrastructure.Services.MongoDB;
|
||||
|
||||
public class ChatMongoDbService : MongoDbService, IChatMongoDbService
|
||||
{
|
||||
public ChatMongoDbService(string connectionString, string databaseName, string collectionName)
|
||||
: base(connectionString, databaseName, collectionName)
|
||||
{
|
||||
}
|
||||
|
||||
// Implement additional methods specific to Chat database if needed
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using Application.Services.Database.MongoDB;
|
||||
|
||||
namespace Infrastructure.Services.MongoDB;
|
||||
|
||||
public class MedicalHistoryMongoDbService : MongoDbService, IMedicalHistoryMongoDbService
|
||||
{
|
||||
public MedicalHistoryMongoDbService(string connectionString, string databaseName, string collectionName)
|
||||
: base(connectionString, databaseName, collectionName)
|
||||
{
|
||||
}
|
||||
|
||||
// Implement additional methods specific to Medical History database if needed
|
||||
}
|
||||
@@ -1,61 +1,74 @@
|
||||
using Application.Services.Database;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Bson;
|
||||
|
||||
namespace Infrastructure.Services.MongoDB;
|
||||
|
||||
public class MongoDbService : IMongoDbService
|
||||
namespace Infrastructure.Services.MongoDB
|
||||
{
|
||||
private readonly IMongoDatabase _database;
|
||||
|
||||
public MongoDbService(string connectionString)
|
||||
public class MongoDbService
|
||||
{
|
||||
var settings = MongoClientSettings.FromConnectionString(connectionString);
|
||||
settings.ServerApi = new ServerApi(ServerApiVersion.V1);
|
||||
var _database = new MongoClient(settings);
|
||||
|
||||
try {
|
||||
var result = _database.GetDatabase("admin").RunCommand<BsonDocument>(new BsonDocument("ping", 1));
|
||||
Console.WriteLine("Pinged your deployment. You successfully connected to MongoDB!");
|
||||
} catch (Exception ex) {
|
||||
Console.WriteLine(ex);
|
||||
private readonly IMongoClient _database;
|
||||
private readonly string _databaseName;
|
||||
private readonly string _collectionName;
|
||||
|
||||
public MongoDbService(string connectionString, string databaseName, string collectionName)
|
||||
{
|
||||
_databaseName = databaseName;
|
||||
_collectionName = collectionName;
|
||||
|
||||
Console.WriteLine($"Connection string: {connectionString}");
|
||||
Console.WriteLine($"Database Name: {databaseName}");
|
||||
Console.WriteLine($"Collection Name: {collectionName}");
|
||||
|
||||
var settings = MongoClientSettings.FromConnectionString(connectionString);
|
||||
settings.ServerApi = new ServerApi(ServerApiVersion.V1);
|
||||
_database = new MongoClient(settings);
|
||||
|
||||
try
|
||||
{
|
||||
var result = _database.GetDatabase("admin").RunCommand<BsonDocument>(new BsonDocument("ping", 1));
|
||||
Console.WriteLine("Pinged your deployment. You successfully connected to MongoDB!");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex);
|
||||
}
|
||||
}
|
||||
|
||||
public IMongoCollection<T> GetCollection<T>()
|
||||
{
|
||||
return _database.GetDatabase(_databaseName).GetCollection<T>(_collectionName);
|
||||
}
|
||||
|
||||
public async Task<List<T>> FindAsync<T>(List<(string FieldName, string Value)> criteria)
|
||||
{
|
||||
var collection = _database.GetDatabase(_databaseName).GetCollection<T>(_collectionName);
|
||||
|
||||
var filters = new List<FilterDefinition<T>>();
|
||||
foreach (var (FieldName, Value) in criteria) filters.Add(Builders<T>.Filter.Eq(FieldName, Value));
|
||||
|
||||
var combinedFilter = Builders<T>.Filter.And(filters);
|
||||
|
||||
return await collection.Find(combinedFilter).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task AddAsync<T>(T document)
|
||||
{
|
||||
var collection = _database.GetDatabase(_databaseName).GetCollection<T>(_collectionName);
|
||||
await collection.InsertOneAsync(document);
|
||||
}
|
||||
|
||||
public async Task ModifyAsync<T>(string keyField, string keyValue, T document)
|
||||
{
|
||||
var collection = _database.GetDatabase(_databaseName).GetCollection<T>(_collectionName);
|
||||
var filter = Builders<T>.Filter.Eq(keyField, keyValue);
|
||||
await collection.ReplaceOneAsync(filter, document, new ReplaceOptions { IsUpsert = true });
|
||||
}
|
||||
|
||||
public async Task DeleteAsync<T>(string keyField, string keyValue)
|
||||
{
|
||||
var collection = _database.GetDatabase(_databaseName).GetCollection<T>(_collectionName);
|
||||
var filter = Builders<T>.Filter.Eq(keyField, keyValue);
|
||||
await collection.DeleteOneAsync(filter);
|
||||
}
|
||||
}
|
||||
|
||||
public IMongoCollection<T> GetCollection<T>(string collectionName)
|
||||
{
|
||||
return _database.GetCollection<T>(collectionName);
|
||||
}
|
||||
|
||||
public async Task<List<T>> FindAsync<T>(string collectionName, List<(string FieldName, string Value)> criteria)
|
||||
{
|
||||
var collection = _database.GetCollection<T>(collectionName);
|
||||
|
||||
var filters = new List<FilterDefinition<T>>();
|
||||
foreach (var (FieldName, Value) in criteria) filters.Add(Builders<T>.Filter.Eq(FieldName, Value));
|
||||
|
||||
var combinedFilter = Builders<T>.Filter.And(filters);
|
||||
|
||||
return await collection.Find(combinedFilter).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task AddAsync<T>(string collectionName, T document)
|
||||
{
|
||||
var collection = _database.GetCollection<T>(collectionName);
|
||||
await collection.InsertOneAsync(document);
|
||||
}
|
||||
|
||||
public async Task ModifyAsync<T>(string collectionName, string keyField, string keyValue, T document)
|
||||
{
|
||||
var collection = _database.GetCollection<T>(collectionName);
|
||||
var filter = Builders<T>.Filter.Eq(keyField, keyValue);
|
||||
await collection.ReplaceOneAsync(filter, document, new ReplaceOptions { IsUpsert = true });
|
||||
}
|
||||
|
||||
public async Task DeleteAsync<T>(string collectionName, string keyField, string keyValue)
|
||||
{
|
||||
var collection = _database.GetCollection<T>(collectionName);
|
||||
var filter = Builders<T>.Filter.Eq(keyField, keyValue);
|
||||
await collection.DeleteOneAsync(filter);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -13,7 +13,7 @@ using System.Reflection;
|
||||
[assembly: System.Reflection.AssemblyCompanyAttribute("Infrastructure")]
|
||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+b48d5ee19e5228d9bcb979f2140f996fe7a12723")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+61a55fa7353346bdad2d677f0ec3c044c3aa87d5")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("Infrastructure")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("Infrastructure")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
||||
@@ -1 +1 @@
|
||||
1eff5085180ba5a7e0c455493bf156ab7c10e42899382b41bdf7799d2b2ca6d6
|
||||
08604fac7a3083586b55565c1ab1f39ea8637dd70dd31c4daa6a7693d2108c04
|
||||
|
||||
Binary file not shown.
+1
-1
@@ -1 +1 @@
|
||||
8ecca766b80ccc6e510e107ea1369b5001c45e66e3507ff5e47c4d13fae048e4
|
||||
39d9df72cda14e46450bf8a8087069ed85d06562c962bd2ab07df607c60c7cf4
|
||||
|
||||
Binary file not shown.
Binary file not shown.
@@ -1 +1 @@
|
||||
{"documents":{"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\*":"https://raw.githubusercontent.com/andrei-mihnea-cerbu/CC-FinalProj/b48d5ee19e5228d9bcb979f2140f996fe7a12723/*"}}
|
||||
{"documents":{"C:\\Users\\Andrei Cerbu\\Documents\\FACULTATE\\CC-FinalProj\\*":"https://raw.githubusercontent.com/andrei-mihnea-cerbu/CC-FinalProj/61a55fa7353346bdad2d677f0ec3c044c3aa87d5/*"}}
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user