12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Azure.Cosmos;
- using System.Text.Json;
- namespace HTEXCosmosDB.Controllers
- {
- [ProducesResponseType(StatusCodes.Status200OK)]
- [ProducesResponseType(StatusCodes.Status400BadRequest)]
- [Route("index")]
- [ApiController]
- public class IndexController: ControllerBase
- {
- private readonly AzureCosmosFactory _azureCosmosFactory;
- public IndexController( AzureCosmosFactory azureCosmosFactory )
- {
- _azureCosmosFactory = azureCosmosFactory;
- }
- [HttpPost("message")]
- public async Task<IActionResult> Message(JsonElement dto)
- {
- List<Teacher> teachers = new List<Teacher>();// where c.id in( '1595321354222','1607409409')
- await foreach (var item in _azureCosmosFactory.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").
- GetItemQueryIteratorSql<Teacher>(queryText: "select value c from c", requestOptions: new Microsoft.Azure.Cosmos.QueryRequestOptions() { PartitionKey= new Microsoft.Azure.Cosmos.PartitionKey("Base") }))
- {
- teachers.Add(item);
- }
- List<JsonElement> elements = new List<JsonElement>();
- await foreach (var item in _azureCosmosFactory.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").
- GetItemQueryStreamIteratorSql(queryText: "select value c from c where c.id in( '1595321354222','1607409409222')", requestOptions: new Microsoft.Azure.Cosmos.QueryRequestOptions() { PartitionKey= new Microsoft.Azure.Cosmos.PartitionKey("Base") }))
- {
- var json = await JsonDocument.ParseAsync(item.Content);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- elements.Add(obj);
- }
- }
- }
- return Ok(new { response = teachers,elements });
- }
- }
- public class Teacher
- {
- public string id { get; set;}
- public string name { get; set; }
- public string code { get; set; }
- public string pk { get; set; }
- }
- }
|