From f7767a660c1ab2f5e98d7bf8ad0a7bc9eb801ddf Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 24 Mar 2017 12:25:20 -0400 Subject: [PATCH] You can now follow and unfollow users. --- .../Controllers/ProfilesController.cs | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/Project-Unite/Controllers/ProfilesController.cs b/Project-Unite/Controllers/ProfilesController.cs index 2b6ee53..4e5e436 100644 --- a/Project-Unite/Controllers/ProfilesController.cs +++ b/Project-Unite/Controllers/ProfilesController.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Web; using System.Web.Mvc; using Microsoft.AspNet.Identity; @@ -27,6 +28,45 @@ namespace Project_Unite.Controllers return View(user); } + public ActionResult UnfollowUser(string id) + { + var db = new ApplicationDbContext(); + if (db.Users.FirstOrDefault(x => x.Id == id) == null) + return new HttpStatusCodeResult(404); + if (!ACL.IsFollowed(User.Identity.Name, id)) + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + var uid = User.Identity.GetUserId(); + var userToFollow = db.Users.FirstOrDefault(x => x.Id == id); + var follow = db.Follows.FirstOrDefault(x=>x.Followed==userToFollow.Id&&x.Follower==uid); + if(follow != null) + db.Follows.Remove(follow); + db.SaveChanges(); + return RedirectToAction("ViewProfile", new { id = userToFollow.DisplayName }); + + + } + + + public ActionResult FollowUser(string id) + { + var db = new ApplicationDbContext(); + if (db.Users.FirstOrDefault(x => x.Id == id) == null) + return new HttpStatusCodeResult(404); + if (ACL.IsFollowed(User.Identity.Name, id)) + return new HttpStatusCodeResult(HttpStatusCode.BadRequest); + var uid = User.Identity.GetUserId(); + var userToFollow = db.Users.FirstOrDefault(x => x.Id == id); + var follow = new UserFollow(); + follow.Id = Guid.NewGuid().ToString(); + follow.Followed = id; + follow.Follower = uid; + db.Follows.Add(follow); + db.SaveChanges(); + return RedirectToAction("ViewProfile", new { id = userToFollow.DisplayName }); + + + } + [Authorize] public ActionResult DislikePost(string id) {