TopicService.Add.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading.Tasks;
  4. using System.Transactions;
  5. using GxPress.Common.Exceptions;
  6. using GxPress.Common.Tools;
  7. using GxPress.EnumConst;
  8. using GxPress.Request.App.Topic;
  9. using GxPress.Result;
  10. using Newtonsoft.Json;
  11. namespace GxPress.Service.Implement.Topic
  12. {
  13. public partial class TopicService
  14. {
  15. /// <summary>
  16. /// 创建话题
  17. /// </summary>
  18. /// <param name="request"></param>
  19. /// <returns></returns>
  20. public async Task<AppResultJson<Entity.Topic.Topic>> InsertTopicAsync(TopicInRequest request)
  21. {
  22. var result = new AppResultJson<Entity.Topic.Topic>();
  23. if (string.IsNullOrWhiteSpace(request.Title) && string.IsNullOrWhiteSpace(request.HtmlContent))
  24. throw new BusinessException("标题和内容必填一项!");
  25. try
  26. {
  27. using (TransactionScope transactionScope = new TransactionScope())
  28. {
  29. //创建话题
  30. var topic = new Entity.Topic.Topic
  31. {
  32. UserId = request.UserId,
  33. Content = request.Content,
  34. HtmlContent = request.HtmlContent,
  35. Title = request.Title,
  36. GroupId = request.GroupId,
  37. FolderId = request.FolderId,
  38. IsDraft = request.IsDraft
  39. };
  40. if (!string.IsNullOrEmpty(request.Content))
  41. {
  42. var contentJsonData = JsonConvert.DeserializeObject<List<ContentJsonData>>(request.Content);
  43. foreach (var item in contentJsonData)
  44. {
  45. item.File = StringUtils.RemoveDomain(item.File);
  46. if (item.Type == AllTypeConst.Text.GetHashCode())
  47. topic.TextContent += item.Text;
  48. }
  49. topic.Content = JsonConvert.SerializeObject(contentJsonData);
  50. }
  51. else
  52. {
  53. topic.TextContent = "[]";
  54. topic.Content = "[]";
  55. }
  56. var topicId = await _topicRepository.InsertAsync(topic);
  57. //修改话题访问量
  58. await _visitService.AddVisit(request.UserId, GxPress.EnumConst.AllTypeConst.Topic.GetHashCode(), topicId);
  59. result = new AppResultJson<Entity.Topic.Topic>
  60. {
  61. Code = StatusCodeConst.SucceedCode.GetHashCode(),
  62. Msg = "",
  63. Success = false,
  64. Data = await _topicRepository.GetAsync(topicId)
  65. };
  66. transactionScope.Complete();
  67. }
  68. }
  69. catch
  70. {
  71. return result = new AppResultJson<Entity.Topic.Topic>
  72. {
  73. Code = StatusCodeConst.ErrorCode.GetHashCode(),
  74. Msg = StatusCodeConst.ErrorMsg.GetDescriptionOriginal(),
  75. Success = false,
  76. Data = new Entity.Topic.Topic()
  77. };
  78. }
  79. return result;
  80. }
  81. }
  82. }