Backend Pacients, Doctors and MedicalHistory endpoints
- added for Pacients: POST: /register POST: /login POST: /reset_password PUT: /profile DELETE: /profile - added for Doctors: POST: /register POST: /login POST: /reset_password PUT: /profile DELETE: /profile - added for MedicalHistories: POST: /:id (only by pacient) - done GET: /:id (only by pacient) - done PUT: /:id (only by doctor) - done PUT /grand_access - TODO
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
namespace Application.Endpoints.Pacients.Login;
|
||||
|
||||
public class PacientLoginDTO
|
||||
{
|
||||
public string? Email { get; set; }
|
||||
public string? Password { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using Application.Services.Database;
|
||||
|
||||
namespace Application.Endpoints.Pacients.Login;
|
||||
|
||||
public class PacientLoginHandler
|
||||
{
|
||||
private readonly IPacientRepository _database;
|
||||
|
||||
public PacientLoginHandler(IPacientRepository database)
|
||||
{
|
||||
_database = database;
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> Handle(PacientLoginDTO loginDTO)
|
||||
{
|
||||
var validation = new PacientLoginValidation(_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,29 @@
|
||||
using Application.Services.Database;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.Pacients.Login;
|
||||
|
||||
public class PacientLoginValidation : AbstractValidator<PacientLoginDTO>
|
||||
{
|
||||
private readonly IPacientRepository _pacientRepository;
|
||||
|
||||
public PacientLoginValidation(IPacientRepository pacientRepository)
|
||||
{
|
||||
_pacientRepository = pacientRepository;
|
||||
|
||||
RuleFor(x => x.Email)
|
||||
.NotEmpty().WithMessage("Email is required.")
|
||||
.EmailAddress().WithMessage("Invalid email format.")
|
||||
.MustAsync(BeExistingPacient).WithMessage("Pacient 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> BeExistingPacient(string email, CancellationToken cancellationToken)
|
||||
{
|
||||
var pacient = await _pacientRepository.FindByEmailAsync(email);
|
||||
return pacient != null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Application.Endpoints.Pacients.Profile;
|
||||
|
||||
public class PacientProfileDTO
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string Password { get; set; }
|
||||
public string Description { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
using Application.Services.Database;
|
||||
|
||||
namespace Application.Endpoints.Pacients.Profile;
|
||||
|
||||
public class PacientProfileHandler
|
||||
{
|
||||
private readonly IPacientRepository _pacientRepository;
|
||||
|
||||
public PacientProfileHandler(IPacientRepository pacientRepository)
|
||||
{
|
||||
_pacientRepository = pacientRepository;
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> HandleGet(Guid id)
|
||||
{
|
||||
var pacient = await _pacientRepository.GetByIdAsync(id).ConfigureAwait(false);
|
||||
if (pacient != null)
|
||||
{
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = $"Retrieved pacient with id: {id}",
|
||||
Data = pacient
|
||||
};
|
||||
}
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = $"Pacient with id: {id} not found",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> HandleGetAll()
|
||||
{
|
||||
var pacients = await _pacientRepository.GetAllAsync().ConfigureAwait(false);
|
||||
if (pacients.Any())
|
||||
{
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = "Retrieved pacients",
|
||||
Data = pacients.ToList()
|
||||
};
|
||||
}
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = "Pacients not found",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> HandleUpdate(Guid id, PacientProfileDTO updateDto)
|
||||
{
|
||||
var validation = new PacientProfileValidation(_pacientRepository);
|
||||
var validationResult = await validation.ValidateAsync(updateDto);
|
||||
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
var errorMessage = validationResult.Errors.Select(e => e.ErrorMessage).ToList();
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = string.Join(", ", errorMessage),
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
var pacientToUpdate = await _pacientRepository.GetByIdAsync(id);
|
||||
|
||||
if (pacientToUpdate == null)
|
||||
{
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = "Pacient not found for given Id",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
pacientToUpdate.Email = updateDto.Email;
|
||||
pacientToUpdate.Password = updateDto.Password;
|
||||
pacientToUpdate.Name = updateDto.Name;
|
||||
|
||||
await _pacientRepository.UpdateAsync(pacientToUpdate);
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = "Pacient updated successfully",
|
||||
Data = pacientToUpdate
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> HandleDelete(Guid id)
|
||||
{
|
||||
var pacientToDelete = await _pacientRepository.GetByIdAsync(id);
|
||||
if (pacientToDelete == null)
|
||||
{
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = $"Pacient with id: {id} does not exist",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
await _pacientRepository.DeleteAsync(pacientToDelete);
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = $"Pacient with id: {id} was succesfully deleted",
|
||||
Data = pacientToDelete
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using Application.Services.Database;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.Pacients.Profile;
|
||||
|
||||
public class PacientProfileValidation : AbstractValidator<PacientProfileDTO>
|
||||
{
|
||||
private readonly IPacientRepository _pacientRepository;
|
||||
|
||||
public PacientProfileValidation(IPacientRepository pacientRepository)
|
||||
{
|
||||
_pacientRepository = pacientRepository;
|
||||
|
||||
RuleFor(x => x.Email)
|
||||
.NotEmpty().WithMessage("Email is required.")
|
||||
.EmailAddress().WithMessage("Invalid email format.")
|
||||
.MustAsync(BeUniqueEmail).WithMessage("Email in use by another pacient.");
|
||||
|
||||
RuleFor(x => x.Password)
|
||||
.NotEmpty().WithMessage("Password is required.")
|
||||
.MinimumLength(8).WithMessage("Password must be at least 8 characters long.");
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Name is required.")
|
||||
.MinimumLength(3).WithMessage("Name must be at least 3 characters long.");
|
||||
|
||||
RuleFor(x => x.Description)
|
||||
.MaximumLength(3000).WithMessage("Description must not exceed 3000 characters.");
|
||||
}
|
||||
|
||||
private async Task<bool> BeUniqueEmail(string email, CancellationToken cancellationToken)
|
||||
{
|
||||
var pacient = await _pacientRepository.FindByEmailAsync(email);
|
||||
if (pacient != null)
|
||||
{
|
||||
return pacient.Email.Equals(email, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
return pacient == null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Application.Endpoints.Pacients.Login;
|
||||
|
||||
public class PacientRegistrationDto
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string Password { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using Application.Endpoints.Pacients.Login;
|
||||
using Application.Services.Database;
|
||||
using Core.Entities;
|
||||
|
||||
namespace Application.Endpoints.Pacients.Registration;
|
||||
|
||||
public class PacientRegistrationHandler
|
||||
{
|
||||
private readonly IPacientRepository _pacientRepository;
|
||||
|
||||
public PacientRegistrationHandler(IPacientRepository pacientRepository)
|
||||
{
|
||||
_pacientRepository = pacientRepository;
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> Handle(PacientRegistrationDto registrationDTO)
|
||||
{
|
||||
var validation = new PacientRegistrationValidation(_pacientRepository);
|
||||
var validationResult = await validation.ValidateAsync(registrationDTO);
|
||||
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
var errorMessage = validationResult.Errors.Select(e => e.ErrorMessage).ToList();
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = string.Join(", ", errorMessage),
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
var pacient = new Pacient
|
||||
{
|
||||
Email = registrationDTO.Email,
|
||||
Password = registrationDTO.Password,
|
||||
Name = registrationDTO.Name
|
||||
};
|
||||
|
||||
await _pacientRepository.AddAsync(pacient);
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = "Pacient registered successfully",
|
||||
Data = pacient // Be careful with sending sensitive data like Passwords
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using Application.Endpoints.Pacients.Login;
|
||||
using Application.Services.Database;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.Pacients.Registration;
|
||||
|
||||
public class PacientRegistrationValidation : AbstractValidator<PacientRegistrationDto>
|
||||
{
|
||||
private readonly IPacientRepository _pacientRepository;
|
||||
|
||||
public PacientRegistrationValidation(IPacientRepository pacientRepository)
|
||||
{
|
||||
_pacientRepository = pacientRepository;
|
||||
|
||||
RuleFor(x => x.Email)
|
||||
.NotEmpty().WithMessage("Email is required.")
|
||||
.EmailAddress().WithMessage("Invalid email format.")
|
||||
.MustAsync(BeUniqueEmail).WithMessage("Email already exists.");
|
||||
|
||||
RuleFor(x => x.Password)
|
||||
.NotEmpty().WithMessage("Password is required.")
|
||||
.MinimumLength(8).WithMessage("Password must be at least 8 characters long.");
|
||||
|
||||
RuleFor(x => x.Name)
|
||||
.NotEmpty().WithMessage("Name is required.")
|
||||
.MinimumLength(3).WithMessage("Name must be at least 3 characters long.");
|
||||
}
|
||||
|
||||
private async Task<bool> BeUniqueEmail(string email, CancellationToken cancellationToken)
|
||||
{
|
||||
var pacient = await _pacientRepository.FindByEmailAsync(email);
|
||||
return pacient == null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using Application.Endpoints.Pacients.Login;
|
||||
using Application.Services.Database;
|
||||
|
||||
namespace Application.Endpoints.Pacients.ResetPassword;
|
||||
|
||||
public class PacientResetPasswordHandler
|
||||
{
|
||||
private readonly IPacientRepository _pacientRepository;
|
||||
|
||||
public PacientResetPasswordHandler(IPacientRepository pacientRepository)
|
||||
{
|
||||
_pacientRepository = pacientRepository;
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> Handle(PacientLoginDTO resetPacientDto)
|
||||
{
|
||||
var validation = new PacientResetPasswordValidation(_pacientRepository);
|
||||
var validationResult = await validation.ValidateAsync(resetPacientDto);
|
||||
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
var errorMessage = validationResult.Errors.Select(e => e.ErrorMessage).ToList();
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = string.Join(", ", errorMessage),
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
var currentPacient = await _pacientRepository.FindByEmailAsync(resetPacientDto.Email);
|
||||
|
||||
var updatedPacient = currentPacient;
|
||||
|
||||
updatedPacient.Password = resetPacientDto.Password;
|
||||
|
||||
await _pacientRepository.UpdateAsync(updatedPacient);
|
||||
|
||||
updatedPacient = await _pacientRepository.GetByIdAsync(currentPacient.Id);
|
||||
|
||||
if (updatedPacient.Password != resetPacientDto.Password)
|
||||
{
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = $"Failed to update passwor for pacient {updatedPacient.Name}",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = $"Password of pacient {updatedPacient.Name} has been reset succesfully",
|
||||
Data = updatedPacient.Email
|
||||
};
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
using Application.Services.Database;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.Pacients.Login;
|
||||
|
||||
public class PacientResetPasswordValidation : AbstractValidator<PacientLoginDTO>
|
||||
{
|
||||
private readonly IPacientRepository _pacientRepository;
|
||||
|
||||
public PacientResetPasswordValidation(IPacientRepository pacientRepository)
|
||||
{
|
||||
_pacientRepository = pacientRepository;
|
||||
|
||||
RuleFor(x => x.Email)
|
||||
.NotEmpty().WithMessage("Email is required.")
|
||||
.EmailAddress().WithMessage("Invalid email format.")
|
||||
.MustAsync(BeExistingPacient).WithMessage("Pacient 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.")
|
||||
.MustAsync((dto, password, context, cancellationToken) => BeDifferentFromOldPassword(dto.Email, password, cancellationToken))
|
||||
.WithMessage("New password cannot be the same as old password.");
|
||||
}
|
||||
|
||||
private async Task<bool> BeExistingPacient(string email, CancellationToken cancellationToken)
|
||||
{
|
||||
var pacient = await _pacientRepository.FindByEmailAsync(email);
|
||||
return pacient != null;
|
||||
}
|
||||
|
||||
private async Task<bool> BeDifferentFromOldPassword(string email, string newPassword, CancellationToken cancellationToken)
|
||||
{
|
||||
var currentPacient = await _pacientRepository.FindByEmailAsync(email);
|
||||
return !newPassword.Equals(currentPacient?.Password, StringComparison.Ordinal);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user