medicalHistory: deletion when patient is deleted
This commit is contained in:
@@ -14,46 +14,38 @@ public class AppointmentsMongoDbService : MongoDbService, IAppointmentsMongoDbSe
|
||||
public async Task<bool> IsAppointmentUnique(AppointmentManagementDto dto)
|
||||
{
|
||||
var appointment = dto.Appointment.ToUniversalTime();
|
||||
|
||||
|
||||
var criteria = new List<(string FieldName, string Value)>
|
||||
{
|
||||
("DoctorId", dto.DoctorId.ToString()),
|
||||
("PatientId", dto.PatientId.ToString())
|
||||
};
|
||||
|
||||
|
||||
var appointments = await FindAsync<Appointment>(criteria);
|
||||
|
||||
foreach (var app in appointments)
|
||||
{
|
||||
if (app.AppointmentsList.Any(a => a.Date == appointment.Date))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public async Task<bool> DoesAppointmentExists(AppointmentManagementDto dto)
|
||||
{
|
||||
var appointment = dto.Appointment.ToUniversalTime();
|
||||
|
||||
|
||||
var criteria = new List<(string FieldName, string Value)>
|
||||
{
|
||||
("DoctorId", dto.DoctorId.ToString()),
|
||||
("PatientId", dto.PatientId.ToString())
|
||||
};
|
||||
|
||||
|
||||
var appointments = await FindAsync<Appointment>(criteria);
|
||||
|
||||
foreach (var app in appointments)
|
||||
{
|
||||
if (app.AppointmentsList.Any(a => a.Date == appointment.Date))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,81 +1,79 @@
|
||||
using Application.Services.Database;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using MongoDB.Bson;
|
||||
|
||||
namespace Infrastructure.Services.MongoDB
|
||||
namespace Infrastructure.Services.MongoDB;
|
||||
|
||||
public class MongoDbService
|
||||
{
|
||||
public class MongoDbService
|
||||
private readonly string _collectionName;
|
||||
private readonly IMongoClient _database;
|
||||
private readonly string _databaseName;
|
||||
|
||||
public MongoDbService(string connectionString, string databaseName, string collectionName)
|
||||
{
|
||||
private readonly IMongoClient _database;
|
||||
private readonly string _databaseName;
|
||||
private readonly string _collectionName;
|
||||
_databaseName = databaseName;
|
||||
_collectionName = collectionName;
|
||||
|
||||
public MongoDbService(string connectionString, string databaseName, string 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
|
||||
{
|
||||
_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);
|
||||
}
|
||||
var result = _database.GetDatabase("admin").RunCommand<BsonDocument>(new BsonDocument("ping", 1));
|
||||
Console.WriteLine("Pinged your deployment. You successfully connected to MongoDB!");
|
||||
}
|
||||
|
||||
public IMongoCollection<T> GetCollection<T>()
|
||||
catch (Exception ex)
|
||||
{
|
||||
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 async Task DeleteByIdAsync<T>(string id)
|
||||
{
|
||||
var collection = _database.GetDatabase(_databaseName).GetCollection<T>(_collectionName);
|
||||
var filter = Builders<T>.Filter.Eq("_id", id);
|
||||
await collection.DeleteOneAsync(filter);
|
||||
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 async Task DeleteByIdAsync<T>(string id)
|
||||
{
|
||||
var collection = _database.GetDatabase(_databaseName).GetCollection<T>(_collectionName);
|
||||
var filter = Builders<T>.Filter.Eq("_id", id);
|
||||
await collection.DeleteOneAsync(filter);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user