public async Task <IActionResult> Patch([FromRoute] string id, [FromBody] ApiPatchCommand <ApiGeoTask> patch) { try { var currentPrincipal = HttpContext.User; var currentUserName = currentPrincipal?.Identity?.Name; using var logScope = Logger.BeginScope("{User}", currentUserName); Logger.LogInformation(ApiLogEvent.ApiRequest, "Task patch command. Id={Id}. Command={Command}", id, patch.ToDictionary()); if (patch?.Patch is null || String.IsNullOrWhiteSpace(id)) { Logger.LogWarning(ApiLogEvent.ApiArgumentError, "Patch task empty argument."); return(BadRequest()); } var getQuery = new EntityQuery <GeoTask>(id, currentPrincipal); var getResult = await Mediator.Send(getQuery); if (getResult is null || !getResult.Success) { Logger.LogWarning(ApiLogEvent.ApiErrorResponse, "Get task error response. Error={Error}.", getResult?.Errors); return(BadRequest()); } var apiGeoTask = getResult.Entity.ToApiGeoTask(); patch.Patch.ApplyTo(apiGeoTask); var command = apiGeoTask.ToGeoTaskUpdateCommand(id, currentPrincipal, patch.MessageTitle, patch.MessageDescription); var result = await Mediator.Send(command); if (result is null || !result.Success) { Logger.LogWarning(ApiLogEvent.ApiErrorResponse, "Task update error response. Error={Error}.", result?.Errors); return(BadRequest()); } return(Ok()); } catch (Exception ex) when(Logger.WriteScopeWhenException (ApiLogEvent.ApiErrorResponse, ex)) { return(BadRequest()); } }
public static Dictionary <string, object> ToDictionary(this ApiPatchCommand <ApiGeoTask> from) { if (from is null) { return(null); } var strBuilder = new StringBuilder(); if (from.Patch != null) { foreach (var op in from.Patch.Operations) { strBuilder.AppendLine($"op={op.op}, path={op.path}, value={op.value}"); } } return(new Dictionary <string, object> { { "Patch", strBuilder.ToString() }, { "MessageTitle", from.MessageTitle }, { "MessageDescription", from.MessageDescription }, }); }