CosmosDBController.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using HaBookCms.AzureCosmos.CosmosDB.Interfaces;
  2. using HaBookCms.Core.Models.Contest;
  3. using Microsoft.AspNetCore.Authorization;
  4. using Microsoft.AspNetCore.Cors;
  5. using Microsoft.AspNetCore.Mvc;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Threading.Tasks;
  10. namespace HaBookCms.AzureCosmosDBTest.Controllers
  11. {
  12. [Route("api/[controller]")]
  13. [ApiController]
  14. public class CosmosDBController: ControllerBase
  15. {
  16. private IAzureCosmosDBRepository _azureCosmosDB;
  17. public CosmosDBController(IAzureCosmosDBRepository azureCosmosDB)
  18. {
  19. _azureCosmosDB = azureCosmosDB;
  20. }
  21. [HttpGet("save")]
  22. [EnableCors("any")]
  23. [AllowAnonymous]
  24. public async Task<object> SaveActivity() {
  25. Console.WriteLine();
  26. BaseActivty activty = new BaseActivty
  27. {
  28. Id = "activity.111221",
  29. Title = "报名活动",
  30. Introduce = "本次活动纯属娱乐",
  31. ActivityLink = "http://www.runoob.com/csharp/csharp-methods.html",
  32. state = "1",
  33. Models = new BaseActivty.Model[]
  34. {
  35. new BaseActivty.Model {
  36. ModelId="1",
  37. ModelName="报名",
  38. ModelStartTime="2019-01-01",
  39. ModelEndTime = "2019-01-20"
  40. },
  41. new BaseActivty.Model {
  42. ModelId="2",
  43. ModelName="上传附件",
  44. ModelStartTime="2019-01-01",
  45. ModelEndTime = "2019-01-20"
  46. },
  47. new BaseActivty.Model {
  48. ModelId="3",
  49. ModelName="专家评审",
  50. ModelStartTime="2019-01-10",
  51. ModelEndTime = "2019-01-20"
  52. },
  53. new BaseActivty.Model {
  54. ModelId="4",
  55. ModelName="成绩公示",
  56. ModelStartTime="2019-01-18",
  57. ModelEndTime = "2019-01-20"
  58. }
  59. },
  60. Children = new BaseActivty.Parts[] {
  61. new BaseActivty.Parts{
  62. PartId = "label.1",
  63. PartlLabel = "选择器",
  64. PartFiled = "sex",
  65. PartType = "select"
  66. },
  67. new BaseActivty.Parts{
  68. PartId = "label.2",
  69. PartlLabel = "单选按钮",
  70. PartFiled = "education",
  71. PartType = "checkboks"
  72. },
  73. new BaseActivty.Parts{
  74. PartId = "label.3",
  75. PartlLabel = "文本框",
  76. PartFiled = "name",
  77. PartType = "input"
  78. }
  79. }
  80. };
  81. await _azureCosmosDB.Save<BaseActivty>(activty);
  82. return activty.Id;
  83. }
  84. [HttpGet("find")]
  85. [EnableCors("any")]
  86. [AllowAnonymous]
  87. public object ExecuteSimpleQuery()
  88. {
  89. //_azureCosmosDB.FindLinq<BaseActivty>( Queryable.SingleOrDefault);
  90. // Here we find the Andersen family via its LastName
  91. //return await _azureCosmosDB.FindAll<BaseActivty>();
  92. string sql = "select * from c ";
  93. return _azureCosmosDB.FindSQL<BaseActivty>(sql);
  94. // The query is executed synchronously here, but can also be executed asynchronously via the IDocumentQuery<T> interface
  95. //Console.WriteLine("Running LINQ query...");
  96. }
  97. }
  98. }