using Application.Services.Database.MongoDB; using Domain.Entities; using MongoDB.Driver; using Application.Endpoints.Appointments; namespace Infrastructure.Services.MongoDB; public class AppointmentsMongoDbService(string connectionString, string databaseName, string collectionName) : MongoDbService(connectionString, databaseName, collectionName), IAppointmentsMongoDbService { public async Task 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(criteria, token); return appointments.All(a => !a.AppointmentsList.Any(dt => dt == request.Appointment)); } public async Task 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(criteria, token); foreach (var app in appointments) if (app.AppointmentsList.Any(a => a.Date == appointment.Date)) return true; return false; } public async Task> FindPastAppointments(DateTime currentDate, CancellationToken token) { var filterBuilder = Builders.Filter; var filter = filterBuilder.Lt("AppointmentsList", currentDate); return await GetCollection().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(appointmentId, token); } public async Task> GetAllAppointments(CancellationToken token) { return await GetCollection().Find(Builders.Filter.Empty).ToListAsync(token); } }