ServiceBusTopic.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text.Json;
  4. using System.Threading.Tasks;
  5. using Azure.Cosmos;
  6. using Microsoft.Azure.WebJobs;
  7. using Microsoft.Azure.WebJobs.Host;
  8. using Microsoft.Extensions.Logging;
  9. using TEAMModelOS.Models.SchoolInfo;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Extension;
  12. namespace TEAMModelFunction
  13. {
  14. public class ServiceBusTopic
  15. {
  16. private readonly AzureCosmosFactory _azureCosmos;
  17. public ServiceBusTopic( AzureCosmosFactory azureCosmos)
  18. {
  19. _azureCosmos = azureCosmos;
  20. }
  21. [FunctionName("ServiceBusTopic")]
  22. public async Task Run([ServiceBusTrigger("test_topic_ActiveTask", "test_topic_ReciveTask", Connection = "ConnectionBusName")] string mySbMsg, ILogger log)
  23. {
  24. log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
  25. Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  26. var client = _azureCosmos.GetCosmosClient();
  27. keyValuePairs.TryGetValue("id", out object id);
  28. keyValuePairs.TryGetValue("name", out object name);
  29. keyValuePairs.TryGetValue("code", out object code);
  30. keyValuePairs.TryGetValue("status", out object progress);
  31. if (name.ToString().Equals("ExamInfo")) {
  32. ExamInfo exam = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamInfo>(id.ToString(), new PartitionKey($"{code}"));
  33. exam.progress = progress.ToString();
  34. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(exam, id.ToString(), new PartitionKey($"{code}"));
  35. /*List<object> classes = new List<object>();
  36. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: $"select value(c) from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Exam-hbcn") }))
  37. {
  38. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  39. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  40. {
  41. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  42. {
  43. classes.Add(obj.ToObject<object>());
  44. }
  45. }
  46. }*/
  47. }
  48. }
  49. }
  50. }