Beispiel #1
0
        /// <summary>
        /// Re-orders a set of custom entity ids in bulk.
        /// </summary>
        /// <param name="customEntityDefinitionCode">The entity definition code to order. </param>
        /// <param name="orderedCustomEntityIds">
        /// Collection of custom entity ids in the correct order that
        /// should be applied. Any missing entity ids will be given a null ordering (last).
        /// </param>
        /// <param name="localeId">Locale to set the ordering for. Ordering can only be applied for one locale at a time.</param>
        /// <returns>A collection of ids of custom entities that were updated i.e. some entities may not have needed their ordering changed.</returns>
        public async Task <ICollection <int> > ReOrderAsync(
            string customEntityDefinitionCode,
            ICollection <int> orderedCustomEntityIds,
            int?localeId
            )
        {
            var updatedIdString = await _entityFrameworkSqlExecutor
                                  .ExecuteCommandWithOutputAsync <string>(_dbContext,
                                                                          "Cofoundry.CustomEntity_ReOrder",
                                                                          "UpdatedIds",
                                                                          new SqlParameter("CustomEntityDefinitionCode", customEntityDefinitionCode),
                                                                          new SqlParameter("CustomEntityIds", string.Join(",", orderedCustomEntityIds)),
                                                                          CreateNullableIntParameter("LocaleId", localeId)
                                                                          );

            var affectedIds = IntParser.ParseFromDelimitedString(updatedIdString).ToList();

            return(affectedIds);
        }
Beispiel #2
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);
        }