Exemple #1
0
        public async Task <ActionResult> EditPost(int?id, byte[] rowVersion)
        {
            #region Bad Request (null) Check

            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            #endregion
            #region Properties

            string [] fieldsToBind     = { "Title", "Subtitle", "Body", "ModifiedOn", "RowVersion", "CommentId", "TagId", "UserId" };
            Blogpost  blogpostToUpdate = await _blogpostRepository.GetBlogpostById(id);

            byte[] rowVersionException = null;
            int?   tagIdException      = null;

            #endregion

            if (blogpostToUpdate == null)
            {
                Blogpost deletedBlogpost = new Blogpost();

                TryUpdateModel(deletedBlogpost, fieldsToBind);
                ModelState.AddModelError(string.Empty, "Unable to save changes. The Blogpost was deleted by another user.");
                return(View("~/Views/Blogpost/Edit.cshtml", deletedBlogpost));
            }

            if (TryUpdateModel(blogpostToUpdate, fieldsToBind))
            {
                try {
                    await _blogpostRepository.SetRowVersion(blogpostToUpdate, rowVersion);

                    return(RedirectToAction("Index"));
                }
                #region UpdateConcurrency Catch

                catch (DbUpdateConcurrencyException ex) {
                    // 3 lines below get both the values read from the DB and the new values entered by the user
                    DbEntityEntry    entry         = ex.Entries.Single();
                    Blogpost         clientValues  = (Blogpost)entry.Entity;
                    DbPropertyValues databaseEntry = entry.GetDatabaseValuesAsync().Result;
                    if (databaseEntry == null)
                    {
                        ModelState.AddModelError(string.Empty,
                                                 "Unable to save changes. The Blogpost was deleted by another user.");
                    }
                    else
                    {
                        Blogpost databaseValues = (Blogpost)entry.GetDatabaseValuesAsync().Result.ToObject();

                        // TODO: Make sure that the current logged on user gets inserted into the DB as the editor and the creator does not get changed

                        /*
                         * All if statements give custom error messages for each column that has DB values different from
                         *   what the user entered on the Edit page. The last if statement compares the TagId for that blogpost
                         *   and then gets the tags 'Name'
                         */
                        if (databaseValues.Title != clientValues.Title)
                        {
                            ModelState.AddModelError("Title", "Current value: " + databaseValues.Title);
                        }
                        if (databaseValues.Subtitle != clientValues.Subtitle)
                        {
                            ModelState.AddModelError("Subtitle", "Current value: " + databaseValues.Subtitle);
                        }
                        if (databaseValues.Body != clientValues.Body)
                        {
                            ModelState.AddModelError("Body", "Current value: " + databaseValues.Body);
                        }
                        if (databaseValues.TagId != clientValues.TagId)
                        {
                            // This captures the databaseValues' TagId so it can be saved below (must be done outside this catch since it needs to be awaited)
                            tagIdException = databaseValues.TagId;
                        }
                        ModelState.AddModelError(string.Empty,
                                                 "The record you attempted to edit was modified by another user "
                                                 + "after you got the original value. The edit operation was canceled and the current values in the "
                                                 + "database have been displayed. If you still want to edit this record, and overwrite the other "
                                                 + "user's changes, click the 'Save' button again. Otherwise click the 'Back to List' hyperlink.");

                        // This captures the databaseValues' RowVersion so it can be saved below (must be done outside this catch since it needs to be awaited)
                        rowVersionException = databaseValues.RowVersion;
                    }
                }

                #endregion
                #region RetryLimitExceeded Catch

                // Creating new catch for the connection resiliency stuff and the BlogConfiguration.cs class
                catch (RetryLimitExceededException /* dex */) {
                    // TODO: Log the error (uncomment dex variable name and add a line here to write a log
                    ModelState.AddModelError(string.Empty, "Unable to save changes. Try again and, if the problem persists, see the system administrator.");
                }

                #endregion
            }

            // This sets the 'TagId' of the Blogpost object to the new value retrieved from the DB, only if the rowVersionException variable was filled
            if (tagIdException != null)
            {
                ModelState.AddModelError("TagId", "Current value: " + (await _tagRepository.GetTagById(tagIdException)).Name);
            }

            // This sets the 'RowVersion' of the Blogpost object to the new value retrieved from the DB, only if the rowVersionException variable was filled
            if (rowVersionException != null)
            {
                await _blogpostRepository.SetRowVersion(blogpostToUpdate, rowVersionException);
            }

            await PopulateTagsDropDownList(blogpostToUpdate.TagId);

            return(View("~/Views/Blogpost/Edit.cshtml", blogpostToUpdate));
        }