コード例 #1
0
        public void UpdateArticle(object input)
        {
            try
            {
                ArticleRepo articleRepo = new ArticleRepo();

                // 0. Retrieve old article id so we can track which article was updated
                string oldName = articleRepo.GetArticleTitleWithId((int)Article.ID);
                // 1. Update article record in database
                articleRepo.UpdateArticle(Article, User);

                // 1.1 Track article update
                ArticleInfo info = new ArticleInfo(User, Article.Title);
                new Tracker(User).TrackUpdate <ArticleInfo>(info, oldName);

                // 2. If new file was selected overwrite it to older one
                if (SelectedFile != null)
                {
                    File.Copy(SelectedFile, Path.Combine(Environment.CurrentDirectory, "Files\\") + Article.FileName + ".pdf", true);
                }

                // 3. Copy new article properties to parent's selected article (so that the values will be updated on data grid)
                SelectedArticle.CopyByValue(Article, false, true);
                OnPropertyChanged("SelectedArticle");
            }

            catch (Exception e)
            {
                // Message = "constraint failed\r\nUNIQUE constraint failed: tblArticle.Title"
                if (e.Message.Contains("UNIQUE") && e.Message.Contains("Title"))
                {
                    _dialogService.OpenDialog(new DialogOkViewModel("Article with that name already exists", "Duplicate", DialogType.Warning));
                }
                else
                {
                    new BugTracker().Track("Data View (sub window)", "Update Article", e.Message, e.StackTrace);
                    _dialogService.OpenDialog(new DialogOkViewModel("Something went wrong.", "Error", DialogType.Error));
                }
            }

            // Close window
            (input as ICommand).Execute(null);
        }