public async Task ExecuteAsync(AddCustomEntityDraftVersionCommand command, IExecutionContext executionContext) { var definitionCode = await QueryVersionAndGetDefinitionCode(command).FirstOrDefaultAsync(); EntityNotFoundException.ThrowIfNull(definitionCode, command.CustomEntityId); _permissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdatePermission>(definitionCode); var newVersionId = await _entityFrameworkSqlExecutor .ExecuteCommandWithOutputAsync <int?>("Cofoundry.CustomEntity_AddDraft", "CustomEntityVersionId", new SqlParameter("CustomEntityId", command.CustomEntityId), new SqlParameter("CopyFromCustomEntityVersionId", command.CopyFromCustomEntityVersionId), new SqlParameter("CreateDate", executionContext.ExecutionDate), new SqlParameter("CreatorId", executionContext.UserContext.UserId) ); if (!newVersionId.HasValue) { throw new UnexpectedSqlStoredProcedureResultException("Cofoundry.CustomEntity_AddDraft", "No CustomEntityVersionId was returned."); } command.OutputCustomEntityVersionId = newVersionId.Value; _customEntityCache.Clear(definitionCode, command.CustomEntityId); await _messageAggregator.PublishAsync(new CustomEntityDraftVersionAddedMessage() { CustomEntityId = command.CustomEntityId, CustomEntityVersionId = newVersionId.Value, CustomEntityDefinitionCode = definitionCode }); }
public async Task ExecuteAsync(DeleteCustomEntityDraftVersionCommand command, IExecutionContext executionContext) { var draft = await _dbContext .CustomEntityVersions .Include(v => v.CustomEntity) .SingleOrDefaultAsync(v => v.CustomEntityId == command.CustomEntityId && v.WorkFlowStatusId == (int)WorkFlowStatus.Draft); if (draft != null) { _permissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdatePermission>(draft.CustomEntity.CustomEntityDefinitionCode); var definitionCode = draft.CustomEntity.CustomEntityDefinitionCode; var versionId = draft.CustomEntityVersionId; using (var scope = _transactionScopeFactory.Create()) { await _commandExecutor.ExecuteAsync(new DeleteUnstructuredDataDependenciesCommand(CustomEntityVersionEntityDefinition.DefinitionCode, draft.CustomEntityVersionId)); _dbContext.CustomEntityVersions.Remove(draft); await _dbContext.SaveChangesAsync(); scope.Complete(); } _customEntityCache.Clear(definitionCode, command.CustomEntityId); await _messageAggregator.PublishAsync(new CustomEntityDraftVersionDeletedMessage() { CustomEntityId = command.CustomEntityId, CustomEntityDefinitionCode = definitionCode, CustomEntityVersionId = versionId }); } }
public async Task ExecuteAsync(UpdateCustomEntityUrlCommand command, IExecutionContext executionContext) { var entity = await _dbContext .CustomEntities .Include(c => c.CustomEntityVersions) .Where(e => e.CustomEntityId == command.CustomEntityId) .SingleOrDefaultAsync(); EntityNotFoundException.ThrowIfNull(entity, command.CustomEntityId); _permissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdateUrlPermission>(entity.CustomEntityDefinitionCode); var definition = _queryExecutor.GetById <CustomEntityDefinitionSummary>(entity.CustomEntityDefinitionCode); EntityNotFoundException.ThrowIfNull(definition, entity.CustomEntityDefinitionCode); await ValidateIsUnique(command, definition); Map(command, entity, definition); var isPublished = entity.CustomEntityVersions.Any(v => v.WorkFlowStatusId == (int)WorkFlowStatus.Published); await _dbContext.SaveChangesAsync(); _customEntityCache.Clear(entity.CustomEntityDefinitionCode, command.CustomEntityId); await _messageAggregator.PublishAsync(new CustomEntityUrlChangedMessage() { CustomEntityId = command.CustomEntityId, CustomEntityDefinitionCode = entity.CustomEntityDefinitionCode, HasPublishedVersionChanged = isPublished }); }
public async Task ExecuteAsync(UpdateCustomEntityUrlCommand command, IExecutionContext executionContext) { var entity = await _dbContext .CustomEntities .Where(e => e.CustomEntityId == command.CustomEntityId) .SingleOrDefaultAsync(); EntityNotFoundException.ThrowIfNull(entity, command.CustomEntityId); _permissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdateUrlPermission>(entity.CustomEntityDefinitionCode, executionContext.UserContext); var definitionQuery = new GetCustomEntityDefinitionSummaryByCodeQuery(entity.CustomEntityDefinitionCode); var definition = await _queryExecutor.ExecuteAsync(definitionQuery, executionContext); EntityNotFoundException.ThrowIfNull(definition, entity.CustomEntityDefinitionCode); await ValidateIsUniqueAsync(command, definition, executionContext); Map(command, entity, definition); await _dbContext.SaveChangesAsync(); _customEntityCache.Clear(entity.CustomEntityDefinitionCode, command.CustomEntityId); await _messageAggregator.PublishAsync(new CustomEntityUrlChangedMessage() { CustomEntityId = command.CustomEntityId, CustomEntityDefinitionCode = entity.CustomEntityDefinitionCode, HasPublishedVersionChanged = entity.PublishStatusCode == PublishStatusCode.Published }); }
public async Task ExecuteAsync(UnPublishCustomEntityCommand command, IExecutionContext executionContext) { var versions = await _dbContext .CustomEntityVersions .Include(v => v.CustomEntity) .Where(p => p.CustomEntityId == command.CustomEntityId && (p.WorkFlowStatusId == (int)WorkFlowStatus.Published || p.WorkFlowStatusId == (int)WorkFlowStatus.Draft)) .ToListAsync(); var publishedVersion = versions.SingleOrDefault(p => p.WorkFlowStatusId == (int)WorkFlowStatus.Published); EntityNotFoundException.ThrowIfNull(publishedVersion, command.CustomEntityId); _permissionValidationService.EnforceCustomEntityPermission <CustomEntityPublishPermission>(publishedVersion.CustomEntity.CustomEntityDefinitionCode); if (versions.Any(p => p.WorkFlowStatusId == (int)WorkFlowStatus.Draft)) { // If there's already a draft, change to approved. publishedVersion.WorkFlowStatusId = (int)WorkFlowStatus.Approved; } else { // Else set it to draft publishedVersion.WorkFlowStatusId = (int)WorkFlowStatus.Draft; } await _dbContext.SaveChangesAsync(); _customEntityCache.Clear(publishedVersion.CustomEntity.CustomEntityDefinitionCode, command.CustomEntityId); await _messageAggregator.PublishAsync(new CustomEntityUnPublishedMessage() { CustomEntityId = command.CustomEntityId, CustomEntityDefinitionCode = publishedVersion.CustomEntity.CustomEntityDefinitionCode }); }
public async Task ExecuteAsync(AddCustomEntityDraftVersionCommand command, IExecutionContext executionContext) { var definitionCode = await QueryVersionAndGetDefinitionCode(command).FirstOrDefaultAsync(); EntityNotFoundException.ThrowIfNull(definitionCode, command.CustomEntityId); _permissionValidationService.EnforceIsLoggedIn(executionContext.UserContext); _permissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdatePermission>(definitionCode, executionContext.UserContext); var newVersionId = await _customEntityStoredProcedures.AddDraftAsync( command.CustomEntityId, command.CopyFromCustomEntityVersionId, executionContext.ExecutionDate, executionContext.UserContext.UserId.Value); command.OutputCustomEntityVersionId = newVersionId; _customEntityCache.Clear(definitionCode, command.CustomEntityId); await _messageAggregator.PublishAsync(new CustomEntityDraftVersionAddedMessage() { CustomEntityId = command.CustomEntityId, CustomEntityVersionId = newVersionId, CustomEntityDefinitionCode = definitionCode }); }
public async Task ExecuteAsync(ReOrderCustomEntitiesCommand command, IExecutionContext executionContext) { var definition = _customEntityDefinitionRepository.GetByCode(command.CustomEntityDefinitionCode) as IOrderableCustomEntityDefinition; if (definition == null || definition.Ordering == CustomEntityOrdering.None) { throw new InvalidOperationException("Cannot re-order a custom entity type with a definition that does not implement IOrderableCustomEntityDefinition (" + definition.GetType().Name + ")"); } if (!definition.HasLocale && command.LocaleId.HasValue) { throw new ValidationException("Cannot order by locale because this custom entity type is not permitted to have a locale (" + definition.GetType().FullName + ")"); } var affectedIds = await _customEntityStoredProcedures.ReOrderAsync( command.CustomEntityDefinitionCode, command.OrderedCustomEntityIds, command.LocaleId ); foreach (var affectedId in affectedIds) { _customEntityCache.Clear(command.CustomEntityDefinitionCode, affectedId); } var messages = affectedIds.Select(i => new CustomEntityOrderingUpdatedMessage() { CustomEntityDefinitionCode = command.CustomEntityDefinitionCode, CustomEntityId = i }).ToList(); await _messageAggregator.PublishBatchAsync(messages); }
public async Task ExecuteAsync(DeleteCustomEntityCommand command, IExecutionContext executionContext) { var customEntity = await _dbContext .CustomEntities .SingleOrDefaultAsync(p => p.CustomEntityId == command.CustomEntityId); if (customEntity != null) { _permissionValidationService.EnforceCustomEntityPermission <CustomEntityDeletePermission>(customEntity.CustomEntityDefinitionCode); using (var scope = _transactionScopeFactory.Create()) { await _commandExecutor.ExecuteAsync(new DeleteUnstructuredDataDependenciesCommand(customEntity.CustomEntityDefinitionCode, customEntity.CustomEntityId)); _dbContext.CustomEntities.Remove(customEntity); await _dbContext.SaveChangesAsync(); scope.Complete(); } _customEntityCache.Clear(customEntity.CustomEntityDefinitionCode, command.CustomEntityId); await _messageAggregator.PublishAsync(new CustomEntityDeletedMessage() { CustomEntityId = command.CustomEntityId, CustomEntityDefinitionCode = customEntity.CustomEntityDefinitionCode }); } }
private Task OnTransactionComplete(CustomEntityVersion version) { _customEntityCache.Clear(version.CustomEntity.CustomEntityDefinitionCode, version.CustomEntityId); return(_messageAggregator.PublishAsync(new CustomEntityPublishedMessage() { CustomEntityId = version.CustomEntityId, CustomEntityDefinitionCode = version.CustomEntity.CustomEntityDefinitionCode })); }
private Task OnTransactionComplete(CustomEntity entity) { _customEntityCache.Clear(entity.CustomEntityDefinitionCode, entity.CustomEntityId); return(_messageAggregator.PublishAsync(new CustomEntityUnPublishedMessage() { CustomEntityId = entity.CustomEntityId, CustomEntityDefinitionCode = entity.CustomEntityDefinitionCode })); }
private Task OnTransactionComplete(DeleteCustomEntityCommand command, CustomEntity customEntity) { _customEntityCache.Clear(customEntity.CustomEntityDefinitionCode, command.CustomEntityId); return(_messageAggregator.PublishAsync(new CustomEntityDeletedMessage() { CustomEntityId = command.CustomEntityId, CustomEntityDefinitionCode = customEntity.CustomEntityDefinitionCode })); }
private Task OnTransactionComplete(CustomEntityVersion customEntityVersion, CustomEntityVersionPageBlock newBlock) { _customEntityCache.Clear(customEntityVersion.CustomEntity.CustomEntityDefinitionCode, customEntityVersion.CustomEntityId); return(_messageAggregator.PublishAsync(new CustomEntityVersionBlockAddedMessage() { CustomEntityId = customEntityVersion.CustomEntityId, CustomEntityVersionPageBlockId = newBlock.CustomEntityVersionPageBlockId, CustomEntityDefinitionCode = customEntityVersion.CustomEntity.CustomEntityDefinitionCode })); }
private Task OnTransactionComplete(string customEntityDefinitionCode, int customEntityId, int customEntityVersionPageBlockId) { _customEntityCache.Clear(customEntityDefinitionCode, customEntityId); return(_messageAggregator.PublishAsync(new CustomEntityVersionBlockMovedMessage() { CustomEntityId = customEntityId, CustomEntityDefinitionCode = customEntityDefinitionCode, CustomEntityVersionBlockId = customEntityVersionPageBlockId })); }
private Task OnTransactionComplete(CustomEntity entity) { _customEntityCache.Clear(entity.CustomEntityDefinitionCode, entity.CustomEntityId); return(_messageAggregator.PublishAsync(new CustomEntityUrlChangedMessage() { CustomEntityId = entity.CustomEntityId, CustomEntityDefinitionCode = entity.CustomEntityDefinitionCode, HasPublishedVersionChanged = entity.PublishStatusCode == PublishStatusCode.Published })); }
private Task OnUpdateDraftComplete(UpdateCustomEntityDraftVersionCommand command, CustomEntityVersion draft) { _customEntityCache.Clear(command.CustomEntityDefinitionCode, command.CustomEntityId); return(_messageAggregator.PublishAsync(new CustomEntityDraftVersionUpdatedMessage() { CustomEntityId = command.CustomEntityId, CustomEntityDefinitionCode = command.CustomEntityDefinitionCode, CustomEntityVersionId = draft.CustomEntityVersionId })); }
private Task OnTransactionComplete(CustomEntityVersionPageBlock dbBlock) { _customEntityCache.Clear(dbBlock.CustomEntityVersion.CustomEntity.CustomEntityDefinitionCode, dbBlock.CustomEntityVersion.CustomEntity.CustomEntityId); return(_messageAggregator.PublishAsync(new CustomEntityVersionBlockUpdatedMessage() { CustomEntityId = dbBlock.CustomEntityVersion.CustomEntityId, CustomEntityDefinitionCode = dbBlock.CustomEntityVersion.CustomEntity.CustomEntityDefinitionCode, CustomEntityVersionBlockId = dbBlock.CustomEntityVersionPageBlockId })); }
private Task OnTransactionComplete(AddCustomEntityDraftVersionCommand command, string definitionCode, int newVersionId) { _customEntityCache.Clear(definitionCode, command.CustomEntityId); return(_messageAggregator.PublishAsync(new CustomEntityDraftVersionAddedMessage() { CustomEntityId = command.CustomEntityId, CustomEntityVersionId = newVersionId, CustomEntityDefinitionCode = definitionCode })); }
public async Task ExecuteAsync(PublishCustomEntityCommand command, IExecutionContext executionContext) { // Prefer draft, but update published entity if no draft (only one draft permitted) var version = await _dbContext .CustomEntityVersions .Include(v => v.CustomEntity) .Where(v => v.CustomEntityId == command.CustomEntityId && (v.WorkFlowStatusId == (int)WorkFlowStatus.Draft || v.WorkFlowStatusId == (int)WorkFlowStatus.Published)) .OrderByDescending(v => v.WorkFlowStatusId == (int)WorkFlowStatus.Draft) .ThenByDescending(v => v.CreateDate) .FirstOrDefaultAsync(); EntityNotFoundException.ThrowIfNull(version, command.CustomEntityId); var definition = _customEntityDefinitionRepository.GetByCode(version.CustomEntity.CustomEntityDefinitionCode); EntityNotFoundException.ThrowIfNull(definition, version.CustomEntity.CustomEntityDefinitionCode); await _permissionValidationService.EnforceCustomEntityPermissionAsync <CustomEntityPublishPermission>(definition.CustomEntityDefinitionCode); UpdatePublishDate(command, executionContext, version); if (version.WorkFlowStatusId == (int)WorkFlowStatus.Published) { // only thing we can do with a published version is update the date await _dbContext.SaveChangesAsync(); } else { await ValidateTitle(version, definition); using (var scope = _transactionScopeFactory.Create(_dbContext)) { await UpdateUrlSlugIfRequired(version, definition); version.WorkFlowStatusId = (int)WorkFlowStatus.Published; version.CustomEntity.PublishStatusCode = PublishStatusCode.Published; await _dbContext.SaveChangesAsync(); await _customEntityStoredProcedures.UpdatePublishStatusQueryLookupAsync(command.CustomEntityId); scope.Complete(); } } _customEntityCache.Clear(version.CustomEntity.CustomEntityDefinitionCode, command.CustomEntityId); await _messageAggregator.PublishAsync(new CustomEntityPublishedMessage() { CustomEntityId = command.CustomEntityId, CustomEntityDefinitionCode = version.CustomEntity.CustomEntityDefinitionCode }); }
private Task OnTransactionComplete(ReOrderCustomEntitiesCommand command, ICollection <int> affectedIds) { foreach (var affectedId in affectedIds) { _customEntityCache.Clear(command.CustomEntityDefinitionCode, affectedId); } var messages = affectedIds.Select(i => new CustomEntityOrderingUpdatedMessage() { CustomEntityDefinitionCode = command.CustomEntityDefinitionCode, CustomEntityId = i }).ToList(); return(_messageAggregator.PublishBatchAsync(messages)); }
public async Task ExecuteAsync(PublishCustomEntityCommand command, IExecutionContext executionContext) { var versions = await _dbContext .CustomEntityVersions .Include(v => v.CustomEntity) .Where(p => p.CustomEntityId == command.CustomEntityId && (p.WorkFlowStatusId == (int)WorkFlowStatus.Draft || p.WorkFlowStatusId == (int)WorkFlowStatus.Published)) .ToListAsync(); var publishedVersion = versions.SingleOrDefault(v => v.WorkFlowStatusId == (int)WorkFlowStatus.Published); var draftVersion = versions.SingleOrDefault(v => v.WorkFlowStatusId == (int)WorkFlowStatus.Draft); EntityNotFoundException.ThrowIfNull(draftVersion, "Draft:" + command.CustomEntityId); var definition = _customEntityDefinitionRepository.GetByCode(draftVersion.CustomEntity.CustomEntityDefinitionCode); EntityNotFoundException.ThrowIfNull(definition, draftVersion.CustomEntity.CustomEntityDefinitionCode); _permissionValidationService.EnforceCustomEntityPermission <CustomEntityPublishPermission>(definition.CustomEntityDefinitionCode); await ValidateTitle(draftVersion, definition); using (var scope = _transactionScopeFactory.Create()) { // Find the published one and make it approved if (publishedVersion != null) { publishedVersion.WorkFlowStatusId = (int)WorkFlowStatus.Approved; await _dbContext.SaveChangesAsync(); } await UpdateUrlSlugIfRequired(draftVersion, definition); // Find the draft page and make it published draftVersion.WorkFlowStatusId = (int)WorkFlowStatus.Published; await _dbContext.SaveChangesAsync(); scope.Complete(); } _customEntityCache.Clear(draftVersion.CustomEntity.CustomEntityDefinitionCode, command.CustomEntityId); await _messageAggregator.PublishAsync(new CustomEntityPublishedMessage() { CustomEntityId = command.CustomEntityId, CustomEntityDefinitionCode = draftVersion.CustomEntity.CustomEntityDefinitionCode }); }
public async Task ExecuteAsync(UnPublishCustomEntityCommand command, IExecutionContext executionContext) { var customEntity = await _dbContext .CustomEntities .Where(p => p.CustomEntityId == command.CustomEntityId) .SingleOrDefaultAsync(); EntityNotFoundException.ThrowIfNull(customEntity, command.CustomEntityId); _permissionValidationService.EnforceCustomEntityPermission <CustomEntityPublishPermission>(customEntity.CustomEntityDefinitionCode, executionContext.UserContext); if (customEntity.PublishStatusCode == PublishStatusCode.Unpublished) { // No action return; } var version = await _dbContext .CustomEntityVersions .Include(v => v.CustomEntity) .Where(v => v.CustomEntityId == command.CustomEntityId && (v.WorkFlowStatusId == (int)WorkFlowStatus.Draft || v.WorkFlowStatusId == (int)WorkFlowStatus.Published)) .OrderByDescending(v => v.WorkFlowStatusId == (int)WorkFlowStatus.Draft) .ThenByDescending(v => v.CreateDate) .FirstOrDefaultAsync(); EntityNotFoundException.ThrowIfNull(version, command.CustomEntityId); customEntity.PublishStatusCode = PublishStatusCode.Unpublished; version.WorkFlowStatusId = (int)WorkFlowStatus.Draft; using (var scope = _transactionScopeFactory.Create(_dbContext)) { await _dbContext.SaveChangesAsync(); await _customEntityStoredProcedures.UpdatePublishStatusQueryLookupAsync(command.CustomEntityId); scope.Complete(); } _customEntityCache.Clear(customEntity.CustomEntityDefinitionCode, command.CustomEntityId); await _messageAggregator.PublishAsync(new CustomEntityUnPublishedMessage() { CustomEntityId = command.CustomEntityId, CustomEntityDefinitionCode = customEntity.CustomEntityDefinitionCode }); }
public async Task ExecuteAsync(UpdateCustomEntityVersionPageBlockCommand command, IExecutionContext executionContext) { var dbBlock = await _dbContext .CustomEntityVersionPageBlocks .Include(m => m.PageBlockTypeTemplate) .Include(m => m.PageBlockType) .Include(m => m.CustomEntityVersion) .ThenInclude(v => v.CustomEntity) .Where(l => l.CustomEntityVersionPageBlockId == command.CustomEntityVersionPageBlockId) .SingleOrDefaultAsync(); EntityNotFoundException.ThrowIfNull(dbBlock, command.CustomEntityVersionPageBlockId); await _permissionValidationService.EnforceCustomEntityPermissionAsync <CustomEntityUpdatePermission>(dbBlock.CustomEntityVersion.CustomEntity.CustomEntityDefinitionCode); if (dbBlock.CustomEntityVersion.WorkFlowStatusId != (int)WorkFlowStatus.Draft) { throw new NotPermittedException("Page blocks cannot be updated unless the entity is in draft status"); } using (var scope = _transactionScopeFactory.Create(_dbContext)) { await _pageBlockCommandHelper.UpdateModelAsync(command, dbBlock); await _dbContext.SaveChangesAsync(); var dependencyCommand = new UpdateUnstructuredDataDependenciesCommand( CustomEntityVersionPageBlockEntityDefinition.DefinitionCode, dbBlock.CustomEntityVersionPageBlockId, command.DataModel); await _commandExecutor.ExecuteAsync(dependencyCommand); scope.Complete(); } _customEntityCache.Clear(dbBlock.CustomEntityVersion.CustomEntity.CustomEntityDefinitionCode, dbBlock.CustomEntityVersion.CustomEntity.CustomEntityId); await _messageAggregator.PublishAsync(new CustomEntityVersionBlockUpdatedMessage() { CustomEntityId = dbBlock.CustomEntityVersion.CustomEntityId, CustomEntityDefinitionCode = dbBlock.CustomEntityVersion.CustomEntity.CustomEntityDefinitionCode, CustomEntityVersionBlockId = dbBlock.CustomEntityVersionPageBlockId }); }
public async Task ExecuteAsync(DeleteCustomEntityVersionPageModuleCommand command, IExecutionContext executionContext) { var dbResult = await _dbContext .CustomEntityVersionPageModules .Where(m => m.CustomEntityVersionPageModuleId == command.CustomEntityVersionPageModuleId) .Select(m => new { Module = m, CustomEntityId = m.CustomEntityVersion.CustomEntityId, CustomEntityDefinitionCode = m.CustomEntityVersion.CustomEntity.CustomEntityDefinitionCode, WorkFlowStatusId = m.CustomEntityVersion.WorkFlowStatusId }) .SingleOrDefaultAsync(); if (dbResult != null) { _permissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdatePermission>(dbResult.CustomEntityDefinitionCode); if (dbResult.WorkFlowStatusId != (int)WorkFlowStatus.Draft) { throw new NotPermittedException("Page modules cannot be deleted unless the entity is in draft status"); } var customEntityVersionModuleId = dbResult.Module.CustomEntityVersionPageModuleId; using (var scope = _transactionScopeFactory.Create()) { await _commandExecutor.ExecuteAsync(new DeleteUnstructuredDataDependenciesCommand(CustomEntityVersionPageModuleEntityDefinition.DefinitionCode, dbResult.Module.CustomEntityVersionPageModuleId)); _dbContext.CustomEntityVersionPageModules.Remove(dbResult.Module); await _dbContext.SaveChangesAsync(); scope.Complete(); } _customEntityCache.Clear(dbResult.CustomEntityDefinitionCode, dbResult.CustomEntityId); await _messageAggregator.PublishAsync(new CustomEntityVersionModuleDeletedMessage() { CustomEntityId = dbResult.CustomEntityId, CustomEntityDefinitionCode = dbResult.CustomEntityDefinitionCode, CustomEntityVersionId = customEntityVersionModuleId }); } }
public async Task ExecuteAsync(UpdateCustomEntityDraftVersionCommand command, IExecutionContext executionContext) { var definition = _customEntityDefinitionRepository.GetByCode(command.CustomEntityDefinitionCode); var draft = await GetDraftVersionAsync(command); using (var scope = _transactionScopeFactory.Create()) { draft = await CreateDraftIfRequiredAsync(command, draft); await ValidateTitle(command, draft, definition); UpdateDraft(command, draft); await _dbContext.SaveChangesAsync(); var dependencyCommand = new UpdateUnstructuredDataDependenciesCommand( CustomEntityVersionEntityDefinition.DefinitionCode, draft.CustomEntityVersionId, command.Model); await _commandExecutor.ExecuteAsync(dependencyCommand); scope.Complete(); } _customEntityCache.Clear(command.CustomEntityDefinitionCode, command.CustomEntityId); await _messageAggregator.PublishAsync(new CustomEntityDraftVersionUpdatedMessage() { CustomEntityId = command.CustomEntityId, CustomEntityDefinitionCode = command.CustomEntityDefinitionCode, CustomEntityVersionId = draft.CustomEntityVersionId }); if (command.Publish) { using (var scope = _transactionScopeFactory.Create()) { await _commandExecutor.ExecuteAsync(new PublishCustomEntityCommand(draft.CustomEntityId)); scope.Complete(); } } }
public async Task ExecuteAsync(ReOrderCustomEntitiesCommand command, IExecutionContext executionContext) { var definition = _customEntityDefinitionRepository.GetByCode(command.CustomEntityDefinitionCode) as IOrderableCustomEntityDefinition; if (definition == null || definition.Ordering == CustomEntityOrdering.None) { throw new InvalidOperationException("Cannot re-order a custom entity type with a definition that does not implement IOrderableCustomEntityDefinition (" + definition.GetType().Name + ")"); } if (!definition.HasLocale && command.LocaleId.HasValue) { throw new ValidationException("Cannot order by locale because this custom entity type is not permitted to have a locale (" + definition.GetType().FullName + ")"); } var updatedIdString = await _sqlExecutor .ExecuteCommandWithOutputAsync <string>("Cofoundry.CustomEntity_ReOrder", "UpdatedIds", new SqlParameter("CustomEntityDefinitionCode", command.CustomEntityDefinitionCode), new SqlParameter("CustomEntityIds", string.Join(",", command.OrderedCustomEntityIds)), CreateNullableIntParameter("LocaleId", command.LocaleId) ); var affectedIds = IntParser.ParseFromDelimitedString(updatedIdString); foreach (var affectedId in affectedIds) { _customEntityCache.Clear(command.CustomEntityDefinitionCode, affectedId); } var messages = affectedIds.Select(i => new CustomEntityOrderingUpdatedMessage() { CustomEntityDefinitionCode = command.CustomEntityDefinitionCode, CustomEntityId = i }); await _messageAggregator.PublishBatchAsync(messages); }
public async Task ExecuteAsync(AddCustomEntityVersionPageModuleCommand command, IExecutionContext executionContext) { var customEntityVersion = _dbContext .CustomEntityVersions .Include(s => s.CustomEntityVersionPageModules) .Include(s => s.CustomEntity) .FirstOrDefault(v => v.CustomEntityVersionId == command.CustomEntityVersionId); EntityNotFoundException.ThrowIfNull(customEntityVersion, command.CustomEntityVersionId); _permissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdatePermission>(customEntityVersion.CustomEntity.CustomEntityDefinitionCode); if (customEntityVersion.WorkFlowStatusId != (int)WorkFlowStatus.Draft) { throw new NotPermittedException("Page modules cannot be deleted unless the entity is in draft status"); } var templateSectionSection = await _dbContext .PageTemplateSections .FirstOrDefaultAsync(l => l.PageTemplateSectionId == command.PageTemplateSectionId); EntityNotFoundException.ThrowIfNull(templateSectionSection, command.PageTemplateSectionId); var customEntityVersionModules = customEntityVersion .CustomEntityVersionPageModules .Where(m => m.PageTemplateSectionId == templateSectionSection.PageTemplateSectionId); CustomEntityVersionPageModule adjacentItem = null; if (command.AdjacentVersionModuleId.HasValue) { adjacentItem = customEntityVersionModules .SingleOrDefault(m => m.CustomEntityVersionPageModuleId == command.AdjacentVersionModuleId); EntityNotFoundException.ThrowIfNull(adjacentItem, command.AdjacentVersionModuleId); } var newModule = new CustomEntityVersionPageModule(); newModule.PageTemplateSection = templateSectionSection; await _pageModuleCommandHelper.UpdateModelAsync(command, newModule); newModule.CustomEntityVersion = customEntityVersion; _entityOrderableHelper.SetOrderingForInsert(customEntityVersionModules, newModule, command.InsertMode, adjacentItem); _dbContext.CustomEntityVersionPageModules.Add(newModule); using (var scope = _transactionScopeFactory.Create()) { await _dbContext.SaveChangesAsync(); var dependencyCommand = new UpdateUnstructuredDataDependenciesCommand( CustomEntityVersionPageModuleEntityDefinition.DefinitionCode, newModule.CustomEntityVersionPageModuleId, command.DataModel); await _commandExecutor.ExecuteAsync(dependencyCommand); scope.Complete(); } _customEntityCache.Clear(customEntityVersion.CustomEntity.CustomEntityDefinitionCode, customEntityVersion.CustomEntityId); command.OutputCustomEntityVersionId = newModule.CustomEntityVersionPageModuleId; await _messageAggregator.PublishAsync(new CustomEntityVersionModuleAddedMessage() { CustomEntityId = customEntityVersion.CustomEntityId, CustomEntityVersionPageModuleId = newModule.CustomEntityVersionPageModuleId, CustomEntityDefinitionCode = customEntityVersion.CustomEntity.CustomEntityDefinitionCode }); }
public async Task ExecuteAsync(MoveCustomEntityVersionPageBlockCommand command, IExecutionContext executionContext) { var dbResult = await _dbContext .CustomEntityVersionPageBlocks .Where(b => b.CustomEntityVersionPageBlockId == command.CustomEntityVersionPageBlockId) .Select(b => new { Block = b, CustomEntityId = b.CustomEntityVersion.CustomEntityId, CustomEntityDefinitionCode = b.CustomEntityVersion.CustomEntity.CustomEntityDefinitionCode, WorkFlowStatusId = b.CustomEntityVersion.WorkFlowStatusId }) .SingleOrDefaultAsync(); EntityNotFoundException.ThrowIfNull(dbResult, command.CustomEntityVersionPageBlockId); _permissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdatePermission>(dbResult.CustomEntityDefinitionCode, executionContext.UserContext); if (dbResult.WorkFlowStatusId != (int)WorkFlowStatus.Draft) { throw new NotPermittedException("Page blocks cannot be moved unless the entity is in draft status"); } var block = dbResult.Block; var blockToSwapWithQuery = _dbContext .CustomEntityVersionPageBlocks .Where(p => p.PageTemplateRegionId == block.PageTemplateRegionId && p.CustomEntityVersionId == block.CustomEntityVersionId); CustomEntityVersionPageBlock blockToSwapWith; switch (command.Direction) { case OrderedItemMoveDirection.Up: blockToSwapWith = await blockToSwapWithQuery .Where(p => p.Ordering < block.Ordering) .OrderByDescending(p => p.Ordering) .FirstOrDefaultAsync(); break; case OrderedItemMoveDirection.Down: blockToSwapWith = await blockToSwapWithQuery .Where(p => p.Ordering > block.Ordering) .OrderBy(p => p.Ordering) .FirstOrDefaultAsync(); break; default: throw new InvalidOperationException("OrderedItemMoveDirection not recognised: " + command.Direction); } if (blockToSwapWith == null) { return; } int oldOrdering = block.Ordering; block.Ordering = blockToSwapWith.Ordering; blockToSwapWith.Ordering = oldOrdering; await _dbContext.SaveChangesAsync(); _customEntityCache.Clear(dbResult.CustomEntityDefinitionCode, dbResult.CustomEntityId); await _messageAggregator.PublishAsync(new CustomEntityVersionBlockMovedMessage() { CustomEntityId = dbResult.CustomEntityId, CustomEntityDefinitionCode = dbResult.CustomEntityDefinitionCode, CustomEntityVersionBlockId = dbResult.Block.CustomEntityVersionPageBlockId }); }