public async void SaveArticle(object input = null) { _workStatus(true); await Task.Run(() => { // Regex to switch multiple spaces into one (Restricts user to enter more than one space in Title textboxes) RegexOptions options = RegexOptions.None; Regex regex = new Regex("[ ]{2,}", options); // 1. Format title Article.Title = Article.Title.Trim(); Article.Title = regex.Replace(Article.Title, " "); // 2. Add article to database new ArticleRepo().SaveArticle(Article, User); // 3. Copy selected file to root folder with the new ID-based name File.Copy(SelectedFile, Program.GetSectionFilesPath() + Article.FileName + ".pdf"); // 4. Move the selected file into "Done" subfolder string done_path = Path.GetDirectoryName(SelectedFile) + "\\Done\\"; Directory.CreateDirectory(Path.GetDirectoryName(SelectedFile) + "\\Done"); File.Move(SelectedFile, done_path + System.IO.Path.GetFileName(SelectedFile)); }); _workStatus(false); // 5. Clear article attributes and Bookmark list ClearArticleAttributesCommand.Execute(null); }
public async void SaveArticle(object input = null) { try { _workingStatus(true); await Task.Run(() => { ArticleRepo articleRepo = new ArticleRepo(); // Regex to switch multiple spaces into one (Restricts user to enter more than one space in Title textboxes) RegexOptions options = RegexOptions.None; Regex regex = new Regex("[ ]{2,}", options); // 1. Format title Article.Title = Article.Title.Trim(); Article.Title = regex.Replace(Article.Title, " "); // 2. Add article to database articleRepo.SaveArticle(Article, User); // 3. Copy selected file to root folder with the new ID-based name File.Copy(SelectedFile, Path.Combine(Environment.CurrentDirectory, "Files\\") + Article.FileName + ".pdf"); // 4.1 Retrieve id (in reality we retrieve whole article) of newly added article Article currently_added_article = articleRepo.GetArticleWithTitle(Article.Title); // 4.2 Add bookmarks foreach (Bookmark bookmark in Bookmarks) { new BookmarkRepo().AddArticleToBookmark(bookmark, currently_added_article); } // 4.3 Add references foreach (Reference reference in References) { new ReferenceRepo().AddArticleToReference(reference, currently_added_article); } // 4.4 Tracking ArticleInfo info = new ArticleInfo(User, Article.Title, Bookmarks, References); Tracker tracker = new Tracker(User); tracker.TrackCreate <ArticleInfo>(info); File.Copy(SelectedFile, tracker.GetFilesPath() + "\\" + Article.FileName + ".pdf"); // 6. Move the selected file into "Done" subfolder string done_path = Path.GetDirectoryName(SelectedFile) + "\\Done\\"; Directory.CreateDirectory(Path.GetDirectoryName(SelectedFile) + "\\Done"); File.Move(SelectedFile, done_path + System.IO.Path.GetFileName(SelectedFile)); }); // 5. Clear article attributes ClearArticleAttributesCommand.Execute(null); Bookmarks.Clear(); References.Clear(); _workingStatus(false); } catch (Exception e) { new BugTracker().Track("Data Entry", "Add article", e.Message, e.StackTrace); _dialogService.OpenDialog(new DialogOkViewModel("Something went wrong.", "Error", DialogType.Error)); } finally { _workingStatus(false); } }