Search code examples
c#jsonhl7-fhir

How to update Patient in FHIR API?


I have JSON data of Patient and I try to update this with new data. When I try to update this Patient, the entries will be duplicate and not update like this:

{
   "telecom": [
    {
        "system": "phone",
        "value": "2222215",
        "use": "home"
    },
    {
        "system": "phone",
        "value": "2222215",
        "use": "home"
    }
],
"gender": "male",
"birthDate": "2020-12-24",
"address": [
    {
        "use": "home",
        "line": [
            "28MCT"
        ],
        "city": "Hưng Yên",
        "district": "Huyện Kim Động",
        "state": "Thị Trấn Lương Bằng",
        "country": "VNM"
    },
    {
        "use": "home",
        "city": "Hưng Yên",
        "district": "Huyện Kim Động",
        "state": "Thị Trấn Lương Bằng",
        "country": "VNM"
    }
]}

Which way to update exactly? Here is my code:

private static void UpdatePatient(string patientId)
    {
        var client = new FhirClient("http://hapi.fhir.org/baseR4");
        Patient pat = client.Read<Patient>("Patient/1723313");
        pat.Address.Add(new Address(){ 
            Line = new string[1] {"28 MCT"},
            District = "Bến Cát",
            State = "An Thới",
            City = "Bình Dương",
            Country = "VNM"
        });
        client.Update<Patient>(pat);
    }

Thanks for help.


Solution

  • The telecom and address fields are lists. So if you have existing data and you do pat.Address.Add, it will add a new item to the existing list - keeping the already existing address. You will actually have to update your Telecom/Address field first, before sending the updated data to the server.

    For example - between client.Read and client.Update, using System.Linq:

    var a = x.Address.First(ca => ca.Use == Address.AddressUse.Home);
    a.Line = new string[] { "28 MCT" };