CommentController.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. using TEAMModelOS.SDK.Helper.Common.StringHelper;
  14. namespace TEAMModelOS.Controllers
  15. {
  16. [Route("api/[controller]")]
  17. [ApiController]
  18. public class CommentController :BaseController
  19. {
  20. private readonly AzureCosmosFactory _azureCosmos;
  21. public CommentController(AzureCosmosFactory azureCosmos)
  22. {
  23. _azureCosmos = azureCosmos;
  24. }
  25. /// <summary>
  26. /// 添加教师评语快捷回复
  27. /// </summary>
  28. /// <param name="request"></param>
  29. /// <returns></returns>
  30. [HttpPost("addComment")]
  31. public async Task<BaseResponse> AddComment(CommentsDto request)
  32. {
  33. var id = "Comment-" + request.TEAMModelId.Replace("#", "");
  34. ResponseBuilder builder = ResponseBuilder.custom();
  35. List<Comment> Comment = await _azureCosmos.FindByDict<Comment>(new Dictionary<string, object> { { "code", request.TEAMModelId }, { "id", id } });
  36. Comment comment = new Comment();
  37. if (Comment.IsEmpty())
  38. {
  39. comment.id = id;
  40. comment.code = request.TEAMModelId;
  41. comment.comment.Add(request.comment);
  42. Comment.Add(comment);
  43. }
  44. else {
  45. comment = Comment[0];
  46. comment.comment.Add(request.comment);
  47. }
  48. builder.Data(await _azureCosmos.SaveOrUpdate(comment));
  49. return builder.build();
  50. }
  51. /// <summary>
  52. /// 查询教师评语罐头
  53. /// </summary>
  54. /// <param name="request"></param>
  55. /// <returns></returns>
  56. [HttpPost("findComment")]
  57. public async Task<BaseResponse> findComment(JsonElement request)
  58. {
  59. // request.TryAdd("PartitionKey", request.lang);
  60. ResponseBuilder builder = ResponseBuilder.custom();
  61. List<Comment> data = new List<Comment>();
  62. if (StringHelper.getKeyCount(request) > 0)
  63. {
  64. data = await _azureCosmos.FindByDict<Comment>(request);
  65. }
  66. else
  67. {
  68. return builder.Error(ResponseCode.PARAMS_ERROR, "参数异常!").build();
  69. }
  70. return builder.Data(data).build();
  71. }
  72. /// <summary>
  73. /// 更新保存教师评语罐头,如果评语列表为空则删除
  74. /// </summary>
  75. /// <param name="request"></param>
  76. /// <returns></returns>
  77. [HttpPost("upsertComment")]
  78. public async Task<BaseResponse> UpsertComment(Comment request)
  79. {
  80. // request.TryAdd("PartitionKey", request.lang);
  81. ResponseBuilder builder = ResponseBuilder.custom();
  82. Comment comment = null;
  83. if (request.comment.Count > 0)
  84. {
  85. if (string.IsNullOrEmpty(request.id)) {
  86. request.id = "Comment-" + request.code.Replace("#", "");
  87. }
  88. comment = await _azureCosmos.SaveOrUpdate<Comment>(request);
  89. }
  90. else {
  91. if (!string.IsNullOrEmpty(request.id))
  92. {
  93. IdPk idPk = await _azureCosmos.DeleteAsync<Comment>(request.id, request.code);
  94. }
  95. }
  96. return builder.Data(comment).build();
  97. }
  98. }
  99. }