finalizare 1.0
This commit is contained in:
@@ -1,46 +1,36 @@
|
||||
using Application.Endpoints.Appointments;
|
||||
using Application.Services.Database.MongoDB;
|
||||
using Core.Entities;
|
||||
using Application.Services.Database.MongoDB;
|
||||
using Domain.Entities;
|
||||
using MongoDB.Driver;
|
||||
using Application.Endpoints.Appointments;
|
||||
|
||||
namespace Infrastructure.Services.MongoDB;
|
||||
|
||||
public class AppointmentsMongoDbService : MongoDbService, IAppointmentsMongoDbService
|
||||
public class AppointmentsMongoDbService(string connectionString, string databaseName, string collectionName)
|
||||
: MongoDbService(connectionString, databaseName, collectionName), IAppointmentsMongoDbService
|
||||
{
|
||||
public AppointmentsMongoDbService(string connectionString, string databaseName, string collectionName)
|
||||
: base(connectionString, databaseName, collectionName)
|
||||
public async Task<bool> IsAppointmentUnique(AppointmentInformation request, CancellationToken token)
|
||||
{
|
||||
}
|
||||
var criteria = new List<(string FieldName, string Value)>
|
||||
{
|
||||
("DoctorId", request.DoctorId.ToString()),
|
||||
("PatientId", request.PatientId.ToString())
|
||||
};
|
||||
|
||||
public async Task<bool> IsAppointmentUnique(AppointmentManagementDto dto)
|
||||
var appointments = await FindAsync<Appointment>(criteria, token);
|
||||
return appointments.All(a => !a.AppointmentsList.Any(dt => dt == request.Appointment));
|
||||
}
|
||||
|
||||
public async Task<bool> DoesAppointmentExists(AppointmentInformation request, CancellationToken token)
|
||||
{
|
||||
var appointment = dto.Appointment.ToUniversalTime();
|
||||
var appointment = request.Appointment.ToUniversalTime();
|
||||
|
||||
var criteria = new List<(string FieldName, string Value)>
|
||||
{
|
||||
("DoctorId", dto.DoctorId.ToString()),
|
||||
("PatientId", dto.PatientId.ToString())
|
||||
("DoctorId", request.DoctorId.ToString()),
|
||||
("PatientId", request.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);
|
||||
var appointments = await FindAsync<Appointment>(criteria, token);
|
||||
|
||||
foreach (var app in appointments)
|
||||
if (app.AppointmentsList.Any(a => a.Date == appointment.Date))
|
||||
@@ -48,4 +38,27 @@ public class AppointmentsMongoDbService : MongoDbService, IAppointmentsMongoDbSe
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public async Task<List<Appointment>> FindPastAppointments(DateTime currentDate, CancellationToken token)
|
||||
{
|
||||
var filterBuilder = Builders<Appointment>.Filter;
|
||||
var filter = filterBuilder.Lt("AppointmentsList", currentDate);
|
||||
|
||||
return await GetCollection<Appointment>().Find(filter).ToListAsync(token);
|
||||
}
|
||||
|
||||
public async Task UpdateAppointment(Appointment appointment, CancellationToken token)
|
||||
{
|
||||
await ModifyAsync("_id", appointment.Id, appointment, token);
|
||||
}
|
||||
|
||||
public async Task DeleteAppointment(string appointmentId, CancellationToken token)
|
||||
{
|
||||
await DeleteByIdAsync<Appointment>(appointmentId, token);
|
||||
}
|
||||
|
||||
public async Task<List<Appointment>> GetAllAppointments(CancellationToken token)
|
||||
{
|
||||
return await GetCollection<Appointment>().Find(Builders<Appointment>.Filter.Empty).ToListAsync(token);
|
||||
}
|
||||
}
|
||||
@@ -2,12 +2,7 @@
|
||||
|
||||
namespace Infrastructure.Services.MongoDB;
|
||||
|
||||
public class ChatMongoDbService : MongoDbService, IChatMongoDbService
|
||||
public class ChatMongoDbService(string connectionString, string databaseName, string collectionName)
|
||||
: MongoDbService(connectionString, databaseName, collectionName), IChatMongoDbService
|
||||
{
|
||||
public ChatMongoDbService(string connectionString, string databaseName, string collectionName)
|
||||
: base(connectionString, databaseName, collectionName)
|
||||
{
|
||||
}
|
||||
|
||||
// Implement additional methods specific to Chat database if needed
|
||||
}
|
||||
@@ -2,12 +2,7 @@
|
||||
|
||||
namespace Infrastructure.Services.MongoDB;
|
||||
|
||||
public class MedicalHistoryMongoDbService : MongoDbService, IMedicalHistoryMongoDbService
|
||||
public class MedicalHistoryMongoDbService(string connectionString, string databaseName, string collectionName)
|
||||
: MongoDbService(connectionString, databaseName, collectionName), IMedicalHistoryMongoDbService
|
||||
{
|
||||
public MedicalHistoryMongoDbService(string connectionString, string databaseName, string collectionName)
|
||||
: base(connectionString, databaseName, collectionName)
|
||||
{
|
||||
}
|
||||
|
||||
// Implement additional methods specific to Medical History database if needed
|
||||
}
|
||||
@@ -9,23 +9,18 @@ public class MongoDbService
|
||||
private readonly IMongoClient _database;
|
||||
private readonly string _databaseName;
|
||||
|
||||
public MongoDbService(string connectionString, string databaseName, string collectionName)
|
||||
protected 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!");
|
||||
_database.GetDatabase("admin").RunCommand<BsonDocument>(new BsonDocument("ping", 1));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -33,12 +28,19 @@ public class MongoDbService
|
||||
}
|
||||
}
|
||||
|
||||
public MongoDbService(IMongoClient database, string collectionName, string databaseName)
|
||||
{
|
||||
_database = database;
|
||||
_collectionName = collectionName;
|
||||
_databaseName = databaseName;
|
||||
}
|
||||
|
||||
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)
|
||||
public async Task<List<T>> FindAsync<T>(List<(string FieldName, string Value)> criteria, CancellationToken token)
|
||||
{
|
||||
var collection = _database.GetDatabase(_databaseName).GetCollection<T>(_collectionName);
|
||||
|
||||
@@ -47,33 +49,34 @@ public class MongoDbService
|
||||
|
||||
var combinedFilter = Builders<T>.Filter.And(filters);
|
||||
|
||||
return await collection.Find(combinedFilter).ToListAsync();
|
||||
return await collection.Find(combinedFilter).ToListAsync(token);
|
||||
}
|
||||
|
||||
public async Task AddAsync<T>(T document)
|
||||
[Obsolete("Obsolete")]
|
||||
public async Task AddAsync<T>(T document, CancellationToken token)
|
||||
{
|
||||
var collection = _database.GetDatabase(_databaseName).GetCollection<T>(_collectionName);
|
||||
await collection.InsertOneAsync(document);
|
||||
await collection.InsertOneAsync(document, token);
|
||||
}
|
||||
|
||||
public async Task ModifyAsync<T>(string keyField, string keyValue, T document)
|
||||
public async Task ModifyAsync<T>(string keyField, string keyValue, T document, CancellationToken token)
|
||||
{
|
||||
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 });
|
||||
await collection.ReplaceOneAsync(filter, document, new ReplaceOptions { IsUpsert = true }, token);
|
||||
}
|
||||
|
||||
public async Task DeleteAsync<T>(string keyField, string keyValue)
|
||||
public async Task DeleteAsync<T>(string keyField, string keyValue, CancellationToken token)
|
||||
{
|
||||
var collection = _database.GetDatabase(_databaseName).GetCollection<T>(_collectionName);
|
||||
var filter = Builders<T>.Filter.Eq(keyField, keyValue);
|
||||
await collection.DeleteOneAsync(filter);
|
||||
await collection.DeleteOneAsync(filter, token);
|
||||
}
|
||||
|
||||
public async Task DeleteByIdAsync<T>(string id)
|
||||
public async Task DeleteByIdAsync<T>(string id, CancellationToken token)
|
||||
{
|
||||
var collection = _database.GetDatabase(_databaseName).GetCollection<T>(_collectionName);
|
||||
var filter = Builders<T>.Filter.Eq("_id", id);
|
||||
await collection.DeleteOneAsync(filter);
|
||||
await collection.DeleteOneAsync(filter, token);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user