TestController.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Mvc;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text.Json;
  7. using System.Threading.Tasks;
  8. using TEAMModelOS.SDK.DI;
  9. using TEAMModelOS.SDK.Extension;
  10. namespace TEAMModelOS.Controllers.XTest
  11. {
  12. [Route("test")]
  13. [ApiController]
  14. public class TestController :ControllerBase
  15. {
  16. private readonly AzureStorageFactory _azureStorage;
  17. private readonly AzureRedisFactory _azureRedis;
  18. private readonly AzureCosmosFactory _azureCosmos;
  19. public TestController(AzureCosmosFactory azureCosmos, AzureRedisFactory azureRedis, AzureStorageFactory azureStorage) {
  20. _azureCosmos = azureCosmos;
  21. _azureRedis = azureRedis;
  22. _azureStorage = azureStorage;
  23. }
  24. /// <summary>
  25. /// 测试blob多线程写入同一个文件
  26. /// </summary>
  27. /// <returns></returns>
  28. [ProducesDefaultResponseType]
  29. [HttpGet("multiple-blob")]
  30. public async Task<IActionResult> MultipleBlob() {
  31. await _azureStorage.GetBlobContainerClient("hbcn").List("other");
  32. return Ok();
  33. }
  34. /// <summary>
  35. /// 测试redis通配符
  36. /// </summary>
  37. /// <param name="request"></param>
  38. /// <returns></returns>
  39. [ProducesDefaultResponseType]
  40. [HttpGet("test-redis")]
  41. public async Task<IActionResult> TestRedis( ) {
  42. var db = _azureRedis.GetRedisClient(8);
  43. //var keys = server.Keys(pattern: "Vote*", database: db.Database);
  44. //foreach (var key in keys) {
  45. // Console.WriteLine(key);
  46. //}
  47. // db.HashScanAsync()
  48. return Ok() ;
  49. }
  50. /// <summary>
  51. /// 删除
  52. /// </summary>
  53. /// <param name="request"></param>
  54. /// <returns></returns>
  55. [ProducesDefaultResponseType]
  56. //[AuthToken(Roles = "teacher")]
  57. [HttpPost("delete")]
  58. public async Task<IActionResult> Delete(JsonElement request)
  59. {
  60. try
  61. {
  62. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  63. if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
  64. var client = _azureCosmos.GetCosmosClient();
  65. List<Task> tasksFiles = new List<Task>();
  66. var query = $"select c.id from c ";
  67. if (scope.ToString().Equals("school"))
  68. {
  69. var ids = client.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") });
  70. await foreach (var id in ids)
  71. {
  72. using var json = await JsonDocument.ParseAsync(id.ContentStream);
  73. var jsonList = json.RootElement.GetProperty("Documents").EnumerateArray();
  74. //批量删除
  75. foreach (var obj in jsonList)
  76. {
  77. tasksFiles.Add(client.GetContainer("TEAMModelOS", "School").DeleteItemStreamAsync(obj.GetProperty("id").ToString(), new PartitionKey($"{code}")));
  78. }
  79. }
  80. }
  81. else if (scope.ToString().Equals("teacher")) {
  82. var ids = client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") });
  83. await foreach (var id in ids)
  84. {
  85. using var json = await JsonDocument.ParseAsync(id.ContentStream);
  86. var jsonList = json.RootElement.GetProperty("Documents").EnumerateArray();
  87. //批量删除
  88. foreach (var obj in jsonList)
  89. {
  90. //tasksFiles.Add(client.GetContainer("TEAMModelOS", "Teacher").DeleteItemStreamAsync(obj.GetProperty("id").ToString(), new PartitionKey($"{code}")));
  91. }
  92. }
  93. }
  94. await Task.WhenAll(tasksFiles);
  95. return Ok(new { code = 1 });
  96. }
  97. catch (Exception e)
  98. {
  99. return BadRequest(e.StackTrace);
  100. }
  101. }
  102. }
  103. }