public async Task <IActionResult> CreateAsync([FromBody] DashboardModel dashboard) { var parameters = new Dictionary <string, object>(); parameters.Add("Method", "CreateAsync"); try { if (!ModelState.IsValid) { // Throw an error if not valid. throw new Exception("There is an error creating the dashboard."); } // Create the rule in the Twitter API. var ruleResult = await _twitterApiRuleService.CreateRuleAsync(new List <RuleEntry> { new RuleEntry { Tag = dashboard.Title, Value = string.Format("{0} {1}", dashboard.Title, "-is:reply -is:retweet -is:quote") } }); List <Dashboard> dashboards = null; foreach (var ruleEntry in ruleResult.RuleEntries) { // Convert the rules into dashboards. dashboards = dashboards ?? new List <Dashboard>(); var dashboardRecord = new Dashboard(); dashboardRecord.Title = ruleEntry.Tag; dashboardRecord.SearchQuery = ruleEntry.Value; dashboardRecord.TwitterRuleId = ruleEntry.Id; dashboardRecord = await _dashboardService.CreateAsync(dashboardRecord); dashboards.Add(dashboardRecord); // Import the tweets into the dashboard. await ImportTweetsForDashboard(dashboardRecord); } // Send the fact that a dashboard has been created to the clients connected through SignalR. await _blashHub.CreateDashboardAsync(dashboards, _hostApplicationLifetime.ApplicationStopping); // Return the created dashboards. return(Json(new { Success = true, Result = dashboards })); } catch (Exception exception) { // Returns a 500 error and logs the exception. _logger.LogWithParameters(LogLevel.Error, exception, exception.Message, parameters); Response.StatusCode = (int)HttpStatusCode.InternalServerError; return(Json(new { error = new { code = Response.StatusCode, message = exception.Message } })); } }