Esempio n. 1
0
        public EntityViewModel(HashtagEntity hashtagEntity)
        {
            Model = hashtagEntity;

            ExpandedUrl = "#" + hashtagEntity.Tag;
            DisplayUrl  = "#" + hashtagEntity.Tag;

            Notice = Notice.Instance;
        }
        private static void ReplaceHashtag(TextBlock textBlock, HashtagEntity hashtag, ICommand command)
        {
            var hyperlink = new Hyperlink()
            {
                NavigateUri = new Uri(hashtag.Text, UriKind.Relative),
                Foreground  = new SolidColorBrush(Colors.DodgerBlue)
            };

            hyperlink.RequestNavigate += (sender, e) =>
            {
                command.Execute("#" + hashtag.Text);
            };
            hyperlink.Inlines.Add(new Run()
            {
                Text = "#" + hashtag.Text
            });
            textBlock.Inlines.Add(hyperlink);
        }
        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();
        }