From 62fc513e42ff6a52a8dbe69c0555fdf7fdb28ccc Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 7 Apr 2017 09:08:48 -0400 Subject: [PATCH] Add rating of wiki pages. --- .../Controllers/WikiControllerController.cs | 77 ++++++++++++++++++- Project-Unite/Models/WikiModels.cs | 19 +++++ Project-Unite/Views/Wiki/Index.cshtml | 19 +++++ 3 files changed, 114 insertions(+), 1 deletion(-) diff --git a/Project-Unite/Controllers/WikiControllerController.cs b/Project-Unite/Controllers/WikiControllerController.cs index 435e5bd..a8e9f26 100644 --- a/Project-Unite/Controllers/WikiControllerController.cs +++ b/Project-Unite/Controllers/WikiControllerController.cs @@ -10,13 +10,15 @@ namespace Project_Unite.Controllers { public class WikiController : Controller { - public ActionResult Index(string id = "") + public ActionResult Index(string id = "", bool triedtolikeowntopic=false) { var db = new ApplicationDbContext(); var model = new WikiViewModel(); var wikicategories = db.WikiCategories.Where(x => x.Parent == "none"); model.Categories = wikicategories; model.Page = db.WikiPages.FirstOrDefault(x => x.Id == id); + if (triedtolikeowntopic) + ViewBag.Error = "You cannot like or dislike your own wiki page."; return View(model); } @@ -74,5 +76,78 @@ namespace Project_Unite.Controllers return RedirectToAction("Index", new { id = edit.Id }); } + + [Authorize] + public ActionResult DislikePage(string id) + { + var db = new ApplicationDbContext(); + var topic = db.WikiPages.FirstOrDefault(x => x.Id == id); + var uid = User.Identity.GetUserId(); + if (topic == null) + return new HttpStatusCodeResult(404); + if (topic.EditHistory.OrderBy(x => x.EditedAt).First().UserId == User.Identity.GetUserId()) + return RedirectToAction("Index", new { id = id, triedtolikeowntopic = true }); + var like = db.Likes.Where(x => x.Topic == topic.Id).FirstOrDefault(x => x.User == uid); + if (like != null) + { + if (like.IsDislike == false) + { + like.IsDislike = true; + } + else + { + db.Likes.Remove(like); + } + } + else + { + like = new Models.Like(); + like.Id = Guid.NewGuid().ToString(); + like.User = User.Identity.GetUserId(); + like.Topic = topic.Id; + like.LikedAt = DateTime.Now; + like.IsDislike = true; + db.Likes.Add(like); + } + db.SaveChanges(); + return RedirectToAction("Index", new { id = id }); + } + + [Authorize] + public ActionResult LikePage(string id) + { + var db = new ApplicationDbContext(); + var topic = db.WikiPages.FirstOrDefault(x => x.Id == id); + var uid = User.Identity.GetUserId(); + if (topic == null) + return new HttpStatusCodeResult(404); + if (topic.EditHistory.OrderBy(x=>x.EditedAt).First().UserId == User.Identity.GetUserId()) + return RedirectToAction("Index", new { id = id, triedtolikeowntopic = true }); + var like = db.Likes.Where(x => x.Topic == topic.Id).FirstOrDefault(x => x.User == uid); + if (like != null) + { + if (like.IsDislike == true) + { + like.IsDislike = false; + } + else + { + db.Likes.Remove(like); + } + } + else + { + like = new Models.Like(); + like.Id = Guid.NewGuid().ToString(); + like.User = User.Identity.GetUserId(); + like.Topic = topic.Id; + like.LikedAt = DateTime.Now; + like.IsDislike = false; + db.Likes.Add(like); + } + db.SaveChanges(); + return RedirectToAction("Index", new { id = id }); + } + } } \ No newline at end of file diff --git a/Project-Unite/Models/WikiModels.cs b/Project-Unite/Models/WikiModels.cs index a697496..29144d3 100644 --- a/Project-Unite/Models/WikiModels.cs +++ b/Project-Unite/Models/WikiModels.cs @@ -108,6 +108,25 @@ namespace Project_Unite.Models return list.ToArray(); } + + + + } + + public Like[] Likes + { + get + { + return new ApplicationDbContext().Likes.Where(x => x.Topic == this.Id && x.IsDislike == false).ToArray(); + } + } + + public Like[] Dislikes + { + get + { + return new ApplicationDbContext().Likes.Where(x => x.Topic == this.Id && x.IsDislike == true).ToArray(); + } } public string Contents { get; set; } diff --git a/Project-Unite/Views/Wiki/Index.cshtml b/Project-Unite/Views/Wiki/Index.cshtml index 38cac11..737f30a 100644 --- a/Project-Unite/Views/Wiki/Index.cshtml +++ b/Project-Unite/Views/Wiki/Index.cshtml @@ -26,6 +26,15 @@ } +@if (!string.IsNullOrWhiteSpace(ViewBag.Error)) +{ +
+
+

@ViewBag.Error

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