public virtual async Task <IActionResult> SaveAsync(string id) { try { if (string.IsNullOrWhiteSpace(id)) { return(this.ExceptionResult(400, "An entity identifier is required in path.", "NoBody")); } var provider = await GetProviderAsync(); var content = await JsonObjectNode.ParseAsync(Request.Body); var r = await provider.SaveAsync(id, content); var entity = (r as ChangingResultInfo <TEntity>)?.Data; Logger?.LogInformation(new EventId(17006004, "UpdateEntity"), entity != null ? $"Update entity {entity.GetType().Name} {entity.Name} ({entity.Id})." : $"Failed update entity {id} because of non-existing."); return(r.ToActionResult()); } catch (Exception ex) { var er = this.ExceptionResult(ex, true); if (er != null) { Logger?.LogError(new EventId(17006004, "UpdateEntity"), $"Update entity {id} failed. {ex.GetType().Name} {ex.Message}"); return(er); } Logger?.LogError(new EventId(17006004, "UpdateEntity"), $"Update entity {id} failed with internal error. {ex.GetType().Name} {ex.Message}"); throw; } }
internal static async Task <IActionResult> SaveEntityAsync <T>(this ControllerBase controller, Func <string, BaseResourceAccessClient, Task <T> > entityResolver, Func <T, BaseResourceAccessClient, JsonObjectNode, Task <ChangingResultInfo> > save, string id) where T : BaseResourceEntity { if (string.IsNullOrWhiteSpace(id)) { return(ChangeErrorKinds.Argument.ToActionResult("Requires entity identifier.")); } var instance = await controller.GetResourceAccessClientAsync(); var entity = await entityResolver(id, instance); if (controller.Request.Body == null) { return(new ChangingResultInfo <T>(ChangeMethods.Unchanged, entity).ToActionResult()); } if (entity == null) { return(ChangeErrorKinds.NotFound.ToActionResult("The resource does not exist.")); } var delta = await JsonObjectNode.ParseAsync(controller.Request.Body); if (delta.Count == 0) { return(new ChangingResultInfo <T>(ChangeMethods.Unchanged, entity, "Nothing update request.").ToActionResult()); } entity.SetProperties(delta); var result = await save(entity, instance, delta); return(result.ToActionResult()); }
/// <summary> /// Gets the JSON object from body. /// </summary> /// <param name="request">The HTTP request.</param> /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> /// <returns>A JSON object instance; or null, if no body.</returns> /// <exception cref="JsonException">json does not represent a valid single JSON object.</exception> /// <exception cref="ArgumentException">options contains unsupported options.</exception> public static Task <JsonObjectNode> ReadBodyAsJsonAsync(this HttpRequest request, CancellationToken cancellationToken) { if (request == null || request.Body == null) { return(null); } return(JsonObjectNode.ParseAsync(request.Body, default, cancellationToken));
public async Task <IActionResult> SaveSettingsDataByKeyAsync(string key) { if (string.IsNullOrWhiteSpace(key)) { return(BadRequest()); } var instance = await this.GetResourceAccessClientAsync(); var body = await JsonObjectNode.ParseAsync(Request.Body); var result = await instance.SaveSettingsAsync(key, null, body); Logger?.LogInformation(new EventId(17001103, "SaveGlobalSettings"), "Save global settings {0}.", key); return(result.ToActionResult()); }
/// <summary> /// Deserializes the HTTP JSON content into an object as the specific type. /// </summary> /// <typeparam name="T">The type of the result expected.</typeparam> /// <param name="httpContent">The http response content.</param> /// <param name="origin">A value of type seek origin indicating the reference point used to obtain the new position before deserialziation.</param> /// <param name="cancellationToken">The optional cancellation token.</param> /// <returns>The result serialized.</returns> /// <exception cref="ArgumentNullException">The argument is null.</exception> public static async Task <T> DeserializeJsonAsync <T>(this HttpContent httpContent, SeekOrigin origin, CancellationToken cancellationToken = default) { if (httpContent == null) { throw new ArgumentNullException(nameof(httpContent), "httpContent should not be null."); } #if NET5_0_OR_GREATER var stream = await httpContent.ReadAsStreamAsync(cancellationToken); #else var stream = await httpContent.ReadAsStreamAsync(); #endif IO.StreamCopy.TrySeek(stream, origin); var type = typeof(T); if (type == typeof(JsonObjectNode)) { return((T)(object)await JsonObjectNode.ParseAsync(stream, default, cancellationToken));
public virtual async Task <IActionResult> SaveAsync(string id) { try { var delta = await JsonObjectNode.ParseAsync(Request.Body); var r = await SaveAsync(id, delta); return(r.ToActionResult()); } catch (Exception ex) { var er = this.ExceptionResult(ex, true); if (er != null) { Logger?.LogError(new EventId(17006014, "UpdateEntity"), $"Update entity {id} failed. {ex.GetType().Name} {ex.Message}"); return(er); } Logger?.LogError(new EventId(17006014, "UpdateEntity"), $"Update entity {id} failed with internal error. {ex.GetType().Name} {ex.Message}"); throw; } }