private TwitterUserStatistic CreateOrUpdateUserStatistic(string twitter_nick, string topPositiveWordsJson, string topNegativeWordsJson)
        {
            var existedStatistic = _context.TwitterUserStatistics.FirstOrDefault(stat => stat.TwitterNick.Equals(twitter_nick));

            if (existedStatistic == null)
            {
                existedStatistic = new TwitterUserStatistic()
                {
                    TwitterNick          = twitter_nick,
                    LastUpdated          = DateTime.Now,
                    TopPositiveWordsJson = topPositiveWordsJson,
                    TopNegativeWordsJson = topNegativeWordsJson
                };

                _context.TwitterUserStatistics.Add(existedStatistic);
            }
            else
            {
                existedStatistic.LastUpdated          = DateTime.Now;
                existedStatistic.TopNegativeWordsJson = topPositiveWordsJson;
                existedStatistic.TopNegativeWordsJson = topNegativeWordsJson;

                _context.TwitterUserStatistics.Update(existedStatistic);
            }

            _context.SaveChanges();

            return(existedStatistic);
        }
        public ActionResult <PredictionDTO> GetUserPrediction(string twitter_nick, int count)
        {
            if (string.IsNullOrEmpty(twitter_nick) || count == 0)
            {
                return(BadRequest());
            }

            var analytics = UserRecentAnalytics(twitter_nick);

            if (analytics != null)
            {
                var response = new PredictionDTO()
                {
                    ActivityChartPath    = $"/output/activity_{twitter_nick}.png",
                    PieChartPath         = $"/output/piechart_{twitter_nick}.png",
                    LastModified         = analytics.LastUpdated,
                    TopPositiveWordsJson = analytics.TopPositiveWordsJson,
                    TopNegativeWordsJson = analytics.TopNegativeWordsJson
                };

                return(Ok(response));
            }
            else
            {
                try
                {
                    string link = $"http://localhost:5000/api/analytics?twitter_nick={twitter_nick}&count={count}";
                    string predictionResponse = HttpSender.SendHttpRequest(new Uri(link), "POST");

                    dynamic parsedResponse = JsonConvert.DeserializeObject(predictionResponse);

                    TwitterUserStatistic statistic = CreateOrUpdateUserStatistic(
                        twitter_nick,
                        double.Parse(parsedResponse["good"].ToString()),
                        double.Parse(parsedResponse["bad"].ToString()));

                    var response = new PredictionDTO()
                    {
                        ActivityChartPath    = $"/output/activity_{twitter_nick}.png",
                        PieChartPath         = $"/output/activity_{twitter_nick}.png",
                        LastModified         = statistic.LastUpdated,
                        TopPositiveWordsJson = analytics.TopPositiveWordsJson,
                        TopNegativeWordsJson = analytics.TopNegativeWordsJson
                    };

                    return(Ok(response));
                }
                catch (Exception ex)
                {
                    return(BadRequest(ex.Message));
                }
            }
        }