diff --git a/Project-Unite/Controllers/WikiControllerController.cs b/Project-Unite/Controllers/WikiControllerController.cs index b49a39e..f6b09cd 100644 --- a/Project-Unite/Controllers/WikiControllerController.cs +++ b/Project-Unite/Controllers/WikiControllerController.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; +using Microsoft.AspNet.Identity; using Project_Unite.Models; namespace Project_Unite.Controllers @@ -18,5 +19,48 @@ namespace Project_Unite.Controllers model.Page = db.WikiPages.FirstOrDefault(x => x.Id == id); return View(model); } + + [Authorize] + public ActionResult AddPage() + { + return View(new AddWikiPageViewModel()); + } + + [Authorize] + [HttpPost] + [ValidateAntiForgeryToken] + public ActionResult AddPage(AddWikiPageViewModel model) + { + if (!ModelState.IsValid) + return View(model); + + var db = new ApplicationDbContext(); + var page = new WikiPage(); + page.CategoryId = model.ParentId; + page.Contents = model.Content; + page.Name = model.Name; + + string allowed = "abcdefghijklmnopqrstuvwxyz1234567890_"; + page.Id = page.Name.ToLower(); + foreach(var c in page.Id.ToCharArray()) + { + if (!allowed.Contains(c)) + page.Id = page.Id.Replace(c, '_'); + } + + var edit = new ForumPostEdit(); + edit.Id = Guid.NewGuid().ToString(); + edit.Parent = page.Id; + edit.EditedAt = DateTime.Now; + edit.EditReason = "Page created."; + edit.PreviousState = page.Contents; + edit.UserId = User.Identity.GetUserId(); + + db.ForumPostEdits.Add(edit); + db.WikiPages.Add(page); + db.SaveChanges(); + + return RedirectToAction("Index", new { id = edit.Id }); + } } } \ No newline at end of file diff --git a/Project-Unite/Models/WikiModels.cs b/Project-Unite/Models/WikiModels.cs index 8009d49..96e0858 100644 --- a/Project-Unite/Models/WikiModels.cs +++ b/Project-Unite/Models/WikiModels.cs @@ -7,6 +7,17 @@ using System.Web.Mvc; namespace Project_Unite.Models { + public class AddWikiPageViewModel : AddWikiCategoryViewModel + { + public AddWikiPageViewModel() : base() + { + Parents.Remove(Parents.FirstOrDefault(x => x.Value == "none")); + } + [AllowHtml] + [Required(ErrorMessage = "Please enter content for your page.")] + public string Content { get; set; } + } + public class AddWikiCategoryViewModel { public AddWikiCategoryViewModel() @@ -32,9 +43,9 @@ namespace Project_Unite.Models public List Parents { get; set; } - [Required(AllowEmptyStrings = false, ErrorMessage ="Please name your category.")] - [MinLength(5, ErrorMessage ="Your category's name must be at least 5 characters long.")] - [MaxLength(25, ErrorMessage ="Your category's name must be at most 25 characters long.")] + [Required(AllowEmptyStrings = false, ErrorMessage ="Please name your category/page.")] + [MinLength(5, ErrorMessage ="Your category/page's name must be at least 5 characters long.")] + [MaxLength(25, ErrorMessage ="Your category/page's name must be at most 25 characters long.")] public string Name { get; set; } diff --git a/Project-Unite/Project-Unite.csproj b/Project-Unite/Project-Unite.csproj index 3201682..42daee2 100644 --- a/Project-Unite/Project-Unite.csproj +++ b/Project-Unite/Project-Unite.csproj @@ -545,6 +545,7 @@ + diff --git a/Project-Unite/Views/Wiki/AddPage.cshtml b/Project-Unite/Views/Wiki/AddPage.cshtml new file mode 100644 index 0000000..ac7ab3b --- /dev/null +++ b/Project-Unite/Views/Wiki/AddPage.cshtml @@ -0,0 +1,37 @@ +@model Project_Unite.Models.AddWikiPageViewModel +@{ + ViewBag.Title = "Add new page"; +} + +

Add new page

+ +

Want to contribute to the wiki and add a new page? Awesome. We'll get the page up - just fill in this form.

+ +@using (Html.BeginForm()) +{ + @Html.AntiForgeryToken() +
+
+ @Html.ValidationSummary() +
+
+ + + + + + + + + + + + + + + + + + +
Page title:@Html.TextBoxFor(Model => Model.Name, new{@class="form-control"})
Category:@Html.DropDownListFor(Model => Model.ParentId, Model.Parents, new { @class = "form-control" })
Page contents:@Html.TextAreaFor(Model => Model.Content, new { @rows = "10", @class = "form-control" })
+} \ No newline at end of file