Пример #1
0
        public async Task UnfollowUserHandler(FollowUserDto request)
        {
            ClaimsPrincipal user         = _iHttpContextAccessor.HttpContext.User;
            string          followerName = user.Identity.Name.ToLower();
            string          followerID   = ClaimExtensions.GetUserId(user);
            var             following    = await _dbContext.Users.Where(p => p.Username == request.Username.ToLower()).Select(p =>
                                                                                                                              new UserEntity()
            {
                Id                  = p.Id,
                Username            = p.Username,
                Password            = p.Password,
                Email               = p.Email,
                UserFollowRelations = p.UserFollowRelations,
            }).SingleOrDefaultAsync();

            var relation = await _dbContext.UserFollowRelations.Where(p => (p.FollowerId == int.Parse(followerID) && p.FollowingId == following.Id)).Select(p =>
                                                                                                                                                            new UserFollowRelationEntity()
            {
                Id          = p.Id,
                FollowerId  = p.FollowerId,
                FollowingId = p.FollowingId,
            }).SingleOrDefaultAsync();

            _dbContext.UserFollowRelations.Remove(relation);
            await _dbContext.SaveChangesAsync();
        }
        public async Task RetweetHandler(int id)
        {
            ClaimsPrincipal user   = _iHttpContextAccessor.HttpContext.User;
            int             userID = int.Parse(ClaimExtensions.GetUserId(user));

            var tweet = await _dbContext.Tweets.Where(p => p.Id == id).Select(p =>
                                                                              new TweetEntity()
            {
                Id           = p.Id,
                Content      = p.Content,
                CreatedAt    = p.CreatedAt,
                CreatorId    = p.CreatorId,
                LikeCount    = p.LikeCount,
                RetweetCount = p.RetweetCount,
                IsRetweet    = p.IsRetweet,
            }).SingleOrDefaultAsync();

            if (tweet is null)
            {
                throw new TwitterApiException(400, "Invalid tweet id");
            }
            tweet.RetweetCount++;

            _dbContext.Tweets.Update(tweet);
            await _dbContext.SaveChangesAsync();

            var relation = new UserTweetRelationEntity();

            relation.TweetId = tweet.Id;
            relation.UserId  = userID;
            await _dbContext.UserTweetRelations.AddRangeAsync(relation);

            await _dbContext.SaveChangesAsync();

            var relationn = new TweetRetweeterRelationEntity();

            relationn.TweetId     = tweet.Id;
            relationn.RetweeterId = userID;
            await _dbContext.TweetRetweeterRelations.AddRangeAsync(relationn);

            await _dbContext.SaveChangesAsync();

            var activityLog = new ActivityLogEntity()
            {
                ActorId        = userID,
                ActorName      = user.Identity.Name,
                ActionTypeId   = (int)ActionLogEnums.Retweet,
                ActionTypeName = "Retweet",
                TargetTweetId  = tweet.Id,
                TargetUserId   = tweet.CreatorId,
                Date           = DateTime.Now,
            };

            await _dbContext.ActivityLogs.AddAsync(activityLog);

            await _dbContext.SaveChangesAsync();
        }
Пример #3
0
        public async Task <List <ResponseTweetDto> > GetHomeTweetsHandler()
        {
            ClaimsPrincipal user       = _iHttpContextAccessor.HttpContext.User;
            string          userName   = user.Identity.Name.ToLower();
            int             userID     = int.Parse(ClaimExtensions.GetUserId(user));
            var             followings = await _dbContext.UserFollowRelations.AsNoTracking().Where(p => p.FollowerId == userID).Select(p =>
                                                                                                                                       new UserFollowRelationEntity()
            {
                Id          = p.Id,
                FollowerId  = p.FollowerId,
                FollowingId = p.FollowingId,
            }).ToListAsync();

            if (followings != null && followings.Count > 0)
            {
                var list = new List <ResponseTweetDto>();
                foreach (var userf in followings)
                {
                    var tweets = await _dbContext.UserTweetRelations.AsNoTracking().Where(p => p.UserId == userf.FollowingId).Select(p =>
                                                                                                                                     new UserTweetRelationEntity()
                    {
                        Id      = p.Id,
                        UserId  = p.UserId,
                        TweetId = p.TweetId,
                    }).ToListAsync();

                    if (tweets != null && tweets.Count > 0)
                    {
                        foreach (var tweetf in tweets)
                        {
                            var tweettt = await _dbContext.Tweets.Where(p => p.Id == tweetf.TweetId).Select(p =>
                                                                                                            new ResponseTweetDto()
                            {
                                Id           = p.Id,
                                Content      = p.Content,
                                CreatedAt    = p.CreatedAt,
                                CreatorId    = p.CreatorId,
                                LikeCount    = p.LikeCount,
                                RetweetCount = p.RetweetCount,
                                IsRetweet    = p.IsRetweet,
                            }).SingleOrDefaultAsync();

                            if (tweettt.CreatorId != tweetf.UserId)
                            {
                                tweettt.IsRetweet = true;
                            }
                            list.Add(tweettt);
                        }
                    }
                    List <ResponseTweetDto> SortedList = list.OrderByDescending(o => o.CreatedAt).ToList();
                    return(SortedList);
                }
            }
            return(null);
        }
Пример #4
0
        public async Task <List <ActivityLogDto> > GetSelfLogHandler()
        {
            ClaimsPrincipal user   = _iHttpContextAccessor.HttpContext.User;
            int             userID = int.Parse(ClaimExtensions.GetUserId(user));

            var logs = await _dbContext.ActivityLogs.Where(p => p.ActorId == userID).Select(p =>
                                                                                            new ActivityLogDto()
            {
                Id             = p.Id,
                ActorId        = p.ActorId,
                ActorName      = p.ActorName,
                ActionTypeId   = p.ActionTypeId,
                ActionTypeName = p.ActionTypeName,
                TargetTweetId  = p.TargetTweetId,
                TargetUserId   = p.TargetUserId,
                Date           = p.Date,
            }).ToListAsync();

            List <ActivityLogDto> SortedList = (List <ActivityLogDto>)logs.OrderByDescending(o => o.Date).ToList();

            return(SortedList);
        }
Пример #5
0
        public async Task FollowUserHandler(FollowUserDto request)
        {
            ClaimsPrincipal user         = _iHttpContextAccessor.HttpContext.User;
            string          followerName = user.Identity.Name.ToLower();
            string          followerID   = ClaimExtensions.GetUserId(user);
            var             following    = await _dbContext.Users.Where(p => p.Username == request.Username.ToLower()).Select(p =>
                                                                                                                              new UserEntity()
            {
                Id                  = p.Id,
                Username            = p.Username,
                Password            = p.Password,
                Email               = p.Email,
                UserFollowRelations = p.UserFollowRelations,
            }).SingleOrDefaultAsync();

            var relation = new UserFollowRelationEntity();

            relation.FollowerId  = int.Parse(followerID);
            relation.FollowingId = following.Id;
            await _dbContext.UserFollowRelations.AddAsync(relation);

            await _dbContext.SaveChangesAsync();

            var activityLog = new ActivityLogEntity()
            {
                ActorId        = int.Parse(followerID),
                ActorName      = user.Identity.Name,
                ActionTypeId   = (int)ActionLogEnums.Follow,
                ActionTypeName = "Follow",
                TargetTweetId  = -1,
                TargetUserId   = following.Id,
                Date           = DateTime.Now,
            };

            await _dbContext.ActivityLogs.AddAsync(activityLog);

            await _dbContext.SaveChangesAsync();
        }
        public async Task AddTweetHandler(AddTweetDto request)
        {
            ClaimsPrincipal user      = _iHttpContextAccessor.HttpContext.User;
            int             creatorID = int.Parse(ClaimExtensions.GetUserId(user));

            var newTweet = new TweetEntity()
            {
                Content      = request.Content,
                CreatedAt    = DateTime.Now,
                CreatorId    = creatorID,
                LikeCount    = 0,
                RetweetCount = 0,
                IsRetweet    = false,
            };

            await _dbContext.Tweets.AddAsync(newTweet);

            await _dbContext.SaveChangesAsync();

            if (request.HashTags != null && request.HashTags.Count > 0)
            {
                var list = new List <TweetHashtagRelationEntity>();
                foreach (var hashtag in request.HashTags)
                {
                    var tag = await searchHashtag(hashtag);

                    if (tag is null)
                    {
                        tag            = new HashtagEntity();
                        tag.Content    = hashtag.Content.ToLower();
                        tag.UsageCount = 1;
                        await _dbContext.Hashtags.AddAsync(tag);

                        await _dbContext.SaveChangesAsync();
                    }
                    else
                    {
                        tag.UsageCount++;
                        _dbContext.Hashtags.Update(tag);
                        await _dbContext.SaveChangesAsync();
                    }

                    var relationht = new HashtagTweetRelationEntity();
                    relationht.HashtagId = tag.Id;
                    relationht.TweetId   = newTweet.Id;
                    await _dbContext.HashtagTweetRelations.AddAsync(relationht);

                    await _dbContext.SaveChangesAsync();


                    list.Add(new TweetHashtagRelationEntity()
                    {
                        TweetId   = newTweet.Id,
                        HashtagId = tag.Id,
                    });
                }
                await _dbContext.TweetHashtagRelations.AddRangeAsync(list);
            }
            await _dbContext.SaveChangesAsync();

            var relationtu = new UserTweetRelationEntity();

            relationtu.TweetId = newTweet.Id;
            relationtu.UserId  = creatorID;
            await _dbContext.UserTweetRelations.AddRangeAsync(relationtu);

            await _dbContext.SaveChangesAsync();
        }