Ejemplo n.º 1
0
        public async Task <ActionResult> UpdateApiDetails([FromBody] EditApiDetailsViewModel model)
        {
            if (ModelState.IsValid)
            {
                string          userId = User.Claims.First(x => x.Type == "UserID").Value;
                ApplicationUser user   = await _userRepo.GetUserById(userId);

                APIDetails details = _contextRepo.GetAPIDetails(user);
                if (details == null)
                {
                    var apiDetails = new APIDetails()
                    {
                        User              = user,
                        ApiKey            = model.APIKey,
                        ApiSecretKey      = model.APISecretKey,
                        AccessTokenSecret = model.AccessTokenSecret,
                        AccessToken       = model.AccessToken,
                        DateUpdated       = DateTime.UtcNow
                    };
                    _contextRepo.Create(apiDetails);
                }
                else
                {
                    details.ApiKey            = model.APIKey;
                    details.ApiSecretKey      = model.APISecretKey;
                    details.AccessTokenSecret = model.AccessTokenSecret;
                    details.AccessToken       = model.AccessToken;
                    details.DateUpdated       = DateTime.UtcNow;
                    _contextRepo.Update(details);
                }

                return(Ok());
            }
            return(BadRequest());
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> PullTweets([FromBody] PullTweetViewModel model)
        {
            if (ModelState.IsValid)
            {
                var userId = User.Claims.First(x => x.Type == "UserID").Value;
                var user   = await _userRepo.GetUserById(userId);

                APIDetails details = _contextRepo.GetAPIDetails(user);
                var        tweet   = _helper.GetTweets(model, details);
                if (tweet.Any())
                {
                    string filename  = model.Title;
                    bool   fileExist = _contextRepo.FileNameExist(model.Title);
                    if (fileExist)
                    {
                        filename = model.Title + "_" + Guid.NewGuid();
                    }
                    _helper.CreateCsv(tweet, filename);
                    var pullDetails = new PullDetails()
                    {
                        User     = user,
                        Title    = model.Title,
                        Keyword  = model.Keyword,
                        FileName = filename,
                        Quantity = model.Quantity,
                        Date     = DateTime.UtcNow
                    };
                    _contextRepo.Create(pullDetails);

                    return(Ok());
                }
                return(Ok("tweet is null"));
            }
            return(BadRequest());
        }
Ejemplo n.º 3
0
        public IEnumerable <ITweet> GetTweets(PullTweetViewModel model, APIDetails details)
        {
            try
            {
                // Get a AuthenticatedUser from a specific set of credentials
                Auth.SetUserCredentials(details.ApiKey, details.ApiSecretKey, details.AccessToken, details.AccessTokenSecret);
                //var authenticatedUser = User.GetAuthenticatedUser(userCredentials);

                var from            = Convert.ToDateTime(model.From);
                var to              = Convert.ToDateTime(model.To);
                var searchParameter = new SearchTweetsParameters(model.Keyword)
                {
                    //GeoCode = new GeoCode(9.0820, 8.6753, 1, DistanceMeasure.Miles),
                    Lang = LanguageFilter.English,
                    //SearchType = SearchResultType.Popular,
                    MaximumNumberOfResults = model.Quantity,
                    //Until = new DateTime(2019, 08, 02),
                    //Since = from.Date,
                    //Until = to.Date,
                    //SinceId = 399616835892781056,
                    //MaxId = 405001488843284480,
                    //Filters = TweetSearchFilters.Images | TweetSearchFilters.Verified
                };

                IEnumerable <ITweet> tweets = Search.SearchTweets(searchParameter);
                return(tweets);
            }
            catch (Exception)
            {
                throw;
            }
        }
Ejemplo n.º 4
0
        public IPlaceTrends GetTrendsFromPlace(long woeid, APIDetails details)
        {
            try
            {
                // Get a AuthenticatedUser from a specific set of credentials
                Auth.SetUserCredentials(details.ApiKey, details.ApiSecretKey, details.AccessToken, details.AccessTokenSecret);

                var trends = Trends.GetTrendsAt(woeid);
                return(trends);
            }
            catch (Exception)
            {
                throw;
            }
        }
Ejemplo n.º 5
0
        public async Task <APIDetails> InsertStudentRecord(List <Student> students)
        {
            try
            {
                APIDetails    aPIDetails = new APIDetails();
                StringBuilder cmdString  = new StringBuilder("INSERT INTO student (name, class, mark, sex) VALUES");
                using var conn = new MySqlConnection(MySqlConnectionStringBuilder.ConnectionString);

                List <string> datarows    = new List <string>();
                int           rejectCount = 0;
                foreach (var student in students)
                {
                    // some validations
                    if (student.Mark > 100 || student.Name.Length > 50 || student.ClassNumber.Length > 50 ||
                        string.IsNullOrEmpty(GenderList.Find(x => student.Gender.Equals(x, StringComparison.OrdinalIgnoreCase))))
                    {
                        _logger.LogInformation($"Record rejected: {JsonConvert.SerializeObject(student)}");
                        rejectCount++;
                        continue;
                    }
                    datarows.Add(string.Format("('{0}','{1}', '{2}', '{3}')", MySqlHelper.EscapeString(student.Name),
                                               MySqlHelper.EscapeString(student.ClassNumber), MySqlHelper.EscapeString(student.Mark.ToString()),
                                               MySqlHelper.EscapeString(student.Gender?.ToLower())));
                }
                cmdString.Append(string.Join(",", datarows));
                cmdString.Append(";");
                await conn.OpenAsync();

                int      rowCount  = 0;
                DateTime startTime = DateTime.Now;
                using (MySqlCommand myCmd = new MySqlCommand(cmdString.ToString(), conn))
                {
                    myCmd.CommandType = CommandType.Text;
                    rowCount          = myCmd.ExecuteNonQuery();
                }
                var resTime = DateTime.Now - startTime;
                _logger.LogInformation($"Number of records rejected: {rejectCount}");
                _logger.LogInformation($"Number of records inserted: {rowCount}");
                aPIDetails.ResponseTimeinSec = resTime.TotalSeconds.ToString();
                aPIDetails.NumberOfRecords   = rowCount;
                return(aPIDetails);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Ejemplo n.º 6
0
        public async Task <ActionResult> GetCountryTrend([FromBody] TrendViewModel model)
        {
            if (ModelState.IsValid)
            {
                var userId = User.Claims.First(x => x.Type == "UserID").Value;
                var user   = await _userRepo.GetUserById(userId);

                APIDetails details  = _contextRepo.GetAPIDetails(user);
                long       newWoeid = long.Parse(model.WoeID);
                var        trends   = _helper.GetTrendsFromPlace(newWoeid, details);

                return(Ok(new
                {
                    location = trends.woeIdLocations,
                    trending = trends.Trends
                }));
            }
            return(BadRequest("Woe ID is empty"));
        }