private void Follow(long selfPerId, params long[] followingIds) { using(KoalaBlogDbContext dbContext = new KoalaBlogDbContext()) { PersonXPersonHandler pxpHandler = new PersonXPersonHandler(dbContext); foreach (var followingId in followingIds) { PersonXPerson pxp = new PersonXPerson() { FollowerID = selfPerId, FollowingID = followingId }; pxpHandler.Add(pxp); } pxpHandler.SaveChanges(); } }
/// <summary> /// 关注 /// </summary> /// <param name="followerId">关注者ID</param> /// <param name="followingId">被关注者ID</param> /// <returns></returns> public async Task<bool> FollowAsync(long followerId, long followingId) { GroupHandler groupHandler = new GroupHandler(_dbContext); PersonXPersonHandler pxpHandler = new PersonXPersonHandler(_dbContext); //1. 检查被关注人黑名单是否存在关注人,若已拉黑,则无法关注。 Group blackGroup = await groupHandler.Include(x => x.GroupMembers).SingleOrDefaultAsync(x => x.PersonID == followingId && x.Type == GroupType.BlackList); if(blackGroup != null && blackGroup.GroupMembers.Count > 0) { bool isInGroupMember = blackGroup.GroupMembers.Select(x => x.PersonID).Contains(followerId); if(isInGroupMember) { throw new DisplayableException("由于用户设置,你无法关注。"); } } //2. 检查关注人的关注名单是否已经关注了。 bool isFollow = await pxpHandler.Fetch(x => x.FollowerID == followerId && x.FollowingID == followingId).SingleOrDefaultAsync() != null; //3. 如果未关注则添加纪录。 if(!isFollow) { PersonXPerson pxp = new PersonXPerson() { FollowerID = followerId, FollowingID = followingId }; pxpHandler.Add(pxp); return await SaveChangesAsync() > 0; } return true; }