Reputation: 311
Currently have an asp.net web api and implementing JSON Patch using AspNetCore.JsonPatch
and want to perform a side effect if a certain action is done via Json patch?
E.g. patch request to update an array is done then triggers another function to be called to update a database.
Is there anything built in to do this?
Upvotes: 0
Views: 467
Reputation: 9953
There is no built in can do it, But you can use automapper
to achieve it. Refer to this demo:
[HttpPatch("update/{id}")]
public Person Patch(int id, [FromBody]JsonPatchDocument<PersonDTO> personPatch)
{
// Get our original person object from the database.
PersonDatabase personDatabase = dbcontext.xx.GetById(id);
//Use Automapper to map that to our DTO object.
PersonDTO personDTO = _mapper.Map<PersonDTO>(personDatabase);
//Apply the patch to that DTO.
personPatch.ApplyTo(personDTO);
//Use automapper to map the DTO back ontop of the database object.
_mapper.Map(personDTO, personDatabase);
//Update our person in the database.
dbcontext.xx.Update(personDatabase);
return personDTO;
}
Upvotes: 1