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:
@@ -1,9 +1,8 @@
|
||||
namespace Application.Endpoints
|
||||
namespace Application.Endpoints;
|
||||
|
||||
public class BaseResponse
|
||||
{
|
||||
public class BaseResponse
|
||||
{
|
||||
public bool Success { get; set; }
|
||||
public string? Message { get; set; }
|
||||
public object? Data { get; set; }
|
||||
}
|
||||
public bool Success { get; set; }
|
||||
public string? Message { get; set; }
|
||||
public object? Data { get; set; }
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
namespace Application.Endpoints.Doctors.Login
|
||||
namespace Application.Endpoints.Doctors.Login;
|
||||
|
||||
public class DoctorLoginDTO
|
||||
{
|
||||
public class DoctorLoginDTO
|
||||
{
|
||||
public string? Email { get; set; }
|
||||
public string? Password { get; set; }
|
||||
}
|
||||
public string? Email { get; set; }
|
||||
public string? Password { get; set; }
|
||||
}
|
||||
|
||||
@@ -1,38 +1,37 @@
|
||||
using Application.Services.Database;
|
||||
|
||||
namespace Application.Endpoints.Doctors.Login
|
||||
namespace Application.Endpoints.Doctors.Login;
|
||||
|
||||
public class DoctorLoginHandler
|
||||
{
|
||||
public class DoctorLoginHandler
|
||||
private readonly IDoctorRepository _database;
|
||||
|
||||
public DoctorLoginHandler(IDoctorRepository database)
|
||||
{
|
||||
private readonly IDoctorRepository _database;
|
||||
_database = database;
|
||||
}
|
||||
|
||||
public DoctorLoginHandler(IDoctorRepository database)
|
||||
public async Task<BaseResponse> Handle(DoctorLoginDTO loginDTO)
|
||||
{
|
||||
var validation = new DoctorLoginValidation(_database);
|
||||
var validationResult = await validation.ValidateAsync(loginDTO);
|
||||
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
_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
|
||||
};
|
||||
}
|
||||
|
||||
var errorMessage = validationResult.Errors.FirstOrDefault()?.ErrorMessage;
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = "Authentication successful",
|
||||
Success = false,
|
||||
Message = errorMessage,
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = "Authentication successful",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,29 +1,29 @@
|
||||
using Application.Services.Database;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.Doctors.Login
|
||||
namespace Application.Endpoints.Doctors.Login;
|
||||
|
||||
public class DoctorLoginValidation : AbstractValidator<DoctorLoginDTO>
|
||||
{
|
||||
public class DoctorLoginValidation : AbstractValidator<DoctorLoginDTO>
|
||||
private readonly IDoctorRepository _doctorRepository;
|
||||
|
||||
public DoctorLoginValidation(IDoctorRepository doctorRepository)
|
||||
{
|
||||
private readonly IDoctorRepository _doctorRepository;
|
||||
_doctorRepository = 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.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.");
|
||||
}
|
||||
|
||||
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, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _doctorRepository.IsDoctorExisting(email);
|
||||
}
|
||||
private async Task<bool> BeExistingDoctor(string email, CancellationToken cancellationToken)
|
||||
{
|
||||
var doctor = await _doctorRepository.FindByEmailAsync(email);
|
||||
return doctor != null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Application.Endpoints.Doctors.Profile;
|
||||
|
||||
public class DoctorProfileDTO
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string Password { get; set; }
|
||||
public string Description { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
using Application.Services.Database;
|
||||
|
||||
namespace Application.Endpoints.Doctors.Profile;
|
||||
|
||||
public class DoctorProfileHandler
|
||||
{
|
||||
private readonly IDoctorRepository _doctorRepository;
|
||||
|
||||
public DoctorProfileHandler(IDoctorRepository doctorRepository)
|
||||
{
|
||||
_doctorRepository = doctorRepository;
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> HandleGet(Guid id)
|
||||
{
|
||||
var doctor = await _doctorRepository.GetByIdAsync(id).ConfigureAwait(false);
|
||||
if (doctor != null)
|
||||
{
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = $"Retrieved doctor with id: {id}",
|
||||
Data = doctor
|
||||
};
|
||||
}
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = $"Doctor with id: {id} not found",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> HandleGetAll()
|
||||
{
|
||||
var doctors = await _doctorRepository.GetAllAsync().ConfigureAwait(false);
|
||||
if (doctors.Any())
|
||||
{
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = "Retrieved doctors",
|
||||
Data = doctors.ToList()
|
||||
};
|
||||
}
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = "Doctors not found",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> HandleUpdate(Guid id, DoctorProfileDTO updateDto)
|
||||
{
|
||||
var validation = new DoctorProfileValidation(_doctorRepository);
|
||||
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 doctorToUpdate = await _doctorRepository.GetByIdAsync(id);
|
||||
|
||||
if (doctorToUpdate == null)
|
||||
{
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = "Doctor not found for given Id",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
doctorToUpdate.Email = updateDto.Email;
|
||||
doctorToUpdate.Password = updateDto.Password;
|
||||
doctorToUpdate.Name = updateDto.Name;
|
||||
doctorToUpdate.Description = updateDto.Description;
|
||||
|
||||
await _doctorRepository.UpdateAsync(doctorToUpdate);
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = "Doctor updated successfully",
|
||||
Data = doctorToUpdate
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> HandleDelete(Guid id)
|
||||
{
|
||||
var doctorToDelete = await _doctorRepository.GetByIdAsync(id);
|
||||
if (doctorToDelete == null)
|
||||
{
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = $"Doctor with id: {id} does not exist",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
await _doctorRepository.DeleteAsync(doctorToDelete);
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = $"Doctor with id: {id} was succesfully deleted",
|
||||
Data = doctorToDelete
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using Application.Services.Database;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.Doctors.Profile;
|
||||
|
||||
public class DoctorProfileValidation : AbstractValidator<DoctorProfileDTO>
|
||||
{
|
||||
private readonly IDoctorRepository _doctorRepository;
|
||||
|
||||
public DoctorProfileValidation(IDoctorRepository doctorRepository)
|
||||
{
|
||||
_doctorRepository = doctorRepository;
|
||||
|
||||
RuleFor(x => x.Email)
|
||||
.NotEmpty().WithMessage("Email is required.")
|
||||
.EmailAddress().WithMessage("Invalid email format.")
|
||||
.MustAsync(BeUniqueEmail).WithMessage("Email in use by another doctor.");
|
||||
|
||||
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 doctor = await _doctorRepository.FindByEmailAsync(email);
|
||||
if (doctor != null)
|
||||
{
|
||||
return doctor.Email.Equals(email, StringComparison.OrdinalIgnoreCase);
|
||||
}
|
||||
return doctor == null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace Application.Endpoints.Doctors.Login;
|
||||
|
||||
public class DoctorRegistrationDto
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string Password { get; set; }
|
||||
public string Description { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using Application.Endpoints.Doctors.Login;
|
||||
using Application.Services.Database;
|
||||
using Core.Entities;
|
||||
|
||||
namespace Application.Endpoints.Doctors.Registration;
|
||||
|
||||
public class DoctorRegistrationHandler
|
||||
{
|
||||
private readonly IDoctorRepository _doctorRepository;
|
||||
|
||||
public DoctorRegistrationHandler(IDoctorRepository doctorRepository)
|
||||
{
|
||||
_doctorRepository = doctorRepository;
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> Handle(DoctorRegistrationDto registrationDTO)
|
||||
{
|
||||
var validation = new DoctorRegistrationValidation(_doctorRepository);
|
||||
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 doctor = new Doctor
|
||||
{
|
||||
Email = registrationDTO.Email,
|
||||
Password = registrationDTO.Password,
|
||||
Name = registrationDTO.Name,
|
||||
Description = registrationDTO.Description
|
||||
};
|
||||
|
||||
await _doctorRepository.AddAsync(doctor);
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = "Doctor registered successfully",
|
||||
Data = doctor // Be careful with sending sensitive data like Passwords
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using Application.Endpoints.Doctors.Login;
|
||||
using Application.Services.Database;
|
||||
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.")
|
||||
.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.");
|
||||
|
||||
RuleFor(x => x.Description)
|
||||
.MaximumLength(3000).WithMessage("Description must not exceed 3000 characters.");
|
||||
}
|
||||
|
||||
private async Task<bool> BeUniqueEmail(string email, CancellationToken cancellationToken)
|
||||
{
|
||||
var doctor = await _doctorRepository.FindByEmailAsync(email);
|
||||
return doctor == null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using Application.Endpoints.Doctors.Login;
|
||||
using Application.Services.Database;
|
||||
|
||||
namespace Application.Endpoints.Doctors.ResetPassword;
|
||||
|
||||
public class DoctorResetPasswordHandler
|
||||
{
|
||||
private readonly IDoctorRepository _doctorRepository;
|
||||
|
||||
public DoctorResetPasswordHandler(IDoctorRepository doctorRepository)
|
||||
{
|
||||
_doctorRepository = doctorRepository;
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> Handle(DoctorLoginDTO resetDoctorDto)
|
||||
{
|
||||
var validation = new DoctorResetPasswordValidation(_doctorRepository);
|
||||
var validationResult = await validation.ValidateAsync(resetDoctorDto);
|
||||
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
var errorMessage = validationResult.Errors.Select(e => e.ErrorMessage).ToList();
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = string.Join(", ", errorMessage),
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
var currentDoctor = await _doctorRepository.FindByEmailAsync(resetDoctorDto.Email);
|
||||
|
||||
var updatedDoctor = currentDoctor;
|
||||
|
||||
updatedDoctor.Password = resetDoctorDto.Password;
|
||||
|
||||
await _doctorRepository.UpdateAsync(updatedDoctor);
|
||||
|
||||
updatedDoctor = await _doctorRepository.GetByIdAsync(currentDoctor.Id);
|
||||
|
||||
if (updatedDoctor.Password != resetDoctorDto.Password)
|
||||
{
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = $"Failed to update passwor for doctor {updatedDoctor.Name}",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = $"Password of doctor {updatedDoctor.Name} has been reset succesfully",
|
||||
Data = updatedDoctor.Email
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using Application.Services.Database;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.Doctors.Login;
|
||||
|
||||
public class DoctorResetPasswordValidation : AbstractValidator<DoctorLoginDTO>
|
||||
{
|
||||
private readonly IDoctorRepository _doctorRepository;
|
||||
|
||||
public DoctorResetPasswordValidation(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.")
|
||||
.MustAsync((dto, password, context, cancellationToken) => BeDifferentFromOldPassword(dto.Email, password, cancellationToken))
|
||||
.WithMessage("New password cannot be the same as old password.");
|
||||
}
|
||||
|
||||
private async Task<bool> BeExistingDoctor(string email, CancellationToken cancellationToken)
|
||||
{
|
||||
var doctor = await _doctorRepository.FindByEmailAsync(email);
|
||||
return doctor != null;
|
||||
}
|
||||
|
||||
private async Task<bool> BeDifferentFromOldPassword(string email, string newPassword, CancellationToken cancellationToken)
|
||||
{
|
||||
var currentDoctor = await _doctorRepository.FindByEmailAsync(email);
|
||||
return !newPassword.Equals(currentDoctor?.Password, StringComparison.Ordinal);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using Application.Services.Database;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.MedicalHistories;
|
||||
|
||||
public class MedicalHistoryCreateValidation : AbstractValidator<MedicalHistoryDTO>
|
||||
{
|
||||
private readonly IPacientRepository _pacientRepository;
|
||||
|
||||
public MedicalHistoryCreateValidation(IPacientRepository pacientRepository)
|
||||
{
|
||||
_pacientRepository = pacientRepository;
|
||||
|
||||
RuleFor(x => x.UserId)
|
||||
.NotEmpty().WithMessage("Pacient is required.")
|
||||
.MustAsync(BeExistingUser).WithMessage("Specified pacient id doesn't exist.");
|
||||
|
||||
RuleFor(x => x.Description)
|
||||
.NotEmpty().WithMessage("Description is required.");
|
||||
}
|
||||
|
||||
private async Task<bool> BeExistingUser(Guid userId, CancellationToken cancellationToken)
|
||||
{
|
||||
var pacient = await _pacientRepository.GetByIdAsync(userId);
|
||||
return pacient != null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Application.Endpoints.MedicalHistories;
|
||||
|
||||
public class MedicalHistoryDTO
|
||||
{
|
||||
public Guid UserId { get; set; }
|
||||
public byte[] Description { get; set; } = [];
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
using Application.Services.Database;
|
||||
using Core.Entities;
|
||||
|
||||
namespace Application.Endpoints.MedicalHistories;
|
||||
|
||||
public class MedicalHistoryHandler
|
||||
{
|
||||
private readonly IMedicalHistoryRepository _medicalHistoryRepository;
|
||||
private readonly IPacientRepository _pacientRepository;
|
||||
|
||||
public MedicalHistoryHandler(IMedicalHistoryRepository medicalHistoryRepository, IPacientRepository pacientRepository)
|
||||
{
|
||||
_medicalHistoryRepository = medicalHistoryRepository;
|
||||
_pacientRepository = pacientRepository;
|
||||
}
|
||||
public async Task<BaseResponse> HandleGet(Guid id)
|
||||
{
|
||||
var medicalHistory = await _medicalHistoryRepository.GetByIdAsync(id).ConfigureAwait(false);
|
||||
if (medicalHistory != null)
|
||||
{
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = $"Retrieved Medical History with id: {id}",
|
||||
Data = medicalHistory
|
||||
};
|
||||
}
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = $"Medical History with id: {id} not found",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> HandleCreate(Guid userId, byte[] description)
|
||||
{
|
||||
var validation = new MedicalHistoryCreateValidation(_pacientRepository);
|
||||
var validationResult = await validation.ValidateAsync(new MedicalHistoryDTO { UserId = userId, Description = description});
|
||||
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
var errorMessage = validationResult.Errors.Select(e => e.ErrorMessage).ToList();
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = string.Join(", ", errorMessage),
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
var medicalHistory = new MedicalHistory
|
||||
{
|
||||
UserId = userId,
|
||||
Description = description
|
||||
};
|
||||
|
||||
await _medicalHistoryRepository.AddAsync(medicalHistory);
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = "Medical history record registered successfully",
|
||||
Data = medicalHistory
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<BaseResponse> HandleUpdate(Guid id, MedicalHistoryDTO updateDto)
|
||||
{
|
||||
var validation = new MedicalHistoryUpdateValidation(_medicalHistoryRepository, _pacientRepository);
|
||||
var validationResult = await validation.ValidateAsync(new MedicalHistoryUpdateDTO { Id = id, UserId = updateDto.UserId, Description = updateDto.Description});
|
||||
|
||||
if (!validationResult.IsValid)
|
||||
{
|
||||
var errorMessage = validationResult.Errors.Select(e => e.ErrorMessage).ToList();
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = string.Join(", ", errorMessage),
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
var medicalHistoryToUpdate = await _medicalHistoryRepository.GetByIdAsync(id);
|
||||
|
||||
if (medicalHistoryToUpdate == null)
|
||||
{
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = false,
|
||||
Message = "Medical history not found for given Id",
|
||||
Data = null
|
||||
};
|
||||
}
|
||||
|
||||
medicalHistoryToUpdate.UserId = updateDto.UserId;
|
||||
medicalHistoryToUpdate.Description = updateDto.Description;
|
||||
|
||||
await _medicalHistoryRepository.UpdateAsync(medicalHistoryToUpdate);
|
||||
|
||||
return new BaseResponse
|
||||
{
|
||||
Success = true,
|
||||
Message = "Pacient updated successfully",
|
||||
Data = medicalHistoryToUpdate
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Application.Endpoints.MedicalHistories;
|
||||
|
||||
public class MedicalHistoryUpdateDTO
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public Guid UserId { get; set; }
|
||||
public byte[] Description { get; set; } = [];
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using Application.Services.Database;
|
||||
using FluentValidation;
|
||||
|
||||
namespace Application.Endpoints.MedicalHistories;
|
||||
|
||||
public class MedicalHistoryUpdateValidation : AbstractValidator<MedicalHistoryUpdateDTO>
|
||||
{
|
||||
private readonly IMedicalHistoryRepository _medicalHistoryRepository;
|
||||
private readonly IPacientRepository _pacientRepository;
|
||||
|
||||
public MedicalHistoryUpdateValidation(IMedicalHistoryRepository medicalHistoryRepository, IPacientRepository pacientRepository)
|
||||
{
|
||||
_medicalHistoryRepository = medicalHistoryRepository;
|
||||
_pacientRepository = pacientRepository;
|
||||
|
||||
RuleFor(x => x.Id)
|
||||
.NotEmpty().WithMessage("Id is required")
|
||||
.MustAsync(BeExistingMedicalHistoryRecord).WithMessage("Medical hostory record does not exist");
|
||||
|
||||
RuleFor(x => x.UserId)
|
||||
.NotEmpty().WithMessage("Pacient is required.")
|
||||
.MustAsync(BeExistingUser).WithMessage("Specified pacient id doesn't exist.");
|
||||
|
||||
RuleFor(x => x.Description)
|
||||
.NotEmpty().WithMessage("Description is required.");
|
||||
}
|
||||
|
||||
private async Task<bool> BeExistingMedicalHistoryRecord(Guid guid, CancellationToken token)
|
||||
{
|
||||
var record = await _medicalHistoryRepository.GetByIdAsync(guid);
|
||||
return record != null;
|
||||
}
|
||||
|
||||
private async Task<bool> BeExistingUser(Guid userId, CancellationToken cancellationToken)
|
||||
{
|
||||
var pacient = await _pacientRepository.GetByIdAsync(userId);
|
||||
return pacient != null;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
namespace Application.Services.Database
|
||||
namespace Application.Services.Database;
|
||||
|
||||
public interface IConversationRepository
|
||||
{
|
||||
public interface IConversationRepository
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,18 @@
|
||||
namespace Application.Services.Database
|
||||
using Core.Entities;
|
||||
|
||||
namespace Application.Services.Database;
|
||||
|
||||
public interface IDoctorRepository
|
||||
{
|
||||
public interface IDoctorRepository
|
||||
{
|
||||
Task<bool> IsDoctorExisting(string email);
|
||||
}
|
||||
Task AddAsync(Doctor doctor);
|
||||
|
||||
Task<Doctor> GetByIdAsync(Guid id);
|
||||
|
||||
Task<Doctor?> FindByEmailAsync(string email);
|
||||
|
||||
Task UpdateAsync(Doctor doctor);
|
||||
|
||||
Task DeleteAsync(Doctor doctor);
|
||||
|
||||
Task<IEnumerable<Doctor>> GetAllAsync();
|
||||
}
|
||||
@@ -1,6 +1,15 @@
|
||||
namespace Application.Services.Database
|
||||
using Core.Entities;
|
||||
|
||||
namespace Application.Services.Database;
|
||||
|
||||
public interface IMedicalHistoryRepository
|
||||
{
|
||||
public interface IMedicalHistoryRepository
|
||||
{
|
||||
}
|
||||
Task<MedicalHistory> GetByIdAsync(Guid id);
|
||||
|
||||
Task<MedicalHistory?> GetByUserIdAsync(Guid userId);
|
||||
|
||||
Task AddAsync(MedicalHistory medicalHistory);
|
||||
|
||||
Task UpdateAsync(MedicalHistory medicalHistory);
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,18 @@
|
||||
namespace Application.Services.Database
|
||||
using Core.Entities;
|
||||
|
||||
namespace Application.Services.Database;
|
||||
|
||||
public interface IPacientRepository
|
||||
{
|
||||
public interface IPacientRepository
|
||||
{
|
||||
}
|
||||
Task AddAsync(Pacient pacient);
|
||||
|
||||
Task<Pacient> GetByIdAsync(Guid id);
|
||||
|
||||
Task<Pacient?> FindByEmailAsync(string email);
|
||||
|
||||
Task UpdateAsync(Pacient doctor);
|
||||
|
||||
Task DeleteAsync(Pacient doctor);
|
||||
|
||||
Task<IEnumerable<Pacient>> GetAllAsync();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user