Exemplo n.º 1
0
        /// <summary>
        /// Remove a 'Retweet' from the Tweet if it's already exists
        /// or add new if it's not
        /// </summary>
        /// <param name="tweetId"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task <int> UpdateRetweets(int tweetId, TwitterCopyUser user)
        {
            var tweet = await _tweetRepository.GetTweetWithRetweetsAsync(tweetId);

            var retweet = new Retweet
            {
                Tweet = tweet,
                User  = user
            };

            var dupe = tweet.Retweets.FirstOrDefault(x => x.TweetId == tweet.Id && x.UserId == user.Id);

            if (dupe == null)
            {
                // If no duplicate was found
                // Add new retweet to the database
                _retweetRepository.Add(retweet);
            }
            else
            {
                // If duplicate was found
                // Delete dupe instead of retweer because
                // retweet doesn't have Id value
                _retweetRepository.Delete(dupe);
            }

            await _retweetRepository.SaveAsync();

            return(tweet.Retweets.Count);
        }