public EitherAsync <Error, Unit> FollowAccountAsync(Guid followerId, Guid followeeId)
        {
            if (followerId == followeeId)
            {
                return(Errors.CannotFollowSelf);
            }

            var followeeContext = GetAccountByIdAsync(followeeId).ToAsync();
            var followerContext = GetAccountByIdAsync(followerId).ToAsync();

            return
                (followeeContext.Bind(followee =>
                                      followerContext.Bind(follower =>
                                                           IsFollowingAccount(followerId, followeeId).BindAsync <Unit>(async isFollowing =>
            {
                if (isFollowing)
                {
                    return Errors.AlreadyFollowing;
                }

                await _dbContext.Entry(followee)
                .Collection(nameof(followee.Followers))
                .LoadAsync();

                followee.Followers.Add(follower);

                await _dbContext.UpdateAsync(followee);

                return unit;
            }))));
        }
Beispiel #2
0
        // TODO: Remove code repetition
        public async Task <Either <Error, Unit> > LikePostAsync(Guid accountId, Guid postId)
        {
            var result =
                from post in GetPostByIdAsync(postId).ToAsync()
                from account in _accountService.GetAccountByIdAsync(accountId).ToAsync()
                select(post, account);

            return(await result.MatchAsync <Either <Error, Unit> >(
                       async res =>
            {
                await _dbContext.Entry(res.account)
                .Collection(nameof(res.account.Likes))
                .LoadAsync();

                if (res.account.Likes.Contains(res.post))
                {
                    return unit;
                }

                res.account.Likes.Add(res.post);
                await _dbContext.UpdateAsync(res.account);
                return unit;
            },
                       err => err
                       ));
        }