api v2.0
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
namespace Application.Endpoints.Patients.Registration;
|
||||
|
||||
public class PatientRegistrationDto
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string Password { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
using Application.Services.Database;
|
||||
using Application.Services.HashingAlgorithms;
|
||||
using Core.Entities;
|
||||
|
||||
namespace Application.Endpoints.Patients.Registration;
|
||||
|
||||
public class PatientRegistrationHandler
|
||||
{
|
||||
private readonly IHashingAlgorithms _hashingAlgorithms;
|
||||
private readonly IPatientRepository _patientRepository;
|
||||
|
||||
public PatientRegistrationHandler(IPatientRepository patientRepository, IHashingAlgorithms hashingAlgorithms)
|
||||
{
|
||||
_patientRepository = patientRepository;
|
||||
_hashingAlgorithms = hashingAlgorithms;
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> Handle(PatientRegistrationDto registrationDTO)
|
||||
{
|
||||
var validation = new PatientRegistrationValidation(_patientRepository);
|
||||
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 patient = new Patient();
|
||||
patient.SetEmail(registrationDTO.Email);
|
||||
patient.SetName(registrationDTO.Name);
|
||||
patient.SetPassword(_hashingAlgorithms.SHA256Algorithm(registrationDTO.Password));
|
||||
|
||||
await _patientRepository.AddAsync(patient);
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
StatusCode = HttpStatusCodes.Created,
|
||||
Message = "Patient registered successfully",
|
||||
Data = patient // Be careful with sending sensitive data like Passwords
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using Application.Services.Database;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.Patients.Registration;
|
||||
|
||||
public class PatientRegistrationValidation : AbstractValidator<PatientRegistrationDto>
|
||||
{
|
||||
private readonly IPatientRepository _patientRepository;
|
||||
|
||||
public PatientRegistrationValidation(IPatientRepository patientRepository)
|
||||
{
|
||||
_patientRepository = patientRepository;
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
private async Task<bool> BeUniqueEmail(string email, CancellationToken cancellationToken)
|
||||
{
|
||||
var pacient = await _patientRepository.FindByEmailAsync(email);
|
||||
return pacient == null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user