CommentController.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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.Extension.DataResult.JsonRpcRequest;
  9. using TEAMModelOS.SDK.Extension.DataResult.JsonRpcResponse;
  10. using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
  11. using TEAMModelOS.SDK.Module.AzureCosmosDBV3;
  12. using TEAMModelOS.Service.Models;
  13. namespace TEAMModelOS.Controllers
  14. {
  15. [Route("api/[controller]")]
  16. [ApiController]
  17. public class CommentController :BaseController
  18. {
  19. private readonly IAzureCosmosDBV3Repository _cosmos;
  20. public CommentController(IAzureCosmosDBV3Repository _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<BaseJosnRPCResponse> AddComment(JosnRPCRequest<CommentsDto> request)
  31. {
  32. var id = "Comment-" + request.@params.TEAMModelId.Replace("#", "");
  33. JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
  34. List<Comment> Comment = await _cosmos.FindByDict<Comment>(new Dictionary<string, object> { { "code", request.@params.TEAMModelId }, { "id", id } });
  35. Comment comment = new Comment();
  36. if (Comment.IsEmpty())
  37. {
  38. comment.id = id;
  39. comment.code = request.@params.TEAMModelId;
  40. comment.comment.Add(request.@params.comment);
  41. Comment.Add(comment);
  42. }
  43. else {
  44. comment = Comment[0];
  45. comment.comment.Add(request.@params.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<BaseJosnRPCResponse> findComment(JosnRPCRequest<Dictionary<string, object>> request)
  57. {
  58. // request.@params.TryAdd("PartitionKey", request.lang);
  59. JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
  60. List<Comment> data = new List<Comment>();
  61. if (request.@params.Keys.Count > 0)
  62. {
  63. data = await _cosmos.FindByDict<Comment>(request.@params);
  64. }
  65. else
  66. {
  67. return builder.Error(ResponseCode.PARAMS_ERROR, "参数异常!").build();
  68. }
  69. return builder.Data(data).build();
  70. }
  71. /// <summary>
  72. /// 更新保存教师评语罐头,如果评语列表为空则删除
  73. /// </summary>
  74. /// <param name="request"></param>
  75. /// <returns></returns>
  76. [HttpPost("upsertComment")]
  77. public async Task<BaseJosnRPCResponse> UpsertComment(JosnRPCRequest<Comment> request)
  78. {
  79. // request.@params.TryAdd("PartitionKey", request.lang);
  80. JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
  81. Comment comment = null;
  82. if (request.@params.comment.Count > 0)
  83. {
  84. if (string.IsNullOrEmpty(request.@params.id)) {
  85. request.@params.id = "Comment-" + request.@params.code.Replace("#", "");
  86. }
  87. comment = await _cosmos.SaveOrUpdate<Comment>(request.@params);
  88. }
  89. else {
  90. if (!string.IsNullOrEmpty(request.@params.id))
  91. {
  92. IdPk idPk = await _cosmos.DeleteAsync<Comment>(request.@params.id, request.@params.code);
  93. }
  94. }
  95. return builder.Data(comment).build();
  96. }
  97. }
  98. }