コード例 #1
0
        public async Task <int> FollowAsync(string follower, string followed)
        {
            if (follower.Equals(followed))
            {
                return(-1);
            }
            var followedQuery = from u in context.users where u.username == followed select u;

            if (!await followedQuery.AnyAsync())
            {
                return(-2);
            }
            var followerQuery = from u in context.users where u.username == follower select u;

            if (!await followerQuery.AnyAsync())
            {
                return(-3);
            }

            var relationExists = from entry in context.follows
                                 where entry.Followed.username == followed &&
                                 entry.Follower.username == follower
                                 select entry;

            if (await relationExists.AnyAsync())
            {
                return(-4);
            }

            var newFollow = new Follow
            {
                FollowedId = (await followedQuery.FirstOrDefaultAsync()).user_id,
                FollowerId = (await followerQuery.FirstOrDefaultAsync()).user_id
            };

            await context.follows.AddAsync(newFollow);

            await context.SaveChangesAsync();

            return(0);
        }
コード例 #2
0
        public async Task <int> FollowAsync(string follower, string followed)
        {
            //Check if following self
            if (follower.Equals(followed))
            {
                return(-1);
            }

            //Check if refered users exists
            var userQuery = from u in context.users where u.username == followed || u.username == follower select u;

            if ((await userQuery.CountAsync()) != 2)
            {
                return(-2);
            }

            //Check if follow already exists
            var relationExists = from entry in context.follows
                                 where entry.Followed.username == followed &&
                                 entry.Follower.username == follower
                                 select entry;

            if (await relationExists.AnyAsync())
            {
                return(-3);
            }

            var newFollow = new Follow
            {
                FollowedId = await userQuery.Where(u => u.username == followed).Select(u => u.user_id).FirstAsync(),
                FollowerId = await userQuery.Where(u => u.username == follower).Select(u => u.user_id).FirstAsync()
            };

            await context.follows.AddAsync(newFollow);

            await context.SaveChangesAsync();

            return(0);
        }