User posts can be liked and disliked.

This commit is contained in:
Michael 2017-03-23 19:45:15 -04:00
parent ca0d3dd81a
commit 8d7350c24a

View file

@ -27,6 +27,80 @@ public ActionResult ViewProfile(string id)
return View(user);
}
[Authorize]
public ActionResult DislikePosts(string id)
{
var db = new ApplicationDbContext();
var topic = db.UserPosts.FirstOrDefault(x => x.Id == id);
var uid = User.Identity.GetUserId();
if (topic == null)
return new HttpStatusCodeResult(404);
if (topic.UserId == User.Identity.GetUserId())
return RedirectToAction("ViewProfile", new { id = ACL.UserNameRaw(uid) });
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("ViewProfile", new { id = ACL.UserNameRaw(uid) });
}
[Authorize]
public ActionResult LikePost(string id)
{
var db = new ApplicationDbContext();
var topic = db.UserPosts.FirstOrDefault(x => x.Id == id);
var uid = User.Identity.GetUserId();
if (topic == null)
return new HttpStatusCodeResult(404);
if (topic.UserId == User.Identity.GetUserId())
return RedirectToAction("ViewProfile", new { id = ACL.UserNameRaw(uid) });
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("ViewProfile", new { id = ACL.UserNameRaw(uid) });
}
public ActionResult PostContent(UserPost model)
{
var db = new ApplicationDbContext();