Files
2024-05-30 15:40:13 +03:00

64 lines
2.3 KiB
C#

using Application.Endpoints.Appointments;
using Application.Services.Database.MongoDB;
using Domain.Entities;
using MongoDB.Driver;
namespace Infrastructure.Services.MongoDB;
public class AppointmentsMongoDbService(string connectionString, string databaseName, string collectionName)
: MongoDbService(connectionString, databaseName, collectionName), IAppointmentsMongoDbService
{
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())
};
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 = request.Appointment.ToUniversalTime();
var criteria = new List<(string FieldName, string Value)>
{
("DoctorId", request.DoctorId.ToString()),
("PatientId", request.PatientId.ToString())
};
var appointments = await FindAsync<Appointment>(criteria, token);
foreach (var app in appointments)
if (app.AppointmentsList.Any(a => a.Date == appointment.Date))
return true;
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);
}
}