From 5860170c1fed2342232d7e35015b669e8b8ec471 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 7 Apr 2017 09:57:24 -0400 Subject: Add "edit page" button to wiki --- .../Controllers/WikiControllerController.cs | 44 ++++++++++++++++++++++ Project-Unite/Models/WikiModels.cs | 1 + Project-Unite/Project-Unite.csproj | 1 + Project-Unite/Views/Wiki/EditPage.cshtml | 29 ++++++++++++++ Project-Unite/Views/Wiki/Index.cshtml | 7 ++++ 5 files changed, 82 insertions(+) create mode 100644 Project-Unite/Views/Wiki/EditPage.cshtml diff --git a/Project-Unite/Controllers/WikiControllerController.cs b/Project-Unite/Controllers/WikiControllerController.cs index a8e9f26..391c57d 100644 --- a/Project-Unite/Controllers/WikiControllerController.cs +++ b/Project-Unite/Controllers/WikiControllerController.cs @@ -22,6 +22,50 @@ namespace Project_Unite.Controllers return View(model); } + [Authorize] + public ActionResult EditPage(string id) + { + var db = new ApplicationDbContext(); + var model = new AddWikiPageViewModel(); + var wiki = db.WikiPages.FirstOrDefault(x => x.Id == id); + if (wiki == null) + return new HttpStatusCodeResult(404); + model.Content = wiki.Contents; + model.Name = wiki.Name; + model.ParentId = wiki.CategoryId; + model.PageId = id; + return View(model); + } + + [Authorize] + [ValidateAntiForgeryToken] + [HttpPost] + public ActionResult EditPage(AddWikiPageViewModel model) + { + if (!ModelState.IsValid) + return View(model); + var db = new ApplicationDbContext(); + + var wiki = db.WikiPages.FirstOrDefault(x => x.Id == model.PageId); + if (wiki == null) + return new HttpStatusCodeResult(404); + var edit = new ForumPostEdit(); + edit.PreviousState = wiki.Contents; + edit.EditedAt = DateTime.Now; + edit.UserId = User.Identity.GetUserId(); + edit.Id = Guid.NewGuid().ToString(); + edit.Parent = wiki.Id; + + db.ForumPostEdits.Add(edit); + + wiki.Contents = model.Content; + wiki.CategoryId = model.ParentId; + + db.SaveChanges(); + + return RedirectToAction("Index", new { id = model.PageId }); + } + public ActionResult Random() { var db = new ApplicationDbContext(); diff --git a/Project-Unite/Models/WikiModels.cs b/Project-Unite/Models/WikiModels.cs index 29144d3..fee128f 100644 --- a/Project-Unite/Models/WikiModels.cs +++ b/Project-Unite/Models/WikiModels.cs @@ -16,6 +16,7 @@ namespace Project_Unite.Models [AllowHtml] [Required(ErrorMessage = "Please enter content for your page.")] public string Content { get; set; } + public string PageId { get; set; } } public class AddWikiCategoryViewModel diff --git a/Project-Unite/Project-Unite.csproj b/Project-Unite/Project-Unite.csproj index 42daee2..b5724bd 100644 --- a/Project-Unite/Project-Unite.csproj +++ b/Project-Unite/Project-Unite.csproj @@ -546,6 +546,7 @@ + diff --git a/Project-Unite/Views/Wiki/EditPage.cshtml b/Project-Unite/Views/Wiki/EditPage.cshtml new file mode 100644 index 0000000..6c75028 --- /dev/null +++ b/Project-Unite/Views/Wiki/EditPage.cshtml @@ -0,0 +1,29 @@ +@model Project_Unite.Models.AddWikiPageViewModel +@{ + ViewBag.Title = "Edit page"; +} + +

Edit @Model.Name

+ +@using (Html.BeginForm()) +{ + @Html.AntiForgeryToken() + @Html.HiddenFor(Model=>Model.PageId) + @Html.HiddenFor(Model=>Model.Name) + + + + + + + + + + + + + + +
Category:@Html.DropDownListFor(Model=>Model.ParentId, Model.Parents, new{@class="form-control"})
Contents:@Html.TextAreaFor(Model=>Model.Content, new{@class="form-control", rows="10"})
+} + diff --git a/Project-Unite/Views/Wiki/Index.cshtml b/Project-Unite/Views/Wiki/Index.cshtml index d3772a2..02c2845 100644 --- a/Project-Unite/Views/Wiki/Index.cshtml +++ b/Project-Unite/Views/Wiki/Index.cshtml @@ -81,6 +81,13 @@

Last edited by @Html.UserLink(edit.UserId) on @edit.EditedAt

+ if (Request.IsAuthenticated) + { + + } +

@Html.Markdown(Model.Page.Contents)

if (Request.IsAuthenticated) -- cgit v1.2.3