ServiceBusTopic.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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.SDK.DI;
  10. using TEAMModelOS.SDK.Extension;
  11. namespace TEAMModelFunction
  12. {
  13. public class ServiceBusTopic
  14. {
  15. private readonly AzureCosmosFactory _azureCosmos;
  16. public ServiceBusTopic( AzureCosmosFactory azureCosmos)
  17. {
  18. _azureCosmos = azureCosmos;
  19. }
  20. [FunctionName("ServiceBusTopic")]
  21. public async Task Run([ServiceBusTrigger("test_topic_ActiveTask", "test_topic_ReciveTask", Connection = "ConnectionBusName")] string mySbMsg, ILogger log)
  22. {
  23. log.LogInformation($"C# ServiceBus topic trigger function processed message: {mySbMsg}");
  24. Dictionary<string, object> keyValuePairs = mySbMsg.ToObject<Dictionary<string, object>>();
  25. var client = _azureCosmos.GetCosmosClient();
  26. keyValuePairs.TryGetValue("id", out object id);
  27. keyValuePairs.TryGetValue("name", out object name);
  28. keyValuePairs.TryGetValue("code", out object code);
  29. keyValuePairs.TryGetValue("status", out object progress);
  30. if (name.ToString().Equals("ExamInfo")) {
  31. ExamInfo exam = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamInfo>(id.ToString(), new PartitionKey($"{code}"));
  32. exam.progress = progress.ToString();
  33. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(exam, id.ToString(), new PartitionKey($"{code}"));
  34. } else if (name.ToString().Equals("Survey")) {
  35. Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(id.ToString(), new PartitionKey($"{code}"));
  36. survey.progress = progress.ToString();
  37. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(survey, id.ToString(), new PartitionKey($"{code}"));
  38. } else if (name.ToString().Equals("Vote")) {
  39. Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(id.ToString(), new PartitionKey($"{code}"));
  40. vote.progress = progress.ToString();
  41. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(vote, id.ToString(), new PartitionKey($"{code}"));
  42. }
  43. }
  44. }
  45. }