finalizare 1.0

This commit is contained in:
andrei-mihnea-cerbu
2024-05-21 12:10:53 +03:00
parent f7795f7519
commit 1cc1d34003
11268 changed files with 2102399 additions and 10909 deletions
@@ -1,6 +1,6 @@
namespace Application.Endpoints.Appointments;
public class AppointmentManagementDto
public class AppointmentInformation
{
public Guid DoctorId { get; set; }
public Guid PatientId { get; set; }
@@ -1,165 +0,0 @@
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using Core.Entities;
namespace Application.Endpoints.Appointments;
public class AppointmentManagementHandler
{
private readonly IAppointmentsMongoDbService _appointmentsMongoDbService;
private readonly IDoctorRepository _doctorRepository;
private readonly IPatientRepository _patientRepository;
public AppointmentManagementHandler(IAppointmentsMongoDbService appointmentsMongoDbService,
IPatientRepository patientRepository, IDoctorRepository doctorRepository)
{
_appointmentsMongoDbService = appointmentsMongoDbService;
_patientRepository = patientRepository;
_doctorRepository = doctorRepository;
}
public async Task<BaseResponse> HandleCreateAppointment(AppointmentManagementDto dto)
{
var validation = new CreateAppointmentValidator(_appointmentsMongoDbService,
_doctorRepository, _patientRepository);
var validationResult = await validation.ValidateAsync(dto);
if (!validationResult.IsValid)
{
var firstError = validationResult.Errors.FirstOrDefault();
var errorCode = int.TryParse(firstError.ErrorCode, out var code) ? code : -1;
var errorMessage = firstError.ErrorMessage;
return new BaseResponse
{
StatusCode = errorCode,
Message = errorMessage,
Data = null
};
}
var appointmentId = IdentifierGenerator.GenerateId(dto.DoctorId, dto.PatientId);
var criteria = new List<(string FieldName, string Value)>
{
("_id", appointmentId)
};
var appointments = await _appointmentsMongoDbService.FindAsync<Appointment>(criteria);
if (!appointments.Any())
{
var appointment = new Appointment();
appointment.SetId(appointmentId);
appointment.SetDoctorIid(dto.DoctorId.ToString());
appointment.SetPatientId(dto.PatientId.ToString());
appointment.AddAppointment(dto.Appointment);
await _appointmentsMongoDbService.AddAsync(appointment);
}
else
{
var appointment = appointments[0];
appointment.AddAppointment(dto.Appointment);
await _appointmentsMongoDbService.ModifyAsync("_id", appointmentId, appointment);
}
return new BaseResponse
{
StatusCode = HttpStatusCodes.Created,
Message = "Appointment successfully created.",
Data = null
};
}
public async Task<BaseResponse> HandleDeleteAppointment(AppointmentManagementDto dto)
{
var validation = new DeleteAppointmentValidator(_appointmentsMongoDbService,
_doctorRepository, _patientRepository);
var validationResult = await validation.ValidateAsync(dto);
if (!validationResult.IsValid)
{
var firstError = validationResult.Errors.FirstOrDefault();
var errorCode = int.TryParse(firstError.ErrorCode, out var code) ? code : -1;
var errorMessage = firstError.ErrorMessage;
return new BaseResponse
{
StatusCode = errorCode,
Message = errorMessage,
Data = null
};
}
var appointmentId = IdentifierGenerator.GenerateId(dto.DoctorId, dto.PatientId);
var criteria = new List<(string FieldName, string Value)>
{
("_id", appointmentId)
};
var appointments = await _appointmentsMongoDbService.FindAsync<Appointment>(criteria);
if (!appointments.Any())
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = "Appointment not found in system.",
Data = null
};
var appointment = appointments[0];
appointment.RemoveAppointment(dto.Appointment);
await _appointmentsMongoDbService.ModifyAsync("_id", appointmentId, appointment);
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = "Appointment successfully removed.",
Data = null
};
}
public async Task<BaseResponse> HandleGetAppointmentsByPatientId(Guid patientId)
{
var criteria = new List<(string FieldName, string Value)>
{
("PatientId", patientId.ToString())
};
var appointments = await _appointmentsMongoDbService.FindAsync<Appointment>(criteria);
if (appointments.Count == 0)
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = $"Appointments not found in system for patient with id {patientId}.",
Data = null
};
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = $"Appointments for patient with id {patientId} successfully retrieved.",
Data = appointments
};
}
public async Task<BaseResponse> HandleGetAppointmentsByDoctorId(Guid doctorId)
{
var criteria = new List<(string FieldName, string Value)>
{
("DoctorId", doctorId.ToString())
};
var appointments = await _appointmentsMongoDbService.FindAsync<Appointment>(criteria);
if (appointments.Count == 0)
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = $"Appointments not found in system for doctor with id {doctorId}.",
Data = null
};
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = $"Appointments for doctor with id {doctorId} successfully retrieved.",
Data = appointments
};
}
}
@@ -0,0 +1,63 @@
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using Domain.Entities;
namespace Application.Endpoints.Appointments.CreateAppointment;
public class CreateAppointmentHandler(
IAppointmentsMongoDbService appointmentsMongoDbService,
IPatientRepository patientRepository,
IDoctorRepository doctorRepository)
{
public async Task<BaseResponse> Handle(AppointmentInformation request, CancellationToken token)
{
var validation = new CreateAppointmentValidator(appointmentsMongoDbService,
doctorRepository, patientRepository);
var validationResult = await validation.ValidateAsync(request, token);
if (!validationResult.IsValid)
{
var firstError = validationResult.Errors.FirstOrDefault();
var errorCode = int.TryParse(firstError.ErrorCode, out var code) ? code : -1;
var errorMessage = firstError.ErrorMessage;
return new BaseResponse
{
StatusCode = errorCode,
Message = errorMessage,
Data = null
};
}
var appointmentId = IdentifierGenerator.GenerateId(request.DoctorId, request.PatientId);
var criteria = new List<(string FieldName, string Value)>
{
("_id", appointmentId)
};
var appointments = await appointmentsMongoDbService.FindAsync<Appointment>(criteria, token);
if (appointments.Count == 0)
{
var appointment = new Appointment();
appointment.SetId(appointmentId);
appointment.SetDoctorIid(request.DoctorId.ToString());
appointment.SetPatientId(request.PatientId.ToString());
appointment.AddAppointment(request.Appointment);
await appointmentsMongoDbService.AddAsync(appointment, token);
}
else
{
var appointment = appointments[0];
appointment.AddAppointment(request.Appointment);
await appointmentsMongoDbService.ModifyAsync("_id", appointmentId, appointment, token);
}
return new BaseResponse
{
StatusCode = HttpStatusCodes.Created,
Message = "Appointment successfully created.",
Data = null
};
}
}
@@ -2,9 +2,9 @@
using Application.Services.Database.PostgreSQL;
using FluentValidation;
namespace Application.Endpoints.Appointments;
namespace Application.Endpoints.Appointments.CreateAppointment;
public class CreateAppointmentValidator : AbstractValidator<AppointmentManagementDto>
public class CreateAppointmentValidator : AbstractValidator<AppointmentInformation>
{
private readonly IAppointmentsMongoDbService _appointmentsMongoDbService;
private readonly IDoctorRepository _doctorRepository;
@@ -28,7 +28,8 @@ public class CreateAppointmentValidator : AbstractValidator<AppointmentManagemen
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
RuleFor(x => x)
.MustAsync(IsAppointmentUnique).WithMessage("Appointment already in system.")
.MustAsync(IsAppointmentUnique)
.WithMessage("Appointment already in system.")
.WithErrorCode(HttpStatusCodes.Conflict.ToString());
_appointmentsMongoDbService = appointmentsMongoDbService;
@@ -36,15 +37,15 @@ public class CreateAppointmentValidator : AbstractValidator<AppointmentManagemen
_patientRepository = patientRepository;
}
private async Task<bool> IsDoctorRegistered(Guid id, CancellationToken cancellationToken)
private async Task<bool> IsDoctorRegistered(Guid id, CancellationToken token)
{
var doctor = await _doctorRepository.GetByIdAsync(id);
var doctor = await _doctorRepository.GetByIdAsync(id, token);
return doctor != null;
}
private async Task<bool> IsPatientRegistered(Guid id, CancellationToken cancellationToken)
private async Task<bool> IsPatientRegistered(Guid id, CancellationToken token)
{
var patient = await _patientRepository.GetByIdAsync(id);
var patient = await _patientRepository.GetByIdAsync(id, token);
return patient != null;
}
@@ -59,8 +60,8 @@ public class CreateAppointmentValidator : AbstractValidator<AppointmentManagemen
return true;
}
private async Task<bool> IsAppointmentUnique(AppointmentManagementDto dto, CancellationToken cancellationToken)
private async Task<bool> IsAppointmentUnique(AppointmentInformation request, CancellationToken token)
{
return await _appointmentsMongoDbService.IsAppointmentUnique(dto);
return await _appointmentsMongoDbService.IsAppointmentUnique(request, token);
}
}
@@ -0,0 +1,58 @@
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using Domain.Entities;
namespace Application.Endpoints.Appointments.DeleteAppointment;
public class DeleteAppointmentHandler(
IAppointmentsMongoDbService appointmentsMongoDbService,
IPatientRepository patientRepository,
IDoctorRepository doctorRepository)
{
public async Task<BaseResponse> Handle(AppointmentInformation request, CancellationToken token)
{
var validation = new DeleteAppointmentValidator(appointmentsMongoDbService,
doctorRepository, patientRepository);
var validationResult = await validation.ValidateAsync(request, token);
if (!validationResult.IsValid)
{
var firstError = validationResult.Errors.FirstOrDefault();
var errorCode = int.TryParse(firstError.ErrorCode, out var code) ? code : -1;
var errorMessage = firstError.ErrorMessage;
return new BaseResponse
{
StatusCode = errorCode,
Message = errorMessage,
Data = null
};
}
var appointmentId = IdentifierGenerator.GenerateId(request.DoctorId, request.PatientId);
var criteria = new List<(string FieldName, string Value)>
{
("_id", appointmentId)
};
var appointments = await appointmentsMongoDbService.FindAsync<Appointment>(criteria, token);
if (!appointments.Any())
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = "Appointment not found in system.",
Data = null
};
var appointment = appointments[0];
appointment.RemoveAppointment(request.Appointment);
await appointmentsMongoDbService.ModifyAsync("_id", appointmentId, appointment, token);
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = "Appointment successfully removed.",
Data = null
};
}
}
@@ -1,10 +1,11 @@
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using Domain.Entities;
using FluentValidation;
namespace Application.Endpoints.Appointments;
namespace Application.Endpoints.Appointments.DeleteAppointment;
public class DeleteAppointmentValidator : AbstractValidator<AppointmentManagementDto>
public class DeleteAppointmentValidator : AbstractValidator<AppointmentInformation>
{
private readonly IAppointmentsMongoDbService _appointmentsMongoDbService;
private readonly IDoctorRepository _doctorRepository;
@@ -36,15 +37,15 @@ public class DeleteAppointmentValidator : AbstractValidator<AppointmentManagemen
_patientRepository = patientRepository;
}
private async Task<bool> IsDoctorRegistered(Guid id, CancellationToken cancellationToken)
private async Task<bool> IsDoctorRegistered(Guid id, CancellationToken token)
{
var doctor = await _doctorRepository.GetByIdAsync(id);
var doctor = await _doctorRepository.GetByIdAsync(id, token);
return doctor != null;
}
private async Task<bool> IsPatientRegistered(Guid id, CancellationToken cancellationToken)
private async Task<bool> IsPatientRegistered(Guid id, CancellationToken token)
{
var patient = await _patientRepository.GetByIdAsync(id);
var patient = await _patientRepository.GetByIdAsync(id, token);
return patient != null;
}
@@ -59,8 +60,8 @@ public class DeleteAppointmentValidator : AbstractValidator<AppointmentManagemen
return true;
}
private async Task<bool> DoesAppointmentExists(AppointmentManagementDto dto, CancellationToken cancellationToken)
private async Task<bool> DoesAppointmentExists(AppointmentInformation request, CancellationToken token)
{
return await _appointmentsMongoDbService.DoesAppointmentExists(dto);
return await _appointmentsMongoDbService.DoesAppointmentExists(request, token);
}
}
@@ -0,0 +1,54 @@
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using Domain.Entities;
namespace Application.Endpoints.Appointments.QuerriesAppointments;
public class QuerriesAppointmentHandler(IAppointmentsMongoDbService appointmentsMongoDbService)
{
public async Task<BaseResponse> GetByPatientId(Guid patientId, CancellationToken token)
{
var criteria = new List<(string FieldName, string Value)>
{
("PatientId", patientId.ToString())
};
var appointments = await appointmentsMongoDbService.FindAsync<Appointment>(criteria, token);
if (appointments.Count == 0)
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = $"Appointments not found in system.",
Data = null
};
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = $"Appointments successfully retrieved.",
Data = appointments
};
}
public async Task<BaseResponse> GetsByDoctorId(Guid doctorId, CancellationToken token)
{
var criteria = new List<(string FieldName, string Value)>
{
("DoctorId", doctorId.ToString())
};
var appointments = await appointmentsMongoDbService.FindAsync<Appointment>(criteria, token);
if (appointments.Count == 0)
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = $"Appointments not found in system.",
Data = null
};
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = $"Appointments successfully retrieved.",
Data = appointments
};
}
}