public MoreOrLessQuestion GetMoreOrLessQuestion(List <int> lstExclude) { MoreOrLessQuestion q = new MoreOrLessQuestion(); try { using (SqlConnection conn = new SqlConnection(sConn)) { conn.Open(); string sQry = string.Format("SELECT top 1 * FROM TMoreOrLessQuestions "); if (lstExclude.Count > 0) { sQry += "where QuestionKey not in ("; bool bAddComma = false; foreach (int n in lstExclude) { if (bAddComma) { sQry += ", "; } sQry += n.ToString(); bAddComma = true; } sQry += ") "; } sQry += " ORDER BY NEWID()"; using (SqlCommand cmd = new SqlCommand(sQry, conn)) { SqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { q.Id = reader["ID"].ToString(); q.QuestionKey = reader.GetInt("QuestionKey"); q.Category = reader["Category"].ToString(); q.QuestionText = reader["QuestionText"].ToString(); q.Answer = reader.GetInt("Answer"); q.RangeLo = reader.GetInt("RangeLo"); q.RangeHi = reader.GetInt("RangeHi"); } } reader.Close(); } } } catch (SqlException ex) { _logger.LogInformation(ex.Message); } return(q); }
public static async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, [SignalR(HubName = "BroadcastClientMessage")] IAsyncCollector <SignalRMessage> signalRMessages, ILogger log) { _logger = log; log.LogInformation("MoreOrLessGetQuestion."); int nSuppressMessage = await Task.Run(() => { return(99); }); string HubDeviceId = req.Query["HubDeviceId"]; string HubDeviceKey = req.Query["HubDeviceKey"]; string ExcludeKeys = req.Query["ExcludeKeys"]; // All parms must be present if (HubDeviceId == null || HubDeviceKey == null) { return(new BadRequestObjectResult(Wrapper <ApiResult> .GetWrappedError("Invalid parameters", 400))); } DataAccess da = new DataAccess(_logger); // Check Station key string sDeviceKey = da.GetStationHubDeviceKeyFromStationHubId(HubDeviceId); if (!String.Equals(sDeviceKey, HubDeviceKey, StringComparison.OrdinalIgnoreCase)) { return(new BadRequestObjectResult(Wrapper <ApiResult> .GetWrappedError("Invalid key", 400))); } List <int> lstExclude = new List <int>(); if (ExcludeKeys != null) { string[] sArray = ExcludeKeys.Split(','); foreach (string s in sArray) { int n; if (Int32.TryParse(s, out n)) { lstExclude.Add(n); } } } MoreOrLessQuestion q = da.GetMoreOrLessQuestion(lstExclude); var wrappedObject = new Wrapper <MoreOrLessQuestion>(q); wrappedObject.StatusCode = 200; return(new OkObjectResult(wrappedObject)); }