From 62fc513e42ff6a52a8dbe69c0555fdf7fdb28ccc Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 7 Apr 2017 09:08:48 -0400 Subject: Add rating of wiki pages. --- .../Controllers/WikiControllerController.cs | 77 +++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) (limited to 'Project-Unite/Controllers/WikiControllerController.cs') 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 -- cgit v1.2.3