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, executionContext.UserContext); var definitionCode = draft.CustomEntity.CustomEntityDefinitionCode; var versionId = draft.CustomEntityVersionId; using (var scope = _transactionScopeFactory.Create(_dbContext)) { await _commandExecutor.ExecuteAsync(new DeleteUnstructuredDataDependenciesCommand(CustomEntityVersionEntityDefinition.DefinitionCode, draft.CustomEntityVersionId), executionContext); _dbContext.CustomEntityVersions.Remove(draft); await _dbContext.SaveChangesAsync(); await _customEntityStoredProcedures.UpdatePublishStatusQueryLookupAsync(command.CustomEntityId); scope.QueueCompletionTask(() => OnTransactionComplete(command, definitionCode, versionId)); await scope.CompleteAsync(); } } }
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(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 }); } }
public async Task ExecuteAsync(ApproveBookingCommand command, IExecutionContext executionContext) { PermissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdatePermission>(BookingCustomEntityDefinition.DefinitionCode, executionContext.UserContext); using (var scope = DomainRepository.Transactions().CreateScope()) { var booking = await BookingProvider.GetBookingById(command.Id); booking.BookingState = BookingDataModel.BookingStateType.Approved; booking.IsApproved = true; booking.IsCancelled = false; await booking.AddLogEntry(CurrentUserProvider, "Reservationen blev godkendt."); UpdateCustomEntityDraftVersionCommand updateCmd = new UpdateCustomEntityDraftVersionCommand { CustomEntityDefinitionCode = BookingCustomEntityDefinition.DefinitionCode, CustomEntityId = command.Id, Title = booking.MakeTitle(), Publish = true, Model = booking }; await DomainRepository.CustomEntities().Versions().UpdateDraftAsync(updateCmd); await scope.CompleteAsync(); } }
public async Task <UpdateCustomEntityDraftVersionCommand> ExecuteAsync(GetPatchableCommandByIdQuery <UpdateCustomEntityDraftVersionCommand> query, IExecutionContext executionContext) { var dbResult = await _dbContext .CustomEntityVersions .Include(v => v.CustomEntity) .AsNoTracking() .FilterActive() .FilterByCustomEntityId(query.Id) .Where(v => v.WorkFlowStatusId == (int)WorkFlowStatus.Draft) .SingleOrDefaultAsync(); if (dbResult == null) { return(null); } _permissionValidationService.EnforceCustomEntityPermission <CustomEntityReadPermission>(dbResult.CustomEntity.CustomEntityDefinitionCode, executionContext.UserContext); var command = new UpdateCustomEntityDraftVersionCommand() { CustomEntityDefinitionCode = dbResult.CustomEntity.CustomEntityDefinitionCode, CustomEntityId = dbResult.CustomEntityId, Title = dbResult.Title }; var definitionQuery = new GetCustomEntityDefinitionSummaryByCodeQuery(command.CustomEntityDefinitionCode); var definition = await _queryExecutor.ExecuteAsync(definitionQuery, executionContext); EntityNotFoundException.ThrowIfNull(definition, command.CustomEntityDefinitionCode); command.Model = (ICustomEntityDataModel)_dbUnstructuredDataSerializer.Deserialize(dbResult.SerializedData, definition.DataModelType); return(command); }
public async Task <IEnumerable <CustomEntityVersionSummary> > ExecuteAsync(GetCustomEntityVersionSummariesByCustomEntityIdQuery query, IExecutionContext executionContext) { var definitionCode = await _dbContext .CustomEntities .AsNoTracking() .Where(c => c.CustomEntityId == query.CustomEntityId) .Select(c => c.CustomEntityDefinitionCode) .FirstOrDefaultAsync(); if (definitionCode == null) { return(null); } _permissionValidationService.EnforceCustomEntityPermission <CustomEntityReadPermission>(definitionCode); var versions = await Query(query.CustomEntityId).ToListAsync(); if (versions == null) { return(null); } return(versions); }
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(DeleteCustomEntityCommand command, IExecutionContext executionContext) { var customEntity = await _dbContext .CustomEntities .SingleOrDefaultAsync(p => p.CustomEntityId == command.CustomEntityId); if (customEntity != null) { _permissionValidationService.EnforceCustomEntityPermission <CustomEntityDeletePermission>(customEntity.CustomEntityDefinitionCode, executionContext.UserContext); using (var scope = _transactionScopeFactory.Create(_dbContext)) { await _commandExecutor.ExecuteAsync(new DeleteUnstructuredDataDependenciesCommand(customEntity.CustomEntityDefinitionCode, customEntity.CustomEntityId), executionContext); _dbContext.CustomEntities.Remove(customEntity); await _dbContext.SaveChangesAsync(); await _customEntityStoredProcedures.UpdatePublishStatusQueryLookupAsync(command.CustomEntityId); scope.QueueCompletionTask(() => OnTransactionComplete(command, customEntity)); await scope.CompleteAsync(); } } }
public async Task ExecuteAsync(CancelBookingCommand command, IExecutionContext executionContext) { PermissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdatePermission>(BookingCustomEntityDefinition.DefinitionCode, executionContext.UserContext); using (var scope = DomainRepository.Transactions().CreateScope()) { var booking = await BookingProvider.GetBookingById(command.Id); booking.BookingState = BookingDataModel.BookingStateType.Closed; booking.IsCancelled = true; var user = await CurrentUserProvider.GetAsync(); booking.AddLogEntry(new BookingLogEntry { Text = "Reservationen blev aflyst.", Username = user.User.GetFullName(), UserId = user.User.UserId, Timestamp = DateTime.Now }); UpdateCustomEntityDraftVersionCommand updateCmd = new UpdateCustomEntityDraftVersionCommand { CustomEntityDefinitionCode = BookingCustomEntityDefinition.DefinitionCode, CustomEntityId = command.Id, Title = booking.MakeTitle(), Publish = true, Model = booking }; await DomainRepository.CustomEntities().Versions().UpdateDraftAsync(updateCmd); await scope.CompleteAsync(); } }
private async Task <CustomEntityVersionPageBlockRenderDetails> MapAsync( CustomEntityVersionPageBlock versionBlock, string blockTypeFileName, string customEntityDefinitionCode, PublishStatusQuery publishStatus, IExecutionContext executionContext ) { _permissionValidationService.EnforceCustomEntityPermission <CustomEntityReadPermission>(customEntityDefinitionCode, executionContext.UserContext); var blockTypeQuery = new GetPageBlockTypeSummaryByIdQuery(versionBlock.PageBlockTypeId); var blockType = await _queryExecutor.ExecuteAsync(blockTypeQuery, executionContext); EntityNotFoundException.ThrowIfNull(blockType, versionBlock.PageBlockTypeId); var result = new CustomEntityVersionPageBlockRenderDetails(); result.CustomEntityVersionPageBlockId = versionBlock.CustomEntityVersionPageBlockId; result.BlockType = blockType; result.DisplayModel = await _pageVersionBlockModelMapper.MapDisplayModelAsync( blockTypeFileName, versionBlock, publishStatus, executionContext ); return(result); }
public async Task ExecuteAsync(DeleteCustomEntityVersionPageBlockCommand command, IExecutionContext executionContext) { var dbResult = await _dbContext .CustomEntityVersionPageBlocks .Where(m => m.CustomEntityVersionPageBlockId == command.CustomEntityVersionPageBlockId) .Select(m => new { Block = m, CustomEntityId = m.CustomEntityVersion.CustomEntityId, CustomEntityDefinitionCode = m.CustomEntityVersion.CustomEntity.CustomEntityDefinitionCode, WorkFlowStatusId = m.CustomEntityVersion.WorkFlowStatusId }) .SingleOrDefaultAsync(); if (dbResult != null) { _permissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdatePermission>(dbResult.CustomEntityDefinitionCode, executionContext.UserContext); if (dbResult.WorkFlowStatusId != (int)WorkFlowStatus.Draft) { throw new NotPermittedException("Page blocks cannot be deleted unless the entity is in draft status"); } var customEntityVersionBlockId = dbResult.Block.CustomEntityVersionPageBlockId; _dbContext.CustomEntityVersionPageBlocks.Remove(dbResult.Block); await _dbContext.SaveChangesAsync(); _transactionScopeFactory.QueueCompletionTask(_dbContext, () => OnTransactionComplete(dbResult.CustomEntityDefinitionCode, dbResult.CustomEntityId, customEntityVersionBlockId)); } }
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(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(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(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 <CustomEntityDetails> ExecuteAsync(GetCustomEntityDetailsByIdQuery query, IExecutionContext executionContext) { var customEntityVersion = await Query(query.CustomEntityId).FirstOrDefaultAsync(); _permissionValidationService.EnforceCustomEntityPermission <CustomEntityReadPermission>(customEntityVersion.CustomEntity.CustomEntityDefinitionCode, executionContext.UserContext); return(await MapAsync(query, customEntityVersion, executionContext)); }
public async Task ExecuteAsync(AnonymizeBookingsCommand command, IExecutionContext executionContext) { PermissionValidationService.EnforceCustomEntityPermission <CustomEntityDeletePermission>(BookingCustomEntityDefinition.DefinitionCode, executionContext.UserContext); var query = new SearchBookingSummariesQuery { BookingState = new BookingDataModel.BookingStateType[] { BookingDataModel.BookingStateType.Closed }, Start = new DateTime(2000, 1, 1), End = DateTime.Now.AddYears(-3) }; command.AnonymizedCount = 0; foreach (KeyValuePair <int, BookingDataModel> bookingEntry in (await BookingProvider.FindBookingDataInInterval(query)).ToList()) { BookingDataModel booking = bookingEntry.Value; // Protected agains mistakes in the query by checking values again if (!booking.IsArchived && booking.BookingState == BookingDataModel.BookingStateType.Closed && booking.DepartureDate.Value.AddYears(3) < DateTime.Now) { booking.TenantName = "---"; booking.Purpose = "---"; booking.ContactName = "---"; booking.ContactEMail = "ukendt@ukendte-mailmodtagere"; booking.ContactPhone = "---"; booking.ContactAddress = "---"; booking.ContactCity = "---"; booking.Comments = "---"; foreach (var document in booking.Documents) { var deleteDocumentCommand = new DeleteDocumentCommand { Id = document.DocumentId }; await CommandExecutor.ExecuteAsync(deleteDocumentCommand); } booking.LogEntries.Clear(); booking.Documents.Clear(); booking.IsArchived = true; command.AnonymizedCount++; UpdateCustomEntityDraftVersionCommand updateCmd = new UpdateCustomEntityDraftVersionCommand { CustomEntityDefinitionCode = BookingCustomEntityDefinition.DefinitionCode, CustomEntityId = bookingEntry.Key, Title = booking.MakeTitle(), Publish = true, Model = booking }; await DomainRepository.CustomEntities().Versions().UpdateDraftAsync(updateCmd); } } }
public async Task <JsonResult> Get([FromQuery] int id) { IExecutionContext executionContext = await ExecutionContextFactory.CreateAsync(); PermissionValidationService.EnforceCustomEntityPermission <CustomEntityReadPermission>(BookingCustomEntityDefinition.DefinitionCode, executionContext.UserContext); BookingSummary booking = await BookingProvider.GetBookingSummaryById(id); return(ApiResponseHelper.SimpleQueryResponse(booking)); }
private CustomEntityRenderDetails MapCustomEntity(CustomEntityVersion dbResult) { _permissionValidationService.EnforceCustomEntityPermission <CustomEntityReadPermission>(dbResult.CustomEntity.CustomEntityDefinitionCode); var entity = Mapper.Map <CustomEntityRenderDetails>(dbResult); entity.Model = _customEntityDataModelMapper.Map(dbResult.CustomEntity.CustomEntityDefinitionCode, dbResult.SerializedData); return(entity); }
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); _permissionValidationService.EnforceCustomEntityPermission <CustomEntityPublishPermission>(definition.CustomEntityDefinitionCode, executionContext.UserContext); 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 ValidateTitleAsync(version, definition, executionContext); using (var scope = _transactionScopeFactory.Create(_dbContext)) { await UpdateUrlSlugIfRequiredAsync(version, definition, executionContext); 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 }); }
public async Task ExecuteAsync(DeleteAllBookingsCommand command, IExecutionContext executionContext) { PermissionValidationService.EnforceCustomEntityPermission <CustomEntityDeletePermission>(BookingCustomEntityDefinition.DefinitionCode, executionContext.UserContext); foreach (var booking in (await BookingProvider.FindBookingsInInterval(new SearchBookingSummariesQuery())).ToList()) { var deleteCmd = new DeleteBookingCommand { Id = booking.Id }; await CommandExecutor.ExecuteAsync(deleteCmd); } }
public async Task <IDictionary <int, CustomEntitySummary> > ExecuteAsync(GetCustomEntitySummariesByIdRangeQuery query, IExecutionContext executionContext) { var dbResults = await QueryAsync(query, executionContext); // Validation permissions var definitionCodes = dbResults.Select(r => r.CustomEntity.CustomEntityDefinitionCode); _permissionValidationService.EnforceCustomEntityPermission <CustomEntityReadPermission>(definitionCodes, executionContext.UserContext); var mappedResults = await _customEntitySummaryMapper.MapAsync(dbResults, executionContext); return(mappedResults.ToDictionary(r => r.CustomEntityId)); }
public async Task ExecuteAsync(SendBookingMailCommand command, IExecutionContext executionContext) { PermissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdatePermission>(BookingCustomEntityDefinition.DefinitionCode, executionContext.UserContext); using (var scope = DomainRepository.Transactions().CreateScope()) { BookingDataModel booking = await BookingProvider.GetBookingById(command.BookingId); await booking.AddLogEntry(CurrentUserProvider, $"Sendt: {command.Subject}."); byte[] mailBody = System.Text.Encoding.UTF8.GetBytes(command.Message); var addDcoumentCommand = new AddDocumentCommand { Title = command.Subject, MimeType = "text/html", Body = mailBody }; await CommandExecutor.ExecuteAsync(addDcoumentCommand); booking.AddDocument(command.Subject, addDcoumentCommand.OutputDocumentId); UpdateCustomEntityDraftVersionCommand updateCmd = new UpdateCustomEntityDraftVersionCommand { CustomEntityDefinitionCode = BookingCustomEntityDefinition.DefinitionCode, CustomEntityId = command.BookingId, Title = booking.MakeTitle(), Publish = true, Model = booking }; await DomainRepository.CustomEntities().Versions().UpdateDraftAsync(updateCmd); MailAddress to = new MailAddress(booking.ContactEMail, booking.ContactName); MailMessage message = new MailMessage { To = to, Subject = command.Subject, HtmlBody = command.Message }; // It is not really a good idea to contact a mail server during a transaction, but ... // 1) I really don't want the mail being registered in the database as "sent" if the mail sending fails. // 2) One can hope that the dispatcher simply adds the message to an outgoing mail queue. // (and, yes, sending mails may fail much later with an "unknown recipient" or similar) await MailDispatchService.DispatchAsync(message); await scope.CompleteAsync(); } }
public async Task ExecuteAsync(UpdateBookingCommand command, IExecutionContext executionContext) { PermissionValidationService.EnforceCustomEntityPermission <CustomEntityUpdatePermission>(BookingCustomEntityDefinition.DefinitionCode, executionContext.UserContext); using (var scope = DomainRepository.Transactions().CreateScope()) { // Verify teneant category await TenantCategoryProvider.GetTenantCategoryById(command.TenantCategoryId.Value); BookingDataModel booking = await BookingProvider.GetBookingById(command.BookingId); booking.ArrivalDate = command.ArrivalDate.Value; booking.DepartureDate = command.DepartureDate.Value; booking.OnlySelectedWeekdays = command.OnlySelectedWeekdays; booking.SelectedWeekdays = command.SelectedWeekdays; booking.TenantCategoryId = command.TenantCategoryId; booking.TenantName = command.TenantName; booking.Purpose = command.Purpose; booking.ContactName = command.ContactName; booking.ContactPhone = command.ContactPhone; booking.ContactAddress = command.ContactAddress; booking.ContactCity = command.ContactCity; booking.ContactEMail = command.ContactEMail; booking.Comments = command.Comments; booking.RentalPrice = command.RentalPrice; booking.BookingState = command.BookingState; booking.IsApproved = command.IsApproved; booking.IsCancelled = command.IsCancelled; booking.IsCheckedOut = command.IsCheckedOut; booking.IsArchived = command.IsArchived; booking.WelcomeLetterIsSent = command.WelcomeLetterIsSent; booking.Deposit = command.Deposit; booking.DepositReceived = command.DepositReceived; booking.ElectricityReadingStart = command.ElectricityReadingStart; booking.ElectricityReadingEnd = command.ElectricityReadingEnd; booking.ElectricityPriceUnit = command.ElectricityPriceUnit; UpdateCustomEntityDraftVersionCommand updateCmd = new UpdateCustomEntityDraftVersionCommand { CustomEntityDefinitionCode = BookingCustomEntityDefinition.DefinitionCode, CustomEntityId = command.BookingId, Title = booking.MakeTitle(), Publish = true, Model = booking }; await DomainRepository.CustomEntities().Versions().UpdateDraftAsync(updateCmd); await scope.CompleteAsync(); } }
public async Task <CustomEntityRenderSummary> ExecuteAsync(GetCustomEntityRenderSummaryByIdQuery query, IExecutionContext executionContext) { var dbResult = await QueryAsync(query, executionContext); if (dbResult == null) { return(null); } _permissionValidationService.EnforceCustomEntityPermission <CustomEntityReadPermission>(dbResult.CustomEntity.CustomEntityDefinitionCode, executionContext.UserContext); var result = await _customEntityRenderSummaryMapper.MapAsync(dbResult, executionContext); return(result); }
private CustomEntityVersionPageModuleRenderDetails Map(CustomEntityVersionPageModule versionModule, string moduleTypeFileName, string customEntityDefinitionCode, WorkFlowStatusQuery workflowStatus) { _permissionValidationService.EnforceCustomEntityPermission <CustomEntityReadPermission>(customEntityDefinitionCode); var moduleType = _queryExecutor.GetById <PageModuleTypeSummary>(versionModule.PageModuleTypeId); EntityNotFoundException.ThrowIfNull(moduleType, versionModule.PageModuleTypeId); var result = new CustomEntityVersionPageModuleRenderDetails(); result.CustomEntityVersionPageModuleId = versionModule.CustomEntityVersionPageModuleId; result.ModuleType = moduleType; result.DisplayModel = _pageVersionModuleModelMapper.MapDisplayModel(moduleTypeFileName, versionModule, workflowStatus); return(result); }
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 }); }
private Dictionary <int, CustomEntitySummary> Map(List <CustomEntitySummaryQueryModel> dbResults, int[] ids, IExecutionContext executionContext) { var results = new Dictionary <int, CustomEntitySummary>(dbResults.Count); var definitionCodes = dbResults.Select(r => r.CustomEntityDefinitionCode); _permissionValidationService.EnforceCustomEntityPermission <CustomEntityReadPermission>(definitionCodes, executionContext.UserContext); foreach (var dbResult in dbResults) { var entity = Mapper.Map <CustomEntitySummary>(dbResult); entity.Model = _customEntityDataModelMapper.Map(dbResult.CustomEntityDefinitionCode, dbResult.SerializedData); results.Add(entity.CustomEntityId, entity); } return(results); }
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(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 }); } }