public ToggleUserFollowResult ToggleUserFollow(int UserId, int UserToFollowId) { ToggleUserFollowResult result = new ToggleUserFollowResult(); try { var existingData = _userFollowRepo.FindAllBy(u => u.UserId == UserId && u.FollowedUserId == UserToFollowId).FirstOrDefault(); if (existingData == null) { UserFollow follow = new UserFollow(); follow.CreationDate = DateTime.UtcNow; follow.UserId = UserId; follow.FollowedUserId = UserToFollowId; result.Followed = true; _userFollowRepo.Add(follow); } else { result.Followed = false; _userFollowRepo.Delete(existingData); } result.Result = _userFollowRepo.Save(); } catch (Exception e) { Commons.Logger.GenerateError(e, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "UserId = " + UserId + " and UserToFollowId = " + UserToFollowId); } return(result); }
public ActionResult ToggleUserFollow(int UserToFollowId) { bool _success = false; string _Error = ""; bool _UserFollowed = false; try { if (User.Identity.IsAuthenticated && UserSession.UserId != UserToFollowId) { if (UserSession.UserId > 0 && UserToFollowId > 0) { ToggleUserFollowResult result = _userFollowService.ToggleUserFollow(UserSession.UserId, UserToFollowId); if (result != null) { _success = result.Result; _UserFollowed = result.Followed; } } else { _Error = ErrorMessages.UnknownError; } } else { _Error = ErrorMessages.NotAuthorized; } } catch (Exception e) { _success = false; Logger.GenerateError(e, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "UserToFollowId = " + UserToFollowId); } return(Json(new { Result = _success, UserFollowed = _UserFollowed, Error = _Error })); }