summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael <[email protected]>2017-04-07 09:08:48 -0400
committerMichael <[email protected]>2017-04-07 09:08:48 -0400
commit62fc513e42ff6a52a8dbe69c0555fdf7fdb28ccc (patch)
treee946a5b3ee6c9855fc3a3e97709dd4d8385506bc
parent7bc9017b0fd43eb918277319907277edc839794d (diff)
downloadproject-unite-62fc513e42ff6a52a8dbe69c0555fdf7fdb28ccc.tar.gz
project-unite-62fc513e42ff6a52a8dbe69c0555fdf7fdb28ccc.tar.bz2
project-unite-62fc513e42ff6a52a8dbe69c0555fdf7fdb28ccc.zip
Add rating of wiki pages.
-rw-r--r--Project-Unite/Controllers/WikiControllerController.cs77
-rw-r--r--Project-Unite/Models/WikiModels.cs19
-rw-r--r--Project-Unite/Views/Wiki/Index.cshtml19
3 files changed, 114 insertions, 1 deletions
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 @@
</li>
}
+@if (!string.IsNullOrWhiteSpace(ViewBag.Error))
+{
+ <div class="panel panel-danger">
+ <div class="panel-body">
+ <p><span class="glyphicon glyphicon-exclamation-sign"></span> @ViewBag.Error</p>
+ </div>
+ </div>
+}
+
@if (Request.IsAuthenticated)
{
<ul class="nav nav-tabs">
@@ -73,6 +82,16 @@
<p><strong>Last edited by @Html.UserLink(edit.UserId) on @edit.EditedAt</strong></p>
<p>@Html.Markdown(Model.Page.Contents)</p>
+
+ if (Request.IsAuthenticated)
+ {
+ <ul class="nav nav-pills">
+ <li>Was this page helpful?</li>
+ <li><a href="@Url.Action("LikePage", new { id = Model.Page.Id })"><span class="glyphicon glyphicon-check"></span> @Model.Page.Likes.Length</a></li>
+ <li><a href="@Url.Action("DislikePage", new { id = Model.Page.Id })"><span class="close"></span> @Model.Page.Dislikes.Length</a></li>
+
+ </ul>
+ }
}
else
{