ActivityHttpTrigger.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
  17. using TEAMModelOS.SDK.Models.Cosmos;
  18. namespace TEAMModelFunction
  19. {
  20. public class ActivityHttpTrigger
  21. {
  22. private readonly AzureCosmosFactory _azureCosmos;
  23. private readonly DingDing _dingDing;
  24. private readonly AzureStorageFactory _azureStorage;
  25. private readonly AzureRedisFactory _azureRedis;
  26. public ActivityHttpTrigger(AzureCosmosFactory azureCosmos, DingDing dingDing, AzureStorageFactory azureStorage
  27. , AzureRedisFactory azureRedis)
  28. {
  29. _azureCosmos = azureCosmos;
  30. _dingDing = dingDing;
  31. _azureStorage = azureStorage;
  32. _azureRedis = azureRedis;
  33. }
  34. [FunctionName("fix-exam-activity")]
  35. public async Task<IActionResult> ExamActivity([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,ILogger log)
  36. {
  37. log.LogInformation("C# HTTP trigger function processed a request.");
  38. string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
  39. List<string> datas = JsonConvert.DeserializeObject<List<string>>(requestBody);
  40. var client = _azureCosmos.GetCosmosClient();
  41. var query = $"select * from c ";
  42. foreach (string data in datas) {
  43. List<ExamInfo> exams = new List<ExamInfo>();
  44. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(
  45. queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Exam-{data}") }))
  46. {
  47. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  48. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  49. {
  50. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  51. {
  52. exams.Add(obj.ToObject<ExamInfo>());
  53. }
  54. }
  55. }
  56. log.LogInformation($"{exams.ToJsonString()}");
  57. foreach (var info in exams) {
  58. List<string> sub = new List<string>();
  59. foreach (ExamSubject subject in info.subjects)
  60. {
  61. sub.Add(subject.id);
  62. }
  63. ActivityData dataa;
  64. if (info.scope == "school")
  65. {
  66. dataa = new ActivityData
  67. {
  68. id = info.id,
  69. code = $"Activity-{info.school}",
  70. type = "exam",
  71. name = info.name,
  72. startTime = info.startTime,
  73. endTime = info.endTime,
  74. scode = info.code,
  75. scope = info.scope,
  76. classes = info.targetClassIds.IsNotEmpty() ? info.targetClassIds : new List<string> { "" },
  77. tmdids = new List<string> { "" },
  78. progress = "going",
  79. owner = info.school,
  80. subjects = sub
  81. };
  82. await client.GetContainer("TEAMModelOS", "School").UpsertItemAsync<ActivityData>(dataa, new Azure.Cosmos.PartitionKey(dataa.code));
  83. }
  84. else if (info.scope == "private")
  85. {
  86. dataa = new ActivityData
  87. {
  88. id = info.id,
  89. code = $"Activity-Common",
  90. type = "exam",
  91. name = info.name,
  92. startTime = info.startTime,
  93. endTime = info.endTime,
  94. scode = info.code,
  95. scope = info.scope,
  96. progress = "going",
  97. classes = info.targetClassIds.IsNotEmpty() ? info.targetClassIds : new List<string> { "" },
  98. tmdids = new List<string> { "" },
  99. owner = info.creatorId,
  100. subjects = sub
  101. };
  102. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<ActivityData>(dataa, new Azure.Cosmos.PartitionKey(dataa.code));
  103. }
  104. }
  105. }
  106. return new OkObjectResult(new { });
  107. }
  108. [FunctionName("fix-vote-activity")]
  109. public async Task<IActionResult> VoteActivity(
  110. [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
  111. ILogger log)
  112. {
  113. log.LogInformation("C# HTTP trigger function processed a request.1");
  114. string name = req.Query["name"];
  115. string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
  116. dynamic data = JsonConvert.DeserializeObject(requestBody);
  117. log.LogInformation($"{data}");
  118. name = name ?? data?.name;
  119. string responseMessage = string.IsNullOrEmpty(name)
  120. ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
  121. : $"Hello, {name}. This HTTP triggered function executed successfully.";
  122. return new OkObjectResult(responseMessage);
  123. }
  124. [FunctionName("fix-survey-activity")]
  125. public async Task<IActionResult> SurveyActivity(
  126. [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
  127. ILogger log)
  128. {
  129. log.LogInformation("C# HTTP trigger function processed a request.1");
  130. string name = req.Query["name"];
  131. string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
  132. dynamic data = JsonConvert.DeserializeObject(requestBody);
  133. log.LogInformation($"{data}");
  134. name = name ?? data?.name;
  135. string responseMessage = string.IsNullOrEmpty(name)
  136. ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
  137. : $"Hello, {name}. This HTTP triggered function executed successfully.";
  138. return new OkObjectResult(responseMessage);
  139. }
  140. }
  141. }