public async Task <bool> Consume(string message)
        {
            var followEvent = JsonConvert.DeserializeObject <FollowEvent>(message);
            var profile     = await _context.Profiles.FindAsync(followEvent.ProfileId);

            var follower = await _context.Profiles.FindAsync(followEvent.FollowerId);

            if (profile != null && follower != null)
            {
                profile.Followers ??= new List <Domain.Entities.Follow>();

                var followConnectionExist = profile.Followers.Any(x => x.Follower.Id == follower.Id);

                if (!followConnectionExist)
                {
                    var follow = new Domain.Entities.Follow
                    {
                        Id             = Guid.NewGuid(),
                        DateOfCreation = DateTime.Now,
                        Follower       = follower,
                        Profile        = profile
                    };
                    profile.Followers.Add(follow);
                    _context.Follow.Add(follow);
                    _context.Profiles.Update(profile);
                    return(await _context.SaveChangesAsync() > 0);
                }
            }

            return(false);
        }
Ejemplo n.º 2
0
        private IsTrueResponse CheckIfExistAndReturn(FollowEntity follow)
        {
            var isTrueResponse = new IsTrueResponse();

            if (follow is not null)
            {
                isTrueResponse.IsTrue = true;
            }

            return(isTrueResponse);
        }
Ejemplo n.º 3
0
        private async Task <Unit> AddToDatabaseAsync(FollowEntity follow, CancellationToken cancellationToken)
        {
            await _followRepo.AddAsync(follow, cancellationToken);

            if (await _followRepo.SaveAllAsync())
            {
                return(await Task.FromResult(Unit.Value));
            }

            throw new DatabaseException("Error occured while updating database.");
        }
Ejemplo n.º 4
0
        private async Task <Unit> RemoveFollowFromDatabaseAsync(FollowEntity follow)
        {
            await _followRepo.RemoveAsync(follow);

            if (await _followRepo.SaveAllAsync())
            {
                return(await Task.FromResult(Unit.Value));
            }

            throw new DatabaseException("Error occured while updating database.");
        }
Ejemplo n.º 5
0
        private async Task ValidateAsync(FollowEntity follow)
        {
            if (!await DoesFolloweExistAsync(follow))
            {
                throw new NotFoundException("User doesn't exist.");
            }

            if (await IsAlreadyFollowedAsync(follow))
            {
                throw new DatabaseException("You've already followed this user.");
            }
        }
Ejemplo n.º 6
0
        private FollowEntity CheckSelffollowConditionAndReturnEntity(Guid followerId, Guid followeeId)
        {
            if (followerId == followeeId)
            {
                throw new OperationForbiddenException("You cannot follow yourself.");
            }

            var follow = new FollowEntity
            {
                FolloweeId = followeeId,
                FollowerId = followerId
            };

            return(follow);
        }
        public async Task <bool> Consume(string message)
        {
            FollowEvent followEvent = JsonConvert.DeserializeObject <FollowEvent>(message);

            Domain.Entities.Profile profile = await _context.Profiles.FindAsync(followEvent.ProfileId);

            Domain.Entities.Profile follower = await _context.Profiles.FindAsync(followEvent.FollowerId);

            Domain.Entities.Follow follow = await _context.Follows.FirstOrDefaultAsync(x =>
                                                                                       x.Profile == profile && x.Follower == follower);

            if (follow != null)
            {
                _context.Follows.Remove(follow);
                return(await _context.SaveChangesAsync() > 0);
            }

            return(false);
        }
        public async Task <bool> Consume(string message)
        {
            var followEvent = JsonConvert.DeserializeObject <FollowEvent>(message);
            var profile     = await _context.Profiles.FindAsync(followEvent.ProfileId);

            var follower = await _context.Profiles.FindAsync(followEvent.FollowerId);

            var followConnectionExist = await _context.Follows.FirstOrDefaultAsync(x =>
                                                                                   x.Profile == profile && x.Follower == follower);

            if (followConnectionExist == null)
            {
                var newFollow = new Domain.Entities.Follow
                {
                    Id       = Guid.NewGuid(),
                    Profile  = profile,
                    Follower = follower
                };
                _context.Follows.Add(newFollow);
                return(await _context.SaveChangesAsync() > 0);
            }

            return(false);
        }
Ejemplo n.º 9
0
        private async Task <bool> IsAlreadyFollowedAsync(FollowEntity follow)
        {
            var followFromRepo = await _followRepo.GetSingleFollowAsync(follow.FollowerId, follow.FolloweeId);

            return(followFromRepo is not null);
        }
Ejemplo n.º 10
0
        private async Task <bool> DoesFolloweExistAsync(FollowEntity follow)
        {
            var userToBeFollowed = await _userRepo.GetSingleUserByIdAsync(follow.FolloweeId);

            return(userToBeFollowed is not null);
        }