Skip to content

Post Process pushed records

stefffdev edited this page May 20, 2021 · 2 revisions

Sometimes some post-processing has to be done on records that were pushed from the client. For that Purpose PostOperationsAsync returns a list of tuples, that contain the record that was changed, created or deleted as well as the OperationType that triggered the change.

Sample

[HttpPost]
public async Task<IActionResult> PostOperationsAsync(List<NubeOperation> operations)
{
    HttpContext.VerifyUserHasAnyAcceptedScope(_authentication.ScopeRequiredByApi);
    string userId = _authentication.GetUserIdentifier(User);
    string installationId = Request.GetInstallationId();

    try
    {
        var changedEntities = await _operationService
            .ProcessOperationsAsync(_context, operations, userId, installationId);

        foreach (var changedEntity in changedEntities)
        {
            if (changedEntity.Entity is Customer customer && 
                changedEntity.Type != OperationType.Deleted )
            {
                customer.CompanyName = "My Company";
                _context.SaveChanges();
            }
        }
    }
    catch (Exception ex)
    {
        while (ex.InnerException != null)
        {
            ex = ex.InnerException;
        }

        return BadRequest(ex.Message);
    }

    return Ok();
}
Clone this wiki locally