예제 #1
0
        public async Task <TwitterUserViewModel> NewOrUpdatedTwitterDetails(string screenName, long?id, bool forceUpdate = false)
        {
            if (id.HasValue)
            {
                // twitter user already in db, but we don't always want to update the details
                if (forceUpdate)
                {
                    var twitterUser = await _twitterRepository.GetUser(id.Value);

                    var twitterStats = await _twitterRepository.GetStats(id.Value);

                    Auth.SetUserCredentials(consumerKey, consumerSecret, accessToken, accessTokenSecret);
                    var user = User.GetUserFromId(id.Value);

                    if (user == null)
                    {
                        // throw an error
                    }

                    twitterUser.ScreenName = user.ScreenName;
                    twitterUser.CreatedAt  = user.CreatedAt;

                    twitterStats.FriendCount   = user.FriendsCount;
                    twitterStats.FollowerCount = user.FollowersCount;

                    await _twitterRepository.UpdateUser(twitterUser);

                    await _twitterRepository.UpdateStats(twitterStats);

                    return(ModelTransformer.TwitterUserToTwitterUserViewModel(twitterUser, twitterStats));
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                Auth.SetUserCredentials(consumerKey, consumerSecret, accessToken, accessTokenSecret);
                var user = User.GetUserFromScreenName(screenName);

                if (user == null)
                {
                    // throw an error
                }

                var twitterUser = new TwitterUser
                {
                    Id         = user.Id,
                    ScreenName = user.ScreenName,
                    CreatedAt  = user.CreatedAt
                };

                var twitterStats = new TwitterStats
                {
                    Id            = user.Id,
                    FriendCount   = user.FriendsCount,
                    FollowerCount = user.FollowersCount
                };

                await _twitterRepository.AddUser(twitterUser);

                await _twitterRepository.AddStats(twitterStats);

                return(ModelTransformer.TwitterUserToTwitterUserViewModel(twitterUser, twitterStats));
            }
        }