public async Task <int> IncrementQueryCount() { var statistics = await context.Statistics.FirstOrDefaultAsync(); if (statistics == null) { statistics = new StatisticsModel(); context.Add(statistics); await context.SaveChangesAsync(); } statistics.QueryCount += 1; context.Update(statistics); await context.SaveChangesAsync(); return(statistics.QueryCount); }
public async Task <IActionResult> Create([Bind("Key,Name,Description")] SaMiKeyModel saMiKeyModel) { if (!ModelState.IsValid) { return(View(saMiKeyModel)); } // Query SaMi for key validity var uri = configuration.GetSection("Urls").GetSection("Sensors").Value + saMiKeyModel.Key; var request = new HttpRequestMessage(HttpMethod.Get, uri); var client = clientFactory.CreateClient(); var response = await client.SendAsync(request); if (!response.IsSuccessStatusCode) { ModelState.AddModelError("Key", "Invalid key."); return(View(saMiKeyModel)); } var protector = protectionProvider.CreateProtector(purposes); saMiKeyModel.Key = protector.Protect(saMiKeyModel.Key); saMiKeyModel.ApplicationUser = await userManager.GetUserAsync(HttpContext.User); context.Add(saMiKeyModel); await context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }