public IActionResult Add(string p) { PageAddCommand addCommand = new PageAddCommand { ParentVirtualPath = p }; return(View(addCommand)); }
public async Task <IActionResult> Add(PageAddCommand addCommand) { if (ModelState.IsValid) { await _pager.AddPageAsync(addCommand); return(RedirectToAction(nameof(Index)) .Notify(NotificationType.Success, $"Page \"{addCommand.Title}\" added")); } return(View(addCommand)); }
public async Task AddPageAsync(PageAddCommand command) { if (command == null) { throw new ArgumentNullException(nameof(command)); } string parentPath = _pageIOManager.GetPagesDirectory().FullName; if (!string.IsNullOrWhiteSpace(command.ParentVirtualPath)) { Location parentLocation = GetLocation(command.ParentVirtualPath); if (parentLocation == null) { throw new AppException("Parent page not found"); } parentPath = Path.Combine(parentPath, parentLocation.GetDirectoryPath()); } string[] siblingDirectoriesPath = Directory.GetDirectories(parentPath); if (siblingDirectoriesPath.Length > 0) { Regex regex = new Regex($@"^(0*)([1-9]+)\{Separator.Sequence}{command.VirtualName}", RegexOptions.Compiled | RegexOptions.IgnoreCase); string existsDirectoryPath = siblingDirectoriesPath .FirstOrDefault(x => regex.IsMatch(new DirectoryInfo(x).Name)); if (!string.IsNullOrWhiteSpace(existsDirectoryPath)) { throw new AppException($"Virtual name {command.VirtualName} already exists"); } } int sequenceNumber = 0; string lastSiblingDirectoryPath = siblingDirectoriesPath.OrderBy(x => x).LastOrDefault(); if (!string.IsNullOrWhiteSpace(lastSiblingDirectoryPath)) { Int32.TryParse(new DirectoryInfo(lastSiblingDirectoryPath).Name.Split(Separator.Sequence)[0], out sequenceNumber); } sequenceNumber++; string virtualName = (sequenceNumber < 10 ? "0" : "") + sequenceNumber.ToString() + Separator.Sequence + command.VirtualName; DirectoryInfo pageDirectoryInfo = Directory.CreateDirectory(Path.Combine(parentPath, virtualName)); await _pageIOManager.SavePage(Path.Combine(pageDirectoryInfo.FullName, Markdown.Filename), ""); await _pageIOManager.SaveMetadataAsync(pageDirectoryInfo.FullName, new PageMetadata { Title = command.Title, Description = command.Description }); }