12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- using System;
- using System.Collections.Generic;
- using System.Threading.Tasks;
- using System.Transactions;
- using GxPress.Common.Exceptions;
- using GxPress.Common.Tools;
- using GxPress.EnumConst;
- using GxPress.Request.App.Topic;
- using GxPress.Result;
- using Newtonsoft.Json;
- namespace GxPress.Service.Implement.Topic
- {
- public partial class TopicService
- {
- /// <summary>
- /// 创建话题
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public async Task<AppResultJson<Entity.Topic.Topic>> InsertTopicAsync(TopicInRequest request)
- {
- var result = new AppResultJson<Entity.Topic.Topic>();
- if (string.IsNullOrWhiteSpace(request.Title) && string.IsNullOrWhiteSpace(request.HtmlContent))
- throw new BusinessException("标题和内容必填一项!");
- try
- {
- using (TransactionScope transactionScope = new TransactionScope())
- {
- //创建话题
- var topic = new Entity.Topic.Topic
- {
- UserId = request.UserId,
- Content = request.Content,
- HtmlContent = request.HtmlContent,
- Title = request.Title,
- GroupId = request.GroupId,
- FolderId = request.FolderId,
- IsDraft = request.IsDraft
- };
- if (!string.IsNullOrEmpty(request.Content))
- {
- var contentJsonData = JsonConvert.DeserializeObject<List<ContentJsonData>>(request.Content);
- foreach (var item in contentJsonData)
- {
- item.File = StringUtils.RemoveDomain(item.File);
- if (item.Type == AllTypeConst.Text.GetHashCode())
- topic.TextContent += item.Text;
- }
- topic.Content = JsonConvert.SerializeObject(contentJsonData);
- }
- else
- {
- topic.TextContent = "[]";
- topic.Content = "[]";
- }
- var topicId = await _topicRepository.InsertAsync(topic);
- //修改话题访问量
- await _visitService.AddVisit(request.UserId, GxPress.EnumConst.AllTypeConst.Topic.GetHashCode(), topicId);
- result = new AppResultJson<Entity.Topic.Topic>
- {
- Code = StatusCodeConst.SucceedCode.GetHashCode(),
- Msg = "",
- Success = false,
- Data = await _topicRepository.GetAsync(topicId)
- };
- transactionScope.Complete();
- }
- }
- catch
- {
- return result = new AppResultJson<Entity.Topic.Topic>
- {
- Code = StatusCodeConst.ErrorCode.GetHashCode(),
- Msg = StatusCodeConst.ErrorMsg.GetDescriptionOriginal(),
- Success = false,
- Data = new Entity.Topic.Topic()
- };
- }
- return result;
- }
- }
- }
|