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
@@ -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
};
}
}
@@ -0,0 +1,67 @@
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using FluentValidation;
namespace Application.Endpoints.Appointments.CreateAppointment;
public class CreateAppointmentValidator : AbstractValidator<AppointmentInformation>
{
private readonly IAppointmentsMongoDbService _appointmentsMongoDbService;
private readonly IDoctorRepository _doctorRepository;
private readonly IPatientRepository _patientRepository;
public CreateAppointmentValidator(IAppointmentsMongoDbService appointmentsMongoDbService,
IDoctorRepository doctorRepository, IPatientRepository patientRepository)
{
RuleFor(x => x.DoctorId)
.NotEmpty().WithMessage("Id is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MustAsync(IsDoctorRegistered).WithMessage("Doctor is not registered in system")
.WithErrorCode(HttpStatusCodes.NotFound.ToString());
RuleFor(x => x.PatientId)
.NotEmpty().WithMessage("Id is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MustAsync(IsPatientRegistered).WithMessage("Patient is not registered in system")
.WithErrorCode(HttpStatusCodes.NotFound.ToString());
RuleFor(x => x.Appointment)
.Must(IsAppointmentValidFormat).WithMessage("Appointment is not valid")
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
RuleFor(x => x)
.MustAsync(IsAppointmentUnique)
.WithMessage("Appointment already in system.")
.WithErrorCode(HttpStatusCodes.Conflict.ToString());
_appointmentsMongoDbService = appointmentsMongoDbService;
_doctorRepository = doctorRepository;
_patientRepository = patientRepository;
}
private async Task<bool> IsDoctorRegistered(Guid id, CancellationToken token)
{
var doctor = await _doctorRepository.GetByIdAsync(id, token);
return doctor != null;
}
private async Task<bool> IsPatientRegistered(Guid id, CancellationToken token)
{
var patient = await _patientRepository.GetByIdAsync(id, token);
return patient != null;
}
private bool IsAppointmentValidFormat(DateTime appointment)
{
if (appointment == default)
return false;
if (appointment.Date < DateTime.UtcNow.Date)
return false;
return true;
}
private async Task<bool> IsAppointmentUnique(AppointmentInformation request, CancellationToken token)
{
return await _appointmentsMongoDbService.IsAppointmentUnique(request, token);
}
}