ActivityHttpTrigger.cs 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using System;
  2. using System.IO;
  3. using System.Threading.Tasks;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.Azure.WebJobs;
  6. using Microsoft.Azure.WebJobs.Extensions.Http;
  7. using Microsoft.AspNetCore.Http;
  8. using Microsoft.Extensions.Logging;
  9. using Newtonsoft.Json;
  10. using TEAMModelOS.SDK.DI;
  11. using Azure.Cosmos;
  12. using System.Text.Json;
  13. using System.Collections.Generic;
  14. using TEAMModelOS.SDK.Models;
  15. using TEAMModelOS.SDK.Extension;
  16. namespace TEAMModelFunction
  17. {
  18. public class ActivityHttpTrigger
  19. {
  20. private readonly AzureCosmosFactory _azureCosmos;
  21. private readonly DingDing _dingDing;
  22. private readonly AzureStorageFactory _azureStorage;
  23. private readonly AzureRedisFactory _azureRedis;
  24. public ActivityHttpTrigger(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage
  25. , AzureRedisFactory azureRedis)
  26. {
  27. _azureCosmos = azureCosmos;
  28. _dingDing = dingDing;
  29. _azureStorage = azureStorage;
  30. _azureRedis = azureRedis;
  31. }
  32. [FunctionName("fix-exam-activity")]
  33. public async Task<IActionResult> ExamActivity([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,ILogger log)
  34. {
  35. log.LogInformation("C# HTTP trigger function processed a request.");
  36. string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
  37. List<string> datas = JsonConvert.DeserializeObject<List<string>>(requestBody);
  38. var client = _azureCosmos.GetCosmosClient();
  39. List<ExamInfo> exams = new List<ExamInfo>();
  40. var query = $"select * from c ";
  41. foreach (string data in datas) {
  42. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(
  43. queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Exam-{data}") }))
  44. {
  45. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  46. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  47. {
  48. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  49. {
  50. exams.Add(obj.ToObject<ExamInfo>());
  51. }
  52. }
  53. }
  54. }
  55. return new OkObjectResult(new { });
  56. }
  57. [FunctionName("fix-vote-activity")]
  58. public async Task<IActionResult> VoteActivity(
  59. [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
  60. ILogger log)
  61. {
  62. log.LogInformation("C# HTTP trigger function processed a request.1");
  63. string name = req.Query["name"];
  64. string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
  65. dynamic data = JsonConvert.DeserializeObject(requestBody);
  66. log.LogInformation($"{data}");
  67. name = name ?? data?.name;
  68. string responseMessage = string.IsNullOrEmpty(name)
  69. ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
  70. : $"Hello, {name}. This HTTP triggered function executed successfully.";
  71. return new OkObjectResult(responseMessage);
  72. }
  73. [FunctionName("fix-survey-activity")]
  74. public async Task<IActionResult> SurveyActivity(
  75. [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
  76. ILogger log)
  77. {
  78. log.LogInformation("C# HTTP trigger function processed a request.1");
  79. string name = req.Query["name"];
  80. string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
  81. dynamic data = JsonConvert.DeserializeObject(requestBody);
  82. log.LogInformation($"{data}");
  83. name = name ?? data?.name;
  84. string responseMessage = string.IsNullOrEmpty(name)
  85. ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
  86. : $"Hello, {name}. This HTTP triggered function executed successfully.";
  87. return new OkObjectResult(responseMessage);
  88. }
  89. }
  90. }