private bool ValidateRow(ImportRedirectRow row) { if (!UrlValidation.IsRelativeUrl(row.FromUrl)) { return(false); } if (!UrlValidation.ValidFromUrl(row.FromUrl)) { return(false); } if (!UrlValidation.IsWithinLengthLimit(row.FromUrl)) { return(false); } if (!UrlValidation.ValidToUrl(row.ToUrl)) { return(false); } if (!UrlValidation.IsWithinLengthLimit(row.ToUrl)) { return(false); } return(true); }
private async Task <bool> ImportRowAsync(ImportRedirectRow row) { if (!ValidateRow(row)) { return(false); } var existing = await _session.Query <ContentItem>() .With <RedirectPartIndex>(x => x.Url == row.FromUrl) .ListAsync(); ContentItem redirectItem = null; var newItem = false; if (existing.Any()) { redirectItem = existing.FirstOrDefault(x => x.Published); } if (redirectItem == null) { redirectItem = await _contentManager.NewAsync(Constants.RedirectContentType); redirectItem.Owner = _httpContextAccessor.HttpContext.User.Identity.Name; redirectItem.CreatedUtc = DateTime.UtcNow; newItem = true; } if (redirectItem == null) { return(false); } var redirectPart = redirectItem.Get <RedirectPart>(nameof(RedirectPart)); if (redirectPart == null) { return(false); } redirectItem.Author = _httpContextAccessor.HttpContext.User.Identity.Name; redirectItem.DisplayText = row.Title; redirectPart.FromUrl = row.FromUrl?.Trim(); redirectPart.ToUrl = row.ToUrl?.Trim(); redirectPart.IsPermanent = true; redirectPart.Apply(); redirectItem.Apply(nameof(RedirectPart), redirectPart); ContentExtensions.Apply(redirectItem, redirectItem); if (newItem) { await _contentManager.CreateAsync(redirectItem); } else { await _contentManager.UpdateAsync(redirectItem); } await _contentManager.PublishAsync(redirectItem); return(true); }