Add rating of wiki pages.

This commit is contained in:
Michael 2017-04-07 09:08:48 -04:00
parent 7bc9017b0f
commit 62fc513e42
3 changed files with 114 additions and 1 deletions

View file

@ -10,13 +10,15 @@ namespace Project_Unite.Controllers
{ {
public class WikiController : Controller public class WikiController : Controller
{ {
public ActionResult Index(string id = "") public ActionResult Index(string id = "", bool triedtolikeowntopic=false)
{ {
var db = new ApplicationDbContext(); var db = new ApplicationDbContext();
var model = new WikiViewModel(); var model = new WikiViewModel();
var wikicategories = db.WikiCategories.Where(x => x.Parent == "none"); var wikicategories = db.WikiCategories.Where(x => x.Parent == "none");
model.Categories = wikicategories; model.Categories = wikicategories;
model.Page = db.WikiPages.FirstOrDefault(x => x.Id == id); 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); return View(model);
} }
@ -74,5 +76,78 @@ namespace Project_Unite.Controllers
return RedirectToAction("Index", new { id = edit.Id }); 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 });
}
} }
} }

View file

@ -108,6 +108,25 @@ namespace Project_Unite.Models
return list.ToArray(); 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; } public string Contents { get; set; }

View file

@ -26,6 +26,15 @@
</li> </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) @if (Request.IsAuthenticated)
{ {
<ul class="nav nav-tabs"> <ul class="nav nav-tabs">
@ -73,6 +82,16 @@
<p><strong>Last edited by @Html.UserLink(edit.UserId) on @edit.EditedAt</strong></p> <p><strong>Last edited by @Html.UserLink(edit.UserId) on @edit.EditedAt</strong></p>
<p>@Html.Markdown(Model.Page.Contents)</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 else
{ {