API Traieste
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
namespace Application.Endpoints
|
||||
{
|
||||
public class BaseResponse
|
||||
{
|
||||
public bool Success { get; set; }
|
||||
public string? Message { get; set; }
|
||||
public object? Data { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Application.Endpoints.Doctors.Login
|
||||
{
|
||||
public class DoctorLoginDTO
|
||||
{
|
||||
public string? Email { get; set; }
|
||||
public string? Password { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using Application.Services.Database;
|
||||
|
||||
namespace Application.Endpoints.Doctors.Login
|
||||
{
|
||||
public class DoctorLoginHandler
|
||||
{
|
||||
private readonly IDoctorRepository _database;
|
||||
|
||||
public DoctorLoginHandler(IDoctorRepository database)
|
||||
{
|
||||
_database = database;
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> Handle(DoctorLoginDTO loginDTO)
|
||||
{
|
||||
var validation = new DoctorLoginValidation(_database);
|
||||
var validationResult = await validation.ValidateAsync(loginDTO);
|
||||
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
|
||||
var errorMessage = validationResult.Errors.FirstOrDefault()?.ErrorMessage;
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = errorMessage,
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = "Authentication successful",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using Application.Services.Database;
|
||||
using FluentValidation;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Application.Endpoints.Doctors.Login
|
||||
{
|
||||
public class DoctorLoginValidation : AbstractValidator<DoctorLoginDTO>
|
||||
{
|
||||
private readonly IDoctorRepository _doctorRepository;
|
||||
|
||||
public DoctorLoginValidation(IDoctorRepository doctorRepository)
|
||||
{
|
||||
_doctorRepository = doctorRepository;
|
||||
|
||||
RuleFor(x => x.Email)
|
||||
.NotEmpty().WithMessage("Email is required.")
|
||||
.EmailAddress().WithMessage("Invalid email format.")
|
||||
.MustAsync(BeExistingDoctor).WithMessage("Doctor with this email does not exist.");
|
||||
|
||||
RuleFor(x => x.Password)
|
||||
.NotEmpty().WithMessage("Password is required.")
|
||||
.MinimumLength(8).WithMessage("Password must be at least 8 characters long.");
|
||||
}
|
||||
|
||||
private async Task<bool> BeExistingDoctor(string email, System.Threading.CancellationToken cancellationToken)
|
||||
{
|
||||
return await _doctorRepository.IsDoctorExisting(email);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user