JSON Deserialize and update Entity

JSON Deserialize and update Entity

  

Hi,

I have an exposed REST API, I send the JSON data below for the First time

{
"data":{
       "nun":"1234",
   "is_locked":true,
   "is_enabled":true,
   "is_active":true,
   "name":"abddfdfdfdddcx",
   "last_heartbeat_ts":#21-09-1976,12:00GMT+00:00#,
      "last_configured_ts":#21-09-1976,12:00GMT+00:00#,
      "creation_ts":#21-09-1976,12:00GMT+00:00#,
   "config_version":"v1.01",
   "cpu_usage":50,
   "memory_usage":20,
   "alarms":0000000
   }
}

Note: nun is primary key.

The API successfully insert the data into the database.

Now I want to update the a particular column of database row so I send the JSON 

{
"data":{
     "nun":"1234",
     "last_heartbeat_ts":#21-09-1976,12:00GMT+00:00#,
     
   }
}

Now the the deserialise functionality put some default data for the missing properties and update the table accordingly.

How to update only the "last_heartbeat_ts" for that row.

Hi Sowvik Roy,

The only easy way (that I can see right away) to update only selected fields is to use the following pattern:

1. Use a Get<EntityName>ForUpdate using the Id of the record.
2. Change the fields you desire to update in the record returned by the action
3. Use the record returned by the action as the source for a Update<EntityName> action

So, you should identify which fields are being sent by the requester to the REST API so that you can set the values accordingly.

One way to identify the fields that were sent (probably not the best, but anyway...), would be to use an structure that is "different" from the record itself, with extra boolean fields to mark the fields that are present in the request with "false" by default.

Than, in an OnRequest (see this), you could manipulate the request body to include the boolean fields with 'true' to the fields present.

Later on, you can use them to identify which fields to change.

I never did this (update entities from REST information in so directly way), so I don't know if there is a better approach (probably there is).

Anyway, hope this can help you until someone provides a better approach.

Cheers,
Eduardo Jauch