public async Task NewPost(NewPostDto dto) { var memberInfo = await _memberRepository.FirstOrDefaultAsync(t => t.Id == dto.MemberId); if (memberInfo == null) { throw new UserFriendlyException("请先登录账户!"); } var plateInfo = await _plateRepository.FirstOrDefaultAsync(t => t.Id == dto.PlateId); if (plateInfo == null) { throw new UserFriendlyException("请选择发帖版块!"); } if (dto.Title.Length < 5) { throw new UserFriendlyException("标题不能少于5个字"); } if (dto.Content.Length < 5) { throw new UserFriendlyException("帖子内容不能少于5个字"); } ForumPost forumPost = new ForumPost(); forumPost.MemberId = dto.MemberId; forumPost.PlateId = dto.PlateId; forumPost.Title = dto.Title; forumPost.Content = dto.Content; if (dto.Content.Length <= 100) { forumPost.Summary = dto.Content; } else { forumPost.Summary = dto.Content.Substring(0, 100); } forumPost.LastCommentTime = DateTime.Now; var postId = await _forumPostRepository.InsertAndGetIdAsync(forumPost); if (dto.Images != null && dto.Images.Count > 0) { foreach (var image in dto.Images) { ForumImage forumImage = new ForumImage(); forumImage.Type = ForumImageType.Posts; forumImage.Image = image; forumImage.OwnerId = postId; await _forumImageRepository.InsertAsync(forumImage); } } }
public async Task NewComment(NewCommentDto dto) { var memberInfo = await _memberRepository.FirstOrDefaultAsync(t => t.Id == dto.MemberId); if (memberInfo == null) { throw new UserFriendlyException("请先登录账户!"); } var postInfo = await _forumPostRepository.FirstOrDefaultAsync(t => t.Id == dto.PostId); if (postInfo == null) { throw new UserFriendlyException("回复的帖子不存在!"); } if (dto.Content.Length < 5) { throw new UserFriendlyException("回复内容不能少于5个字"); } if (dto.Content.Length > 100) { throw new UserFriendlyException("回复内容不能超过100个字"); } ForumComment forumComment = new ForumComment(); forumComment.MemberId = dto.MemberId; forumComment.ForumPostId = postInfo.Id; forumComment.Content = dto.Content; forumComment.Floor = postInfo.CommentCount + 1; postInfo.LastCommentTime = DateTime.Now; postInfo.CommentCount++; var commentId = await _forumCommentRepository.InsertAndGetIdAsync(forumComment); if (dto.Images != null && dto.Images.Count > 0) { foreach (var image in dto.Images) { ForumImage forumImage = new ForumImage(); forumImage.Type = ForumImageType.Comment; forumImage.Image = image; forumImage.OwnerId = commentId; await _forumImageRepository.InsertAsync(forumImage); } } await _forumPostRepository.UpdateAsync(postInfo); }