mirror of
https://github.com/lempamo/Project-Unite.git
synced 2025-01-22 17:22:15 +00:00
User posts can be liked and disliked.
This commit is contained in:
parent
ca0d3dd81a
commit
8d7350c24a
1 changed files with 74 additions and 0 deletions
|
@ -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();
|
||||
|
|
Loading…
Reference in a new issue