Implemented DB population through the LIC service API
This commit is contained in:
parent
9f4197505e
commit
433723bf80
97
Controllers/HL7Controller.cs
Normal file
97
Controllers/HL7Controller.cs
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
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 _messageService;
|
||||||
|
private readonly PatientService _patientService;
|
||||||
|
private readonly IMapper _mapper;
|
||||||
|
|
||||||
|
public HL7Controller(
|
||||||
|
MessageService messageService,
|
||||||
|
PatientService patientService,
|
||||||
|
IMapper mapper)
|
||||||
|
{
|
||||||
|
_messageService = messageService;
|
||||||
|
_patientService = patientService;
|
||||||
|
_mapper = mapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("process")]
|
||||||
|
public async Task<IActionResult> ProcessHL7Message([FromBody] JsonElement payload)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// Create Patient
|
||||||
|
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>()
|
||||||
|
};
|
||||||
|
|
||||||
|
// Check for existing patient
|
||||||
|
var existingPatient = await _patientService.GetPatientByNameAndDOB(
|
||||||
|
patient.FirstName!,
|
||||||
|
patient.LastName!,
|
||||||
|
patient.DateOfBirth
|
||||||
|
);
|
||||||
|
|
||||||
|
patient = existingPatient ?? await _patientService.CreatePatientAsync(patient);
|
||||||
|
|
||||||
|
// Create Message
|
||||||
|
var message = new Message
|
||||||
|
{
|
||||||
|
ControlId = payload.GetProperty("controlId").GetString(),
|
||||||
|
MessageType = payload.GetProperty("messageType").GetString(),
|
||||||
|
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>()
|
||||||
|
};
|
||||||
|
|
||||||
|
// Create Observations
|
||||||
|
var observationsArray = payload.GetProperty("observations");
|
||||||
|
foreach (var obsElement in observationsArray.EnumerateArray())
|
||||||
|
{
|
||||||
|
var observation = new Observation
|
||||||
|
{
|
||||||
|
ObservationCode = obsElement.GetProperty("ObservationCode").GetString(),
|
||||||
|
ObservationValue = obsElement.GetProperty("ObservationValue").GetString(),
|
||||||
|
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 _messageService.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 ?? "") });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -52,4 +52,14 @@ public class PatientService
|
|||||||
{
|
{
|
||||||
return await _context.Patients.AnyAsync(p => p.PatientId == id);
|
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
BIN
hl7.db-shm
Normal file
Binary file not shown.
BIN
hl7.db-wal
Normal file
BIN
hl7.db-wal
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user