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,7 @@
namespace Application.Endpoints.MedicalHistories.CreateMedicalHistory;
public class CreateMedicalHistoryCommand
{
public Guid UserId { get; set; }
public byte[] Content { get; set; } = [];
}
@@ -0,0 +1,53 @@
using Application.Endpoints.MedicalHistories.ManageAuthorization;
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using Domain.Entities;
namespace Application.Endpoints.MedicalHistories.CreateMedicalHistory;
public class CreateMedicalHistoryHandler(
IMedicalHistoryRepository medicalHistoryRepository,
IPatientRepository patientRepository,
IMedicalHistoryMongoDbService medicalHistoryMongoDbService)
{
public async Task<BaseResponse> Handle(CreateMedicalHistoryCommand request, CancellationToken token)
{
var validation = new CreateMedicalHistoryValidator(patientRepository);
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 medicalHistory = new MedicalHistory();
medicalHistory.SetUserId(request.UserId);
medicalHistory.SetContent(request.Content);
var medicalHistoryId = medicalHistory.Id;
var newMedicalHistory = new MedicalHistoryAuthorisationModel
{
Id = medicalHistoryId.ToString(),
Authorisation = new List<string>()
};
await medicalHistoryMongoDbService.AddAsync(newMedicalHistory, token);
await medicalHistoryRepository.AddAsync(medicalHistory, token);
return new BaseResponse
{
StatusCode = HttpStatusCodes.Created,
Message = "Medical history record registered successfully",
Data = medicalHistory
};
}
}
@@ -1,13 +1,13 @@
using Application.Services.Database.PostgreSQL;
using FluentValidation;
namespace Application.Endpoints.MedicalHistories.FileManagement;
namespace Application.Endpoints.MedicalHistories.CreateMedicalHistory;
public class MedicalHistoryCreateValidation : AbstractValidator<MedicalHistoryCreateDto>
public class CreateMedicalHistoryValidator : AbstractValidator<CreateMedicalHistoryCommand>
{
private readonly IPatientRepository _patientRepository;
public MedicalHistoryCreateValidation(IPatientRepository patientRepository)
public CreateMedicalHistoryValidator(IPatientRepository patientRepository)
{
_patientRepository = patientRepository;
@@ -20,9 +20,9 @@ public class MedicalHistoryCreateValidation : AbstractValidator<MedicalHistoryCr
.NotEmpty().WithMessage("Description is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
}
private async Task<bool> BeExistingUser(Guid userId, CancellationToken cancellationToken)
private async Task<bool> BeExistingUser(Guid userId, CancellationToken token)
{
var patient = await _patientRepository.GetByIdAsync(userId);
var patient = await _patientRepository.GetByIdAsync(userId, token);
return patient != null;
}
}
@@ -0,0 +1,33 @@
using Application.Endpoints.MedicalHistories.ManageAuthorization;
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
namespace Application.Endpoints.MedicalHistories.DeleteMedicalHistory;
public class DeleteMedicalHistoryHandler(
IMedicalHistoryRepository medicalHistoryRepository,
IMedicalHistoryMongoDbService medicalHistoryMongoDbService)
{
public async Task<BaseResponse> Handle(Guid id, CancellationToken token)
{
var medicalRecord = await medicalHistoryRepository.GetByIdAsync(id, token);
if (medicalRecord == null)
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = "Medical record is not in system.",
Data = null
};
await medicalHistoryRepository.DeleteAsync(medicalRecord, token);
await medicalHistoryMongoDbService.DeleteAsync<MedicalHistoryAuthorisationModel>(
"_id", id.ToString(), token);
return new BaseResponse
{
StatusCode = HttpStatusCodes.NoContent,
Message = null,
Data = null
};
}
}
@@ -1,7 +0,0 @@
namespace Application.Endpoints.MedicalHistories.FileManagement;
public class MedicalHistoryCreateDto
{
public Guid UserId { get; set; }
public byte[] Content { get; set; } = [];
}
@@ -1,156 +0,0 @@
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using Core.Entities;
namespace Application.Endpoints.MedicalHistories.FileManagement;
public class MedicalHistoryFileManagementHandler
{
private readonly IMedicalHistoryMongoDbService _medicalHistoryMongoDbService;
private readonly IMedicalHistoryRepository _medicalHistoryRepository;
private readonly IPatientRepository _patientRepository;
public MedicalHistoryFileManagementHandler(IMedicalHistoryRepository medicalHistoryRepository,
IPatientRepository patientRepository, IMedicalHistoryMongoDbService medicalHistoryMongoDbService)
{
_medicalHistoryRepository = medicalHistoryRepository;
_patientRepository = patientRepository;
_medicalHistoryMongoDbService = medicalHistoryMongoDbService;
}
public async Task<BaseResponse> HandleGetAll()
{
var documents = await _medicalHistoryRepository.GetAllAsync().ConfigureAwait(false);
if (documents.Any())
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = "Retrieved medical histories",
Data = documents.ToList()
};
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = "Medical histories not found",
Data = null
};
}
public async Task<BaseResponse> HandleGet(Guid id)
{
var medicalHistory = await _medicalHistoryRepository.GetByIdAsync(id).ConfigureAwait(false);
if (medicalHistory != null)
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = "Medical history successfully retrieved",
Data = medicalHistory
};
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = "Medical history not found in system.",
Data = null
};
}
public async Task<BaseResponse> HandleCreate(MedicalHistoryCreateDto medicalHistoryCreateDto)
{
var validation = new MedicalHistoryCreateValidation(_patientRepository);
var validationResult = await validation.ValidateAsync(medicalHistoryCreateDto);
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 medicalHistory = new MedicalHistory
{
UserId = medicalHistoryCreateDto.UserId,
Content = medicalHistoryCreateDto.Content
};
var medicalHistoryId = medicalHistory.Id;
var newMedicalHistory = new MedicalHistoryAuthorisationModel
{
Id = medicalHistoryId.ToString(),
Authorisation = new List<string>()
};
await _medicalHistoryMongoDbService.AddAsync(newMedicalHistory);
await _medicalHistoryRepository.AddAsync(medicalHistory);
return new BaseResponse
{
StatusCode = HttpStatusCodes.Created,
Message = "Medical history record registered successfully",
Data = medicalHistory
};
}
public async Task<BaseResponse> HandleUpdate(MedicalHistoryUpdateDto updateDto)
{
var validation = new MedicalHistoryUpdateValidation(_medicalHistoryRepository, _patientRepository);
var validationResult = await validation.ValidateAsync(updateDto);
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 medicalHistoryToUpdate = await _medicalHistoryRepository.GetByIdAsync(updateDto.Id);
medicalHistoryToUpdate.Content = updateDto.Content;
await _medicalHistoryRepository.UpdateAsync(medicalHistoryToUpdate);
return new BaseResponse
{
StatusCode = HttpStatusCodes.NoContent,
Message = "Medical record updated successfully",
Data = null
};
}
public async Task<BaseResponse> HandleDelete(Guid id)
{
var medicalRecord = await _medicalHistoryRepository.GetByIdAsync(id);
if (medicalRecord == null)
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = "Medical record is not in system.",
Data = null
};
await _medicalHistoryMongoDbService.DeleteAsync<MedicalHistoryAuthorisationModel>("_id", id.ToString());
await _medicalHistoryRepository.DeleteAsync(medicalRecord);
//TODO delete from MongoDB
return new BaseResponse
{
StatusCode = HttpStatusCodes.NoContent,
Message = null,
Data = null
};
}
}
@@ -1,7 +0,0 @@
namespace Application.Endpoints.MedicalHistories.FileManagement;
public class MedicalHistoryUpdateDto
{
public Guid Id { get; set; }
public byte[] Content { get; set; } = [];
}
@@ -1,30 +0,0 @@
using Application.Services.Database.PostgreSQL;
using FluentValidation;
namespace Application.Endpoints.MedicalHistories.FileManagement;
public class MedicalHistoryUpdateValidation : AbstractValidator<MedicalHistoryUpdateDto>
{
private readonly IMedicalHistoryRepository _medicalHistoryRepository;
private readonly IPatientRepository _patientRepository;
public MedicalHistoryUpdateValidation(IMedicalHistoryRepository medicalHistoryRepository,
IPatientRepository patientRepository)
{
_medicalHistoryRepository = medicalHistoryRepository;
_patientRepository = patientRepository;
RuleFor(x => x.Id)
.NotEmpty().WithMessage("Id is required")
.MustAsync(BeExistingMedicalHistoryRecord).WithMessage("Medical history record does not exist");
RuleFor(x => x.Content)
.NotEmpty().WithMessage("Description is required.");
}
private async Task<bool> BeExistingMedicalHistoryRecord(Guid guid, CancellationToken token)
{
var record = await _medicalHistoryRepository.GetByIdAsync(guid);
return record != null;
}
}
@@ -0,0 +1,7 @@
namespace Application.Endpoints.MedicalHistories.ManageAuthorization;
public class MedicalHistoryAuthorizationInfo
{
public Guid MedicalRecordId { get; set; } = Guid.Empty;
public Guid DoctorId { get; set; } = Guid.Empty;
}
@@ -1,7 +0,0 @@
namespace Application.Endpoints.MedicalHistories.ManageAuthorization;
public class MedicalHistoryManageAuthorizationDoctorDto
{
public Guid MedicalRecordId { get; set; }
public Guid DoctorId { get; set; }
}
@@ -3,24 +3,16 @@ using Application.Services.Database.PostgreSQL;
namespace Application.Endpoints.MedicalHistories.ManageAuthorization;
public class MedicalHistoryManageAuthorizationHandler
public class MedicalHistoryManageAuthorizationHandler(
IMedicalHistoryRepository medicalHistoryRepository,
IMedicalHistoryMongoDbService medicalHistoryMongoDbService,
IDoctorRepository doctorRepository)
{
private readonly IDoctorRepository _doctorRepository;
private readonly IMedicalHistoryMongoDbService _medicalHistoryMongoDbService;
private readonly IMedicalHistoryRepository _medicalHistoryRepository;
public MedicalHistoryManageAuthorizationHandler(IMedicalHistoryRepository medicalHistoryRepository,
IMedicalHistoryMongoDbService medicalHistoryMongoDbService, IDoctorRepository doctorRepository)
public async Task<BaseResponse> HandleGrantDoctorAccess(
MedicalHistoryAuthorizationInfo request, CancellationToken token)
{
_medicalHistoryRepository = medicalHistoryRepository;
_medicalHistoryMongoDbService = medicalHistoryMongoDbService;
_doctorRepository = doctorRepository;
}
public async Task<BaseResponse> HandleGrantDoctorAccess(MedicalHistoryManageAuthorizationDoctorDto infoDto)
{
var validation = new MedicalHistoryManageAuthorizationValidation(_medicalHistoryRepository, _doctorRepository);
var validationResult = await validation.ValidateAsync(infoDto);
var validation = new MedicalHistoryManageAuthorizationValidator(medicalHistoryRepository, doctorRepository);
var validationResult = await validation.ValidateAsync(request, token);
if (!validationResult.IsValid)
{
@@ -37,9 +29,10 @@ public class MedicalHistoryManageAuthorizationHandler
}
var criteria = new List<(string, string)>();
criteria.Add(("_id", infoDto.MedicalRecordId.ToString()));
criteria.Add(("_id", request.MedicalRecordId.ToString()));
var documents = await _medicalHistoryMongoDbService.FindAsync<MedicalHistoryAuthorisationModel>(criteria);
var documents =
await medicalHistoryMongoDbService.FindAsync<MedicalHistoryAuthorisationModel>(criteria, token);
if (!documents.Any())
return new BaseResponse
{
@@ -49,7 +42,7 @@ public class MedicalHistoryManageAuthorizationHandler
};
var authorisations = documents[0].Authorisation;
if (authorisations.Contains(infoDto.ToString()))
if (authorisations.Contains(request.ToString()))
return new BaseResponse
{
StatusCode = HttpStatusCodes.Conflict,
@@ -57,14 +50,15 @@ public class MedicalHistoryManageAuthorizationHandler
Data = null
};
authorisations.Add(infoDto.DoctorId.ToString());
authorisations.Add(request.DoctorId.ToString());
var authorizationModel = new MedicalHistoryAuthorisationModel
{
Id = infoDto.MedicalRecordId.ToString(),
Id = request.MedicalRecordId.ToString(),
Authorisation = authorisations
};
await _medicalHistoryMongoDbService.ModifyAsync("_id", infoDto.MedicalRecordId.ToString(), authorizationModel);
await medicalHistoryMongoDbService.ModifyAsync(
"_id", request.MedicalRecordId.ToString(), authorizationModel, token);
return new BaseResponse
{
@@ -74,10 +68,11 @@ public class MedicalHistoryManageAuthorizationHandler
};
}
public async Task<BaseResponse> HandleRevokeDoctorAccess(MedicalHistoryManageAuthorizationDoctorDto infoDto)
public async Task<BaseResponse> HandleRevokeDoctorAccess(
MedicalHistoryAuthorizationInfo request, CancellationToken token)
{
var validation = new MedicalHistoryManageAuthorizationValidation(_medicalHistoryRepository, _doctorRepository);
var validationResult = await validation.ValidateAsync(infoDto);
var validation = new MedicalHistoryManageAuthorizationValidator(medicalHistoryRepository, doctorRepository);
var validationResult = await validation.ValidateAsync(request, token);
if (!validationResult.IsValid)
{
@@ -94,9 +89,10 @@ public class MedicalHistoryManageAuthorizationHandler
}
var criteria = new List<(string, string)>();
criteria.Add(("_id", infoDto.MedicalRecordId.ToString()));
criteria.Add(("_id", request.MedicalRecordId.ToString()));
var documents = await _medicalHistoryMongoDbService.FindAsync<MedicalHistoryAuthorisationModel>(criteria);
var documents = await medicalHistoryMongoDbService
.FindAsync<MedicalHistoryAuthorisationModel>(criteria, token);
if (!documents.Any())
return new BaseResponse
{
@@ -106,7 +102,7 @@ public class MedicalHistoryManageAuthorizationHandler
};
var authorisations = documents[0].Authorisation;
if (!authorisations.Contains(infoDto.DoctorId.ToString()))
if (!authorisations.Contains(request.DoctorId.ToString()))
return new BaseResponse
{
StatusCode = HttpStatusCodes.Conflict,
@@ -114,14 +110,15 @@ public class MedicalHistoryManageAuthorizationHandler
Data = null
};
authorisations.Remove(infoDto.DoctorId.ToString());
authorisations.Remove(request.DoctorId.ToString());
var authorizationModel = new MedicalHistoryAuthorisationModel
{
Id = infoDto.MedicalRecordId.ToString(),
Id = request.MedicalRecordId.ToString(),
Authorisation = authorisations
};
await _medicalHistoryMongoDbService.ModifyAsync("_id", infoDto.MedicalRecordId.ToString(), authorizationModel);
await medicalHistoryMongoDbService.ModifyAsync(
"_id", request.MedicalRecordId.ToString(), authorizationModel, token);
return new BaseResponse
{
@@ -130,4 +127,53 @@ public class MedicalHistoryManageAuthorizationHandler
Data = null
};
}
public async Task<BaseResponse> HandleCheckAccessToMedicalHistory(
MedicalHistoryAuthorizationInfo request, CancellationToken token)
{
var validation = new MedicalHistoryManageAuthorizationValidator(medicalHistoryRepository, doctorRepository);
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 criteria = new List<(string, string)>();
criteria.Add(("_id", request.MedicalRecordId.ToString()));
var documents = await medicalHistoryMongoDbService
.FindAsync<MedicalHistoryAuthorisationModel>(criteria, token);
if (!documents.Any())
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = "Access to medical history not found.",
Data = null
};
var authorisations = documents[0].Authorisation;
if (!authorisations.Contains(request.DoctorId.ToString()))
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = "Access to medical history is revoked.",
Data = null
};
return new BaseResponse()
{
StatusCode = HttpStatusCodes.OK,
Message = "Access to medical history is granted",
Data = null
};
}
}
@@ -3,12 +3,12 @@ using FluentValidation;
namespace Application.Endpoints.MedicalHistories.ManageAuthorization;
public class MedicalHistoryManageAuthorizationValidation : AbstractValidator<MedicalHistoryManageAuthorizationDoctorDto>
public class MedicalHistoryManageAuthorizationValidator : AbstractValidator<MedicalHistoryAuthorizationInfo>
{
private readonly IDoctorRepository _doctorRepository;
private readonly IMedicalHistoryRepository _medicalHistoryRepository;
public MedicalHistoryManageAuthorizationValidation(IMedicalHistoryRepository medicalHistoryRepository,
public MedicalHistoryManageAuthorizationValidator(IMedicalHistoryRepository medicalHistoryRepository,
IDoctorRepository doctorRepository)
{
RuleFor(x => x.MedicalRecordId)
@@ -27,13 +27,13 @@ public class MedicalHistoryManageAuthorizationValidation : AbstractValidator<Med
private async Task<bool> BeExistingMedicalHistoryRecord(Guid guid, CancellationToken token)
{
var record = await _medicalHistoryRepository.GetByIdAsync(guid);
var record = await _medicalHistoryRepository.GetByIdAsync(guid, token);
return record != null;
}
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;
}
}
@@ -2,6 +2,6 @@
public class MedicalHistoryAuthorisationModel
{
public string Id { get; set; }
public List<string> Authorisation { get; set; } = new();
public string Id { get; set; } = string.Empty;
public List<string> Authorisation { get; set; } = [];
}
@@ -1,7 +0,0 @@
namespace Application.Endpoints.MedicalHistories;
public class MedicalHistoryCreateDto
{
public Guid UserId { get; set; }
public byte[] Content { get; set; } = [];
}
@@ -1,28 +0,0 @@
using Application.Services.Database.PostgreSQL;
using FluentValidation;
namespace Application.Endpoints.MedicalHistories;
public class MedicalHistoryCreateValidation : AbstractValidator<MedicalHistoryCreateDto>
{
private readonly IPatientRepository _patientRepository;
public MedicalHistoryCreateValidation(IPatientRepository patientRepository)
{
_patientRepository = patientRepository;
RuleFor(x => x.UserId)
.NotEmpty().WithMessage("Patient is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString())
.MustAsync(BeExistingUser).WithMessage("Specified patient doesn't exist.")
.WithErrorCode(HttpStatusCodes.NotFound.ToString());
RuleFor(x => x.Content)
.NotEmpty().WithMessage("Description is required.").WithErrorCode(HttpStatusCodes.BadRequest.ToString());
}
private async Task<bool> BeExistingUser(Guid userId, CancellationToken cancellationToken)
{
var patient = await _patientRepository.GetByIdAsync(userId);
return patient != null;
}
}
@@ -1,156 +0,0 @@
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
using Core.Entities;
namespace Application.Endpoints.MedicalHistories;
public class MedicalHistoryHandler
{
private readonly IMedicalHistoryMongoDbService _medicalHistoryMongoDbService;
private readonly IMedicalHistoryRepository _medicalHistoryRepository;
private readonly IPatientRepository _patientRepository;
public MedicalHistoryHandler(IMedicalHistoryRepository medicalHistoryRepository,
IPatientRepository patientRepository, IMedicalHistoryMongoDbService medicalHistoryMongoDbService)
{
_medicalHistoryRepository = medicalHistoryRepository;
_patientRepository = patientRepository;
_medicalHistoryMongoDbService = medicalHistoryMongoDbService;
}
public async Task<BaseResponse> HandleGetAll()
{
var documents = await _medicalHistoryRepository.GetAllAsync().ConfigureAwait(false);
if (documents.Any())
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = "Retrieved medical histories",
Data = documents.ToList()
};
return new BaseResponse
{
StatusCode = HttpStatusCodes.NoContent,
Message = "Medical histories not found",
Data = null
};
}
public async Task<BaseResponse> HandleGet(Guid id)
{
var medicalHistory = await _medicalHistoryRepository.GetByIdAsync(id).ConfigureAwait(false);
if (medicalHistory != null)
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = "Medical history successfully retrieved",
Data = medicalHistory
};
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = "Medical history not found in system.",
Data = null
};
}
public async Task<BaseResponse> HandleCreate(MedicalHistoryCreateDto medicalHistoryCreateDto)
{
var validation = new MedicalHistoryCreateValidation(_patientRepository);
var validationResult = await validation.ValidateAsync(medicalHistoryCreateDto);
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 medicalHistory = new MedicalHistory
{
UserId = medicalHistoryCreateDto.UserId,
Content = medicalHistoryCreateDto.Content
};
var medicalHistoryId = medicalHistory.Id;
var newMedicalHistory = new MedicalHistoryAuthorisationModel
{
Id = medicalHistoryId.ToString(),
Authorisation = new List<string>()
};
await _medicalHistoryMongoDbService.AddAsync(newMedicalHistory);
await _medicalHistoryRepository.AddAsync(medicalHistory);
return new BaseResponse
{
StatusCode = HttpStatusCodes.Created,
Message = "Medical history record registered successfully",
Data = medicalHistory
};
}
public async Task<BaseResponse> HandleUpdate(MedicalHistoryUpdateDto updateDto)
{
var validation = new MedicalHistoryUpdateValidation(_medicalHistoryRepository, _patientRepository);
var validationResult = await validation.ValidateAsync(updateDto);
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 medicalHistoryToUpdate = await _medicalHistoryRepository.GetByIdAsync(updateDto.Id);
medicalHistoryToUpdate.Content = updateDto.Content;
await _medicalHistoryRepository.UpdateAsync(medicalHistoryToUpdate);
return new BaseResponse
{
StatusCode = HttpStatusCodes.NoContent,
Message = "Medical record updated successfully",
Data = null
};
}
public async Task<BaseResponse> HandleDelete(Guid id)
{
var medicalRecord = await _medicalHistoryRepository.GetByIdAsync(id);
if (medicalRecord == null)
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = "Medical record is not in system.",
Data = null
};
await _medicalHistoryMongoDbService.DeleteAsync<MedicalHistoryAuthorisationModel>("_id", id.ToString());
await _medicalHistoryRepository.DeleteAsync(medicalRecord);
//TODO delete from MongoDB
return new BaseResponse
{
StatusCode = HttpStatusCodes.NoContent,
Message = null,
Data = null
};
}
}
@@ -1,7 +0,0 @@
namespace Application.Endpoints.MedicalHistories;
public class MedicalHistoryUpdateDto
{
public Guid Id { get; set; }
public byte[] Content { get; set; } = [];
}
@@ -0,0 +1,7 @@
namespace Application.Endpoints.MedicalHistories.ModifyMedicalHistory;
public class ModifyMedicalHistoryCommand
{
public Guid Id { get; set; }
public byte[] Content { get; set; } = [];
}
@@ -0,0 +1,38 @@
using Application.Services.Database.MongoDB;
using Application.Services.Database.PostgreSQL;
namespace Application.Endpoints.MedicalHistories.ModifyMedicalHistory;
public class ModifyMedicalHistoryHandler(IMedicalHistoryRepository medicalHistoryRepository)
{
public async Task<BaseResponse> Handle(ModifyMedicalHistoryCommand request, CancellationToken token)
{
var validation = new ModifyMedicalHistoryValidator(medicalHistoryRepository);
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 newMedicalHistory = await medicalHistoryRepository.GetByIdAsync(request.Id, token);
newMedicalHistory.SetContent(request.Content);
await medicalHistoryRepository.UpdateAsync(newMedicalHistory, token);
return new BaseResponse
{
StatusCode = HttpStatusCodes.NoContent,
Message = "Medical record updated successfully",
Data = null
};
}
}
@@ -1,18 +1,15 @@
using Application.Services.Database.PostgreSQL;
using FluentValidation;
namespace Application.Endpoints.MedicalHistories;
namespace Application.Endpoints.MedicalHistories.ModifyMedicalHistory;
public class MedicalHistoryUpdateValidation : AbstractValidator<MedicalHistoryUpdateDto>
public class ModifyMedicalHistoryValidator : AbstractValidator<ModifyMedicalHistoryCommand>
{
private readonly IMedicalHistoryRepository _medicalHistoryRepository;
private readonly IPatientRepository _patientRepository;
public MedicalHistoryUpdateValidation(IMedicalHistoryRepository medicalHistoryRepository,
IPatientRepository patientRepository)
public ModifyMedicalHistoryValidator(IMedicalHistoryRepository medicalHistoryRepository)
{
_medicalHistoryRepository = medicalHistoryRepository;
_patientRepository = patientRepository;
RuleFor(x => x.Id)
.NotEmpty().WithMessage("Id is required")
@@ -22,9 +19,9 @@ public class MedicalHistoryUpdateValidation : AbstractValidator<MedicalHistoryUp
.NotEmpty().WithMessage("Description is required.");
}
private async Task<bool> BeExistingMedicalHistoryRecord(Guid guid, CancellationToken token)
private async Task<bool> BeExistingMedicalHistoryRecord(Guid id, CancellationToken token)
{
var record = await _medicalHistoryRepository.GetByIdAsync(guid);
var record = await _medicalHistoryRepository.GetByIdAsync(id, token);
return record != null;
}
}
@@ -0,0 +1,63 @@
using Application.Services.Database.PostgreSQL;
namespace Application.Endpoints.MedicalHistories.QuerriesMedicalHistories;
public class QuerriesMedicalHistoriesHandler(IMedicalHistoryRepository medicalHistoryRepository)
{
public async Task<BaseResponse> HandleGetAll(CancellationToken token)
{
var documents = await medicalHistoryRepository.GetAllAsync(token);
if (documents.Any())
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = "Retrieved medical histories",
Data = documents.ToList()
};
return new BaseResponse
{
StatusCode = HttpStatusCodes.NoContent,
Message = "Medical histories not found",
Data = null
};
}
public async Task<BaseResponse> HandleGet(Guid id, CancellationToken token)
{
var medicalHistory = await medicalHistoryRepository.GetByIdAsync(id, token);
if (medicalHistory != null)
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = "Medical history successfully retrieved",
Data = medicalHistory
};
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = "Medical history not found in system.",
Data = null
};
}
public async Task<BaseResponse> HandleGetByPatientId(Guid patientId, CancellationToken token)
{
var medicalHistory = await medicalHistoryRepository.GetByPatientIdAsync(patientId, token);
if (medicalHistory != null)
return new BaseResponse
{
StatusCode = HttpStatusCodes.OK,
Message = "Medical history successfully retrieved",
Data = medicalHistory
};
return new BaseResponse
{
StatusCode = HttpStatusCodes.NotFound,
Message = "Medical history not found in system.",
Data = null
};
}
}