public async Task <Boolean> Follow(string userid, string followingUserid) { using (var _gorillaCtx = new MSGorillaEntities()) { UserProfile user = _gorillaCtx.UserProfiles.Find(userid); UserProfile followingUser = _gorillaCtx.UserProfiles.Find(followingUserid); if (user == null || followingUser == null) { throw new UserNotFoundException(string.Format("{0} or {1}", userid, followingUserid)); } if (_gorillaCtx.Subscriptions.Where(f => f.Userid == userid && f.FollowingUserid == followingUserid).ToList().Count > 0) { return(true); } Subscription follow = new Subscription(); follow.Userid = userid; follow.FollowingUserid = followingUserid; follow.FollowingUserDisplayName = ""; _gorillaCtx.Subscriptions.Add(follow); user.FollowingsCount++; followingUser.FollowersCount++; await _gorillaCtx.SaveChangesAsync(); return(true); } }
public async Task <Boolean> UnFollow(string userid, string followingUserid) { using (var _gorillaCtx = new MSGorillaEntities()) { Subscription f = _gorillaCtx.Subscriptions.Where(ff => ff.Userid == userid && ff.FollowingUserid == followingUserid).FirstOrDefault(); if (f != null) { _gorillaCtx.Subscriptions.Remove(f); UserProfile user = _gorillaCtx.UserProfiles.Find(userid); UserProfile followingUser = _gorillaCtx.UserProfiles.Find(followingUserid); user.FollowingsCount--; followingUser.FollowersCount--; await _gorillaCtx.SaveChangesAsync(); } return(true); } }