TopicService.Add.cs 2.9 KB

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