TopicService.Add.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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<bool> InsertTopicAsync(TopicInRequest request)
  20. {
  21. if (string.IsNullOrWhiteSpace(request.Title) && string.IsNullOrWhiteSpace(request.HtmlContent))
  22. throw new BusinessException("标题和内容必填一项!");
  23. try
  24. {
  25. using (TransactionScope transactionScope = new TransactionScope())
  26. {
  27. //创建话题
  28. var topic = new Entity.Topic.Topic
  29. {
  30. UserId = request.UserId,
  31. Content = request.Content,
  32. HtmlContent = request.HtmlContent,
  33. Title = request.Title,
  34. GroupId = request.GroupId,
  35. FolderId = request.FolderId
  36. };
  37. if (!string.IsNullOrEmpty(request.Content))
  38. {
  39. var contentJsonData = JsonConvert.DeserializeObject<List<ContentJsonData>>(request.Content);
  40. foreach (var item in contentJsonData)
  41. {
  42. item.File = StringUtils.RemoveDomain(item.File);
  43. }
  44. topic.Content = JsonConvert.SerializeObject(contentJsonData);
  45. }
  46. var topicId = await _topicRepository.InsertAsync(topic);
  47. //修改话题访问量
  48. await _visitService.AddVisit(request.UserId, GxPress.EnumConst.AllTypeConst.Topic.GetHashCode(), topicId);
  49. transactionScope.Complete();
  50. }
  51. }
  52. catch (Exception e)
  53. {
  54. throw new BusinessException(e.Message);
  55. }
  56. return true;
  57. }
  58. }
  59. }