/// <summary> /// Executes a command in a "Patch" style, allowing for a partial update of a resource. In /// order to support this method, there must be a query handler defined that implements /// IQueryHandler<GetQuery<TCommand>> so the full command object can be fecthed /// prior to patching. Once patched and executed, a formatted IHttpActionResult is returned, /// handling any validation errors and permission errors. /// </summary> /// <typeparam name="TCommand">Type of the command to execute</typeparam> /// <param name="controller">The Controller instance using the helper</param> /// <param name="delta">The delta of the command to patch and execute</param> public async Task <IActionResult> RunCommandAsync <TCommand>(ControllerBase controller, IDelta <TCommand> delta) where TCommand : class, ICommand { var query = new GetUpdateCommandQuery <TCommand>(); var command = await _queryExecutor.ExecuteAsync(query); if (delta != null) { delta.Patch(command); } return(await RunCommandAsync(controller, command)); }
/// <summary> /// Executes a command in a "Patch" style, allowing for a partial update of a resource. In /// order to support this method, there must be a query handler defined that implements /// IQueryHandler<GetByIdQuery<TCommand>> so the full command object can be fecthed /// prior to patching. Once patched and executed, a formatted JsonResult is returned, /// handling any validation errors and permission errors. /// </summary> /// <typeparam name="TCommand">Type of the command to execute</typeparam> /// <param name="delta">The delta of the command to patch and execute</param> public async Task <JsonResult> RunCommandAsync <TCommand>(int id, IDelta <TCommand> delta) where TCommand : class, ICommand { var query = new GetUpdateCommandByIdQuery <TCommand>(id); var command = await _queryExecutor.ExecuteAsync(query); if (delta != null) { delta.Patch(command); } return(await RunCommandAsync(command)); }
public async Task <IActionResult> PatchDraft(int pageId, [FromBody] IDelta <UpdatePageDraftVersionCommand> delta) { // Custom patching because we may need to create a draft version first var query = new GetUpdateCommandByIdQuery <UpdatePageDraftVersionCommand>(pageId); var command = await _queryExecutor.ExecuteAsync(query); if (command == null) { var createDraftCommand = new AddPageDraftVersionCommand(); createDraftCommand.PageId = pageId; await _commandExecutor.ExecuteAsync(createDraftCommand); command = await _queryExecutor.ExecuteAsync(query); } delta.Patch(command); return(await _apiResponseHelper.RunCommandAsync(this, command)); }
public void Patch(TKey key, IDelta <TEntity> item) { var entity = _db.Set <TEntity>().Find(key); if (entity == null) { var msg = $"The key ({key}) isn't found in the {typeof(TEntity).Name} table."; throw new NotFoundException(msg); } item.Patch(entity); var a = _db.ChangeTracker.Entries(); if (a.Any(x => x.State == EntityState.Modified || x.State == EntityState.Added || x.State == EntityState.Deleted)) { _db.SaveChanges(); } }