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,38 @@
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using Domain.Entities;
namespace Application.Endpoints.Doctors.DeleteDoctor;
public class DeleteDoctorHandler(
IDoctorRepository doctorRepository,
IAppointmentsMongoDbService appointmentsMongoDbService)
{
public async Task<BaseResponse> Handle(Guid id, CancellationToken token)
{
var doctor = await doctorRepository.GetByIdAsync(id, token);
if (doctor == null)
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = "Doctor was not found.",
Data = null
};
await doctorRepository.DeleteAsync(doctor, token);
var criteria = new List<(string FieldName, string Value)>
{
("DoctorId", id.ToString())
};
var appointments = await appointmentsMongoDbService.FindAsync<Appointment>(criteria, token);
if (appointments.Count != 0)
await appointmentsMongoDbService.DeleteByIdAsync<Appointment>(appointments[0].Id, token);
return new BaseResponse
{
StatusCode = HttpStatusCodes.NoContent,
Message = null,
Data = null
};
}
}
@@ -0,0 +1,10 @@
namespace Application.Endpoints.Doctors.ModifyDoctor;
public class ModifyDoctorCommand
{
public Guid Id { get; set; } = Guid.Empty;
public string Name { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
}
@@ -0,0 +1,47 @@
using Application.Services.Database.PostgreSQL;
using Application.Services.HashingAlgorithms;
namespace Application.Endpoints.Doctors.ModifyDoctor;
public class ModifyDoctorHandler(
IHashingAlgorithms hashingAlgorithms,
IDoctorRepository doctorRepository,
IPatientRepository patientRepository,
IAdminRepository adminRepository)
{
public async Task<BaseResponse> Handle(ModifyDoctorCommand request, CancellationToken token)
{
var validation = new ModifyDoctorValidator(doctorRepository, patientRepository, adminRepository);
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 newDoctor = await doctorRepository.GetByIdAsync(request.Id, token);
newDoctor.SetEmail(request.Email);
newDoctor.SetPassword(hashingAlgorithms.Sha256Algorithm(request.Password));
newDoctor.SetName(request.Name);
newDoctor.SetDescription(request.Description);
await doctorRepository.UpdateAsync(newDoctor, token);
return new BaseResponse
{
StatusCode = HttpStatusCodes.NoContent,
Message = null,
Data = null
};
}
}
@@ -1,25 +1,30 @@
using Application.Services.Database.PostgreSQL;
using FluentValidation;
namespace Application.Endpoints.Doctors.Profile;
namespace Application.Endpoints.Doctors.ModifyDoctor;
public class DoctorProfileValidation : AbstractValidator<DoctorProfileUpdateDto>
public class ModifyDoctorValidator : AbstractValidator<ModifyDoctorCommand>
{
private readonly IDoctorRepository _doctorRepository;
private readonly IPatientRepository _patientRepository;
private readonly IAdminRepository _adminRepository;
public DoctorProfileValidation(IDoctorRepository doctorRepository)
public ModifyDoctorValidator(IDoctorRepository doctorRepository,
IPatientRepository patientRepository, IAdminRepository adminRepository)
{
_doctorRepository = doctorRepository;
_patientRepository = patientRepository;
_adminRepository = adminRepository;
RuleFor(x => x.Id)
.NotEmpty().WithMessage("Id is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MustAsync(IsDoctorRegistered).WithMessage("Doctor is not registered in system")
.MustAsync(IsDoctorRegistered).WithMessage("Patient is not registered in system")
.WithErrorCode(HttpStatusCodes.NotFound.ToString());
RuleFor(x => x.Email)
.NotEmpty().WithMessage("Email is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.EmailAddress().WithMessage("Invalid email format.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MustAsync(BeUniqueEmail).WithMessage("Email in use by another doctor.")
.MustAsync(BeUniqueEmail).WithMessage("Email in use by another patient.")
.WithErrorCode(HttpStatusCodes.Conflict.ToString());
RuleFor(x => x.Password)
@@ -31,21 +36,20 @@ public class DoctorProfileValidation : AbstractValidator<DoctorProfileUpdateDto>
.NotEmpty().WithMessage("Name is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MinimumLength(3).WithMessage("Name must be at least 3 characters long.")
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
RuleFor(x => x.Description)
.MaximumLength(3000).WithMessage("Description must not exceed 3000 characters.")
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
}
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> BeUniqueEmail(string email, CancellationToken cancellationToken)
private async Task<bool> BeUniqueEmail(string email, CancellationToken token)
{
var doctor = await _doctorRepository.FindByEmailAsync(email);
return doctor == null;
var patient = await _patientRepository.FindByEmailAsync(email, token);
var doctor = await _doctorRepository.FindByEmailAsync(email, token);
var admin = await _adminRepository.FindByEmailAsync(email, token);
return patient == null && admin == null;
}
}
@@ -1,110 +0,0 @@
using Application.Services.Database.PostgreSQL;
using Application.Services.HashingAlgorithms;
namespace Application.Endpoints.Doctors.Profile;
public class DoctorProfileHandler
{
private readonly IDoctorRepository _database;
private readonly IHashingAlgorithms _hashingAlgorithms;
public DoctorProfileHandler(IDoctorRepository database, IHashingAlgorithms hashingAlgorithms)
{
_database = database;
_hashingAlgorithms = hashingAlgorithms;
}
public async Task<BaseResponse> HandleGet(Guid id)
{
var doctor = await _database.GetByIdAsync(id).ConfigureAwait(false);
if (doctor != null)
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = $"Retrieved doctor with id: {id}",
Data = doctor
};
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = $"Doctor with id: {id} not found",
Data = null
};
}
public async Task<BaseResponse> HandleGetAll()
{
var doctors = await _database.GetAllAsync().ConfigureAwait(false);
if (doctors.Any())
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = "Retrieved doctors",
Data = doctors.ToList()
};
return new BaseResponse
{
StatusCode = HttpStatusCodes.NoContent,
Message = "Doctors not found",
Data = null
};
}
public async Task<BaseResponse> HandleUpdate(DoctorProfileUpdateDto doctorProfileUpdateDto)
{
var validation = new DoctorProfileValidation(_database);
var validationResult = await validation.ValidateAsync(doctorProfileUpdateDto);
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 doctorToUpdate = await _database.GetByIdAsync(doctorProfileUpdateDto.Id);
doctorToUpdate.SetEmail(doctorProfileUpdateDto.Email);
doctorToUpdate.SetPassword(_hashingAlgorithms.SHA256Algorithm(doctorProfileUpdateDto.Password));
doctorToUpdate.SetName(doctorProfileUpdateDto.Name);
doctorToUpdate.SetDescription(doctorProfileUpdateDto.Description);
await _database.UpdateAsync(doctorToUpdate);
return new BaseResponse
{
StatusCode = HttpStatusCodes.NoContent,
Message = null,
Data = null
};
}
public async Task<BaseResponse> HandleDelete(Guid id)
{
var doctorToDelete = await _database.GetByIdAsync(id);
if (doctorToDelete == null)
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = "Doctor was not found.",
Data = null
};
await _database.DeleteAsync(doctorToDelete);
return new BaseResponse
{
StatusCode = HttpStatusCodes.NoContent,
Message = null,
Data = null
};
}
}
@@ -1,10 +0,0 @@
namespace Application.Endpoints.Doctors.Profile;
public class DoctorProfileUpdateDto
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public string Description { get; set; }
}
@@ -0,0 +1,36 @@
using Application.Services.Database.PostgreSQL;
namespace Application.Endpoints.Doctors.QuerriesDoctors;
public class QuerriesDoctorsHandler(IDoctorRepository database)
{
public async Task<BaseResponse> HandleGet(Guid id, CancellationToken token)
{
var doctor = await database.GetByIdAsync(id, token);
if (doctor != null)
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = $"Retrieved doctor with id: {id}",
Data = doctor
};
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = $"Doctor with id: {id} not found",
Data = null
};
}
public async Task<BaseResponse> HandleGetAll(CancellationToken token)
{
var doctors = await database.GetAllAsync(token);
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = "Retrieved doctors",
Data = doctors.ToList()
};
}
}
@@ -1,9 +0,0 @@
namespace Application.Endpoints.Doctors.Registration;
public class DoctorRegistrationDto
{
public string? Name { get; set; }
public string? Email { get; set; }
public string? Password { get; set; }
public string? Description { get; set; }
}
@@ -1,52 +0,0 @@
using Application.Services.Database.PostgreSQL;
using Application.Services.HashingAlgorithms;
using Core.Entities;
namespace Application.Endpoints.Doctors.Registration;
public class DoctorRegistrationHandler
{
private readonly IDoctorRepository _doctorRepository;
private readonly IHashingAlgorithms _hashingAlgorithms;
public DoctorRegistrationHandler(IDoctorRepository doctorRepository, IHashingAlgorithms hashingAlgorithms)
{
_doctorRepository = doctorRepository;
_hashingAlgorithms = hashingAlgorithms;
}
public async Task<BaseResponse> Handle(DoctorRegistrationDto registrationDTO)
{
var validation = new DoctorRegistrationValidation(_doctorRepository);
var validationResult = await validation.ValidateAsync(registrationDTO);
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 doctor = new Doctor();
doctor.SetEmail(registrationDTO.Email);
doctor.SetPassword(_hashingAlgorithms.SHA256Algorithm(registrationDTO.Password));
doctor.SetName(registrationDTO.Name);
doctor.SetDescription(registrationDTO.Description);
await _doctorRepository.AddAsync(doctor);
return new BaseResponse
{
StatusCode = HttpStatusCodes.Created,
Message = "Doctor registered successfully",
Data = null
};
}
}
@@ -1,40 +0,0 @@
using Application.Services.Database.PostgreSQL;
using FluentValidation;
namespace Application.Endpoints.Doctors.Registration;
public class DoctorRegistrationValidation : AbstractValidator<DoctorRegistrationDto>
{
private readonly IDoctorRepository _doctorRepository;
public DoctorRegistrationValidation(IDoctorRepository doctorRepository)
{
_doctorRepository = doctorRepository;
RuleFor(x => x.Email)
.NotEmpty().WithMessage("Email is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.EmailAddress().WithMessage("Invalid email format.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MustAsync(BeUniqueEmail).WithMessage("Email already exists.")
.WithErrorCode(HttpStatusCodes.Conflict.ToString());
RuleFor(x => x.Password)
.NotEmpty().WithMessage("Password is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MinimumLength(8).WithMessage("Password must be at least 8 characters long.")
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
RuleFor(x => x.Name)
.NotEmpty().WithMessage("Name is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MinimumLength(3).WithMessage("Name must be at least 3 characters long.")
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
RuleFor(x => x.Description)
.MaximumLength(3000).WithMessage("Description must not exceed 3000 characters.")
.WithErrorCode(HttpStatusCodes.BadRequest.ToString());
}
private async Task<bool> BeUniqueEmail(string email, CancellationToken cancellationToken)
{
var doctor = await _doctorRepository.FindByEmailAsync(email);
return doctor == null;
}
}