public async Task<ActionResult> CreatePoll(CreatePollViewModel model) { if (ModelState.IsValid) { var poll = new Poll { Id = Guid.NewGuid(), Options = new List<string> { model.Option1, model.Option2, model.Option3 }, Title = model.Title, Votes = new Dictionary<string, int> { {model.Option1, 0}, {model.Option2, 0}, {model.Option3, 0}, } }; _repository.AddPoll(poll); await SendPollNotificationAsync(poll); return RedirectToAction("Index"); } return View(model); }
public void AddPoll(Poll poll) { lock (PollLock) { ActivePolls[poll.Id] = poll; } }
private async Task SendPollNotificationAsync(Poll poll) { var baseUri = ConfigurationManager.AppSettings["Donky.IntegrationApiRoot"]; var request = new[] { new { Audience = new { Type = "AllUsers" }, Content = new { Type = "Custom", CustomType = "Poll", Data = new { id = poll.Id, title = poll.Title, options = poll.Options } } } }; var response = await _httpClient.SendAsync(new HttpRequestMessage { RequestUri = new Uri($"{baseUri}/content/send"), Method = HttpMethod.Post, Content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json"), Headers = { { "ApiKey", ConfigurationManager.AppSettings["Donky.ApiKey"] } } }); if (!response.IsSuccessStatusCode) { string body = ""; try { body = await response.Content.ReadAsStringAsync(); } catch (Exception ex) { body = $"Could not read body: {ex.Message}"; } throw new InvalidOperationException($"Call to Donky Service failed with response code {response.StatusCode}, body {body}"); } }