Exemplo n.º 1
0
        public async Task Handle(EntityDeleted <BlogPost> notification, CancellationToken cancellationToken)
        {
            var urlToDelete = await _slugService.GetBySlug(notification.Entity.SeName);

            await _slugService.DeleteEntityUrl(urlToDelete);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Validate search engine name
        /// </summary>
        /// <param name="entity">Entity</param>
        /// <param name="seName">Search engine name to validate</param>
        /// <param name="name">User-friendly name used to generate sename</param>
        /// <param name="ensureNotEmpty">Ensreu that sename is not empty</param>
        /// <returns>Valid sename</returns>
        public static async Task <string> ValidateSeName <T>(this T entity, string seName, string name, bool ensureNotEmpty,
                                                             SeoSettings seoSettings, ISlugService slugService, ILanguageService languageService)
            where T : BaseEntity, ISlugEntity
        {
            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }

            //use name if sename is not specified
            if (string.IsNullOrWhiteSpace(seName) && !string.IsNullOrWhiteSpace(name))
            {
                seName = name;
            }

            //validation
            seName = GetSeName(seName, seoSettings);

            //max length
            //For long URLs we can get the following error:
            //"the specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters"
            //that's why we limit it to 200 here (consider a store URL + probably added {0}-{1} below)
            seName = CommonHelper.EnsureMaximumLength(seName, 200);

            if (string.IsNullOrWhiteSpace(seName))
            {
                if (ensureNotEmpty)
                {
                    //use entity identifier as sename if empty
                    seName = entity.Id.ToString();
                }
                else
                {
                    //return. no need for further processing
                    return(seName);
                }
            }

            //ensure this sename is not reserved yet
            var entityName = typeof(T).Name;
            var i          = 2;
            var tempSeName = seName;

            while (true)
            {
                //check whether such slug already exists (and that is not the current entity)
                var entityUrl = await slugService.GetBySlug(tempSeName);

                var reserved1 = entityUrl != null && !(entityUrl.EntityId == entity.Id && entityUrl.EntityName.Equals(entityName, StringComparison.OrdinalIgnoreCase));
                //and it's not in the list of reserved slugs
                var reserved2 = seoSettings.ReservedEntityUrlSlugs.Contains(tempSeName, StringComparer.OrdinalIgnoreCase);
                var reserved3 = (await languageService.GetAllLanguages(true)).Any(language => language.UniqueSeoCode.Equals(tempSeName, StringComparison.OrdinalIgnoreCase));
                if (!reserved1 && !reserved2 && !reserved3)
                {
                    break;
                }

                tempSeName = string.Format("{0}-{1}", seName, i);
                i++;
            }
            seName = tempSeName;

            return(seName);
        }