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(AddPageDraftVersionCommand command, IExecutionContext executionContext)
        {
            var newVersionId = await _entityFrameworkSqlExecutor
                               .ExecuteCommandWithOutputAsync <int?>("Cofoundry.Page_AddDraft",
                                                                     "PageVersionId",
                                                                     new SqlParameter("PageId", command.PageId),
                                                                     new SqlParameter("CopyFromPageVersionId", command.CopyFromPageVersionId),
                                                                     new SqlParameter("CreateDate", executionContext.ExecutionDate),
                                                                     new SqlParameter("CreatorId", executionContext.UserContext.UserId)
                                                                     );

            if (!newVersionId.HasValue)
            {
                throw new UnexpectedSqlStoredProcedureResultException("Cofoundry.Page_AddDraft", "No PageId was returned.");
            }

            _pageCache.Clear(newVersionId.Value);

            // Set Ouput
            command.OutputPageVersionId = newVersionId.Value;

            await _messageAggregator.PublishAsync(new PageDraftVersionAddedMessage()
            {
                PageId        = command.PageId,
                PageVersionId = newVersionId.Value
            });
        }
Beispiel #3
0
        /// <summary>
        /// Adds a draft custom entity version, copying all page blocks and other dependencies. This
        /// query autmatically updates the CustomEntityPublishStatusQuery lookup table.
        /// </summary>
        /// <param name="customEntityId">The id of the custom entity to create the draft for.</param>
        /// <param name="copyFromCustomEntityVersionId">Optional id of the version to copy from, if null we copy from the latest published version.</param>
        /// <param name="createDate">Date to set as the create date for the new version.</param>
        /// <param name="creatorId">Id of the user who created the draft.</param>
        /// <returns>CustomEntityVersionId of the newly created draft version.</returns>
        public async Task <int> AddDraftAsync(
            int customEntityId,
            int?copyFromCustomEntityVersionId,
            DateTime createDate,
            int creatorId
            )
        {
            var newVersionId = await _entityFrameworkSqlExecutor
                               .ExecuteCommandWithOutputAsync <int?>(_dbContext,
                                                                     "Cofoundry.CustomEntity_AddDraft",
                                                                     "CustomEntityVersionId",
                                                                     new SqlParameter("CustomEntityId", customEntityId),
                                                                     new SqlParameter("CopyFromCustomEntityVersionId", copyFromCustomEntityVersionId),
                                                                     new SqlParameter("CreateDate", createDate),
                                                                     new SqlParameter("CreatorId", creatorId)
                                                                     );

            if (!newVersionId.HasValue)
            {
                throw new UnexpectedSqlStoredProcedureResultException("Cofoundry.CustomEntity_AddDraft", "No CustomEntityVersionId was returned.");
            }

            return(newVersionId.Value);
        }
Beispiel #4
0
        public async Task <int> AddDraftAsync(
            int pageId,
            int?copyFromPageVersionId,
            DateTime createDate,
            int creatorId
            )
        {
            const string SP_NAME = "Cofoundry.Page_AddDraft";

            int?newVersionId;

            try
            {
                newVersionId = await _entityFrameworkSqlExecutor
                               .ExecuteCommandWithOutputAsync <int?>(_dbContext,
                                                                     SP_NAME,
                                                                     "PageVersionId",
                                                                     new SqlParameter("PageId", pageId),
                                                                     new SqlParameter("CopyFromPageVersionId", copyFromPageVersionId),
                                                                     new SqlParameter("CreateDate", createDate),
                                                                     new SqlParameter("CreatorId", creatorId)
                                                                     );
            }
            catch (SqlException ex) when(ex.Number == StoredProcedureErrorNumbers.Page_AddDraft.DraftAlreadyExists)
            {
                throw new StoredProcedureExecutionException(ex);
            }
            catch (SqlException ex) when(ex.Number == StoredProcedureErrorNumbers.Page_AddDraft.NoVersionFoundToCopyFrom)
            {
                throw new StoredProcedureExecutionException(ex);
            }

            if (!newVersionId.HasValue)
            {
                throw new UnexpectedStoredProcedureResultException(SP_NAME, "No version id was returned.");
            }

            return(newVersionId.Value);
        }
        public async Task <int> AddIfNotExistsAsync(
            string address,
            DateTime dateNow
            )
        {
            const string SP_NAME = "Cofoundry.IPAddress_AddIfNotExists";

            var ipAddressId = await _entityFrameworkSqlExecutor
                              .ExecuteCommandWithOutputAsync <int?>(_dbContext,
                                                                    SP_NAME,
                                                                    "IPAddressId",
                                                                    new SqlParameter("@Address", address),
                                                                    new SqlParameter("@DateNow", dateNow)
                                                                    );

            if (!ipAddressId.HasValue)
            {
                throw new UnexpectedStoredProcedureResultException(SP_NAME, "No IPAddressId was returned.");
            }

            return(ipAddressId.Value);
        }
Beispiel #6
0
        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 <int> AddEmailDomainIfNotExistsAsync(
            string name,
            string uniqueName,
            DateTime dateNow
            )
        {
            const string SP_NAME = "Cofoundry.EmailDomain_AddIfNotExists";

            var emailDomainId = await _entityFrameworkSqlExecutor
                                .ExecuteCommandWithOutputAsync <int?>(_dbContext,
                                                                      SP_NAME,
                                                                      "EmailDomainId",
                                                                      new SqlParameter("Name", name),
                                                                      new SqlParameter("UniqueName", uniqueName),
                                                                      new SqlParameter("DateNow", dateNow)
                                                                      );

            if (!emailDomainId.HasValue)
            {
                throw new UnexpectedStoredProcedureResultException(SP_NAME, "No EmailDomainId was returned.");
            }

            return(emailDomainId.Value);
        }