CommentController.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using Microsoft.AspNetCore.Mvc;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using TEAMModelOS.Models;
  7. using TEAMModelOS.SDK.Context.Exception;
  8. using TEAMModelOS.SDK;
  9. using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.Service.Models;
  12. using System.Text.Json;
  13. namespace TEAMModelOS.Controllers
  14. {
  15. [Route("api/[controller]")]
  16. [ApiController]
  17. public class CommentController :BaseController
  18. {
  19. private readonly AzureCosmosFactory _cosmos;
  20. public CommentController(AzureCosmosFactory _cosmosDBV3Repository)
  21. {
  22. _cosmos = _cosmosDBV3Repository;
  23. }
  24. /// <summary>
  25. /// 添加教师评语快捷回复
  26. /// </summary>
  27. /// <param name="request"></param>
  28. /// <returns></returns>
  29. [HttpPost("addComment")]
  30. public async Task<BaseResponse> AddComment(CommentsDto request)
  31. {
  32. var id = "Comment-" + request.TEAMModelId.Replace("#", "");
  33. ResponseBuilder builder = ResponseBuilder.custom();
  34. List<Comment> Comment = await _cosmos.FindByDict<Comment>(new Dictionary<string, object> { { "code", request.TEAMModelId }, { "id", id } });
  35. Comment comment = new Comment();
  36. if (Comment.IsEmpty())
  37. {
  38. comment.id = id;
  39. comment.code = request.TEAMModelId;
  40. comment.comment.Add(request.comment);
  41. Comment.Add(comment);
  42. }
  43. else {
  44. comment = Comment[0];
  45. comment.comment.Add(request.comment);
  46. }
  47. builder.Data(await _cosmos.SaveOrUpdate(comment));
  48. return builder.build();
  49. }
  50. /// <summary>
  51. /// 查询教师评语罐头
  52. /// </summary>
  53. /// <param name="request"></param>
  54. /// <returns></returns>
  55. [HttpPost("findComment")]
  56. public async Task<BaseResponse> findComment(JsonElement request)
  57. {
  58. // request.TryAdd("PartitionKey", request.lang);
  59. ResponseBuilder builder = ResponseBuilder.custom();
  60. Dictionary<string, object> dict = new Dictionary<string, object>();
  61. var emobj = request.EnumerateObject();
  62. while (emobj.MoveNext())
  63. {
  64. dict[emobj.Current.Name] = emobj.Current.Value;
  65. }
  66. List<Comment> data = new List<Comment>();
  67. if (dict.Keys.Count > 0)
  68. {
  69. data = await _cosmos.FindByDict<Comment>(dict);
  70. }
  71. else
  72. {
  73. return builder.Error(ResponseCode.PARAMS_ERROR, "参数异常!").build();
  74. }
  75. return builder.Data(data).build();
  76. }
  77. /// <summary>
  78. /// 更新保存教师评语罐头,如果评语列表为空则删除
  79. /// </summary>
  80. /// <param name="request"></param>
  81. /// <returns></returns>
  82. [HttpPost("upsertComment")]
  83. public async Task<BaseResponse> UpsertComment(Comment request)
  84. {
  85. // request.TryAdd("PartitionKey", request.lang);
  86. ResponseBuilder builder = ResponseBuilder.custom();
  87. Comment comment = null;
  88. if (request.comment.Count > 0)
  89. {
  90. if (string.IsNullOrEmpty(request.id)) {
  91. request.id = "Comment-" + request.code.Replace("#", "");
  92. }
  93. comment = await _cosmos.SaveOrUpdate<Comment>(request);
  94. }
  95. else {
  96. if (!string.IsNullOrEmpty(request.id))
  97. {
  98. IdPk idPk = await _cosmos.DeleteAsync<Comment>(request.id, request.code);
  99. }
  100. }
  101. return builder.Data(comment).build();
  102. }
  103. }
  104. }