Compare commits

...

3 Commits
main ... dev

5 changed files with 123 additions and 0 deletions

View File

@ -0,0 +1,92 @@
using Microsoft.AspNetCore.Mvc;
using SimpleLIS.DTO;
using SimpleLIS.Services;
using SimpleLIS.Models;
using AutoMapper;
using System.Text.Json;
namespace SimpleLIS.Controllers;
[ApiController]
[Route("api/hl7")]
public class HL7Controller : ControllerBase
{
private readonly MessageService msgService;
private readonly PatientService ptService;
private readonly IMapper _mapper;
public HL7Controller(
MessageService messageService,
PatientService patientService,
IMapper mapper)
{
msgService = messageService;
ptService = patientService;
_mapper = mapper;
}
[HttpPost("process")]
public async Task<IActionResult> ProcessHL7Message([FromBody] JsonElement payload)
{
try
{
var patient = new Patient
{
HL7PatientId = payload.GetProperty("hl7PatientId").GetString(),
LastName = payload.GetProperty("lastName").GetString(),
FirstName = payload.GetProperty("firstName").GetString(),
DateOfBirth = payload.GetProperty("dateOfBirth").GetDateTime(),
Gender = payload.GetProperty("gender").GetString(),
Messages = new List<Message>()
};
var existingPatient = await ptService.GetPatientByNameAndDOB(
patient.FirstName!,
patient.LastName!,
patient.DateOfBirth
);
patient = existingPatient ?? await ptService.CreatePatientAsync(patient);
var message = new Message
{
ControlId = payload.GetProperty("controlId").GetString() ?? string.Empty,
MessageType = payload.GetProperty("messageType").GetString()?? string.Empty,
SendingApp = payload.GetProperty("sendingApp").GetString(),
SendingFacility = payload.GetProperty("sendingFacility").GetString(),
ReceivingApp = payload.GetProperty("receivingApp").GetString(),
ReceivingFacility = payload.GetProperty("receivingFacility").GetString(),
Timestamp = payload.GetProperty("timestamp").GetDateTime(),
Version = payload.GetProperty("version").GetString(),
PatientId = patient.PatientId,
Patient = patient,
Observations = new List<Observation>()
};
var observationsArray = payload.GetProperty("observations");
foreach (var obsElement in observationsArray.EnumerateArray())
{
var observation = new Observation
{
ObservationCode = obsElement.GetProperty("ObservationCode").GetString()?? string.Empty,
ObservationValue = obsElement.GetProperty("ObservationValue").GetString()?? string.Empty,
Units = obsElement.GetProperty("Units").GetString(),
AbnormalFlag = obsElement.GetProperty("AbnormalFlag").GetString(),
ResultStatus = obsElement.GetProperty("ResultStatus").GetString() ?? "F",
Message = message
};
message.Observations.Add(observation);
}
var createdMessage = await msgService.CreateMessageAsync(message);
return Ok(new {
message = _mapper.Map<MessageDTO>(createdMessage),
patient = _mapper.Map<PatientDTO>(patient)
});
}
catch (Exception ex)
{
return BadRequest(new { error = ex.Message + (ex.InnerException?.Message ?? "") });
}
}
}

View File

@ -1,5 +1,6 @@
using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using SimpleLIS.DTO;
using SimpleLIS.Models;
using SimpleLIS.Services;
@ -41,6 +42,16 @@ public class PatientsController : ControllerBase
return Ok(patientDto);
}
[HttpGet("{id}/observations")]
public async Task<IActionResult> GetPatientObservationById(string id){
List<Observation> observations;
if(int.TryParse(id, out int patientId))
observations = await _patientService.GetPatientObservationById(patientId);
else
observations = await _patientService.GetPatientObservationByHL7PatientId(id);
return Ok(_mapper.Map<List<ObservationDTO>>(observations));
}
[HttpGet]
public async Task<IActionResult> ListPatients()
{

View File

@ -26,6 +26,16 @@ public class PatientService
.FirstOrDefaultAsync(p => p.PatientId == id);
}
public async Task<List<Observation>> GetPatientObservationById(int id){
return await _context.Observations.Include(observation=>observation.Message)
.Where(observation=>observation.Message.PatientId==id).ToListAsync();
}
public async Task<List<Observation>> GetPatientObservationByHL7PatientId(string id){
return await _context.Observations.Include(observation => observation.Message)
.Where(observation => observation.Message.Patient.HL7PatientId == id).ToListAsync();
}
public async Task<IEnumerable<Patient>> ListPatientsAsync()
{
return await _context.Patients.ToListAsync();
@ -52,4 +62,14 @@ public class PatientService
{
return await _context.Patients.AnyAsync(p => p.PatientId == id);
}
public async Task<Patient?> GetPatientByNameAndDOB(string firstName, string lastName, DateTime dateOfBirth)
{
return await _context.Patients
.FirstOrDefaultAsync(p =>
p.FirstName == firstName &&
p.LastName == lastName &&
p.DateOfBirth == dateOfBirth);
}
}

BIN
hl7.db-shm Normal file

Binary file not shown.

BIN
hl7.db-wal Normal file

Binary file not shown.