public async Task <ISeoData> FindBySlugAsync(string slug, CancellationToken cancellationToken = default)
 {
     return((
                await _urlRecordRepository.FindBySlugAsync(
                    slug,
                    cancellationToken: cancellationToken
                    )
                ).ToSeoData());
 }
Exemplo n.º 2
0
        public async Task <string> ValidateSeNameAsync(Guid entityId, string entityName, string seName, string name,
                                                       bool ensureNotEmpty, CancellationToken cancellationToken = default)
        {
            //use name if sename is not specified
            if (string.IsNullOrWhiteSpace(seName) && !string.IsNullOrWhiteSpace(name))
            {
                seName = name;
            }

            //validation
            //TODO: convertNonWesternChars? & allowUnicodeCharsInUrls? should get from settings
            seName = await GetSeNameAsync(seName, true, true, cancellationToken);

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

            //ensure this sename is not reserved yet
            var i          = 2;
            var tempSeName = seName;

            while (true)
            {
                //check whether such slug already exists (and that is not the current entity)
                var urlRecord = await _urlRecordRepository.FindBySlugAsync(tempSeName, cancellationToken);

                var reserved = urlRecord != null && !(urlRecord.EntityId == entityId && urlRecord.EntityName.Equals(entityName, StringComparison.InvariantCultureIgnoreCase));

                //TODO more validation

                if (!reserved)
                {
                    break;
                }

                tempSeName = $"{seName}-{i}";
                i++;
            }

            seName = tempSeName;

            return(seName);
        }
Exemplo n.º 3
0
        public async Task <string> ToSafeSlugAsync(string slug, string entityTypeId)
        {
            var i = 2;

            while (true)
            {
                var urlRecord = await _urlRecordRepository.FindBySlugAsync(slug);

                if (urlRecord != null && urlRecord.EntityTypeId != entityTypeId)
                {
                    slug = $"{slug}-{i}";
                    i++;
                }
                else
                {
                    break;
                }
            }

            return(slug);
        }