Пример #1
0
        public async Task <Comment> AddComment(Comment commentModel, Guid authorUser, List <int> attachmentIdList)
        {
            var post = await _context.Post
                       .Include(x => x.Board)
                       .FirstOrDefaultAsync(x => x.Id == commentModel.PostId);

            if (post == null)
            {
                throw ExceptionFactory.SoftException(ExceptionEnum.PostNotFound, $"Post {commentModel.PostId} doesn't exist");
            }

            commentModel.UserId = authorUser;

            commentModel.PostId = post.Id;
            //commentModel.Text = await _userInputService.Markdown(commentModel.Text);

            /*post.Comments.Add(commentModel);
             *
             * var insertedPost = _context.Update(post);
             *
             * await _context.SaveChangesAsync();*/

            var insertedComment = _context.AddComment(commentModel);

            if (attachmentIdList != null)
            {
                foreach (var attachmentId in attachmentIdList)
                {
                    await AttachFileToComment(insertedComment.Id, attachmentId);
                }
            }

            await _context.SaveChangesAsync();

            var createdComment = await GetComment(insertedComment.Id);

            _requestLifetimeService.SetMyId(createdComment.Id, true);
            _requestLifetimeService.SetPost(post);
            _requestLifetimeService.SetBoard(post.Board);

            _httpContext.HttpContext.Items.Add("RequestLifetime", _requestLifetimeService);

            createdComment.Text = await _userInputService.Markdown(commentModel.Text);

            await _context.Mention.AddRangeAsync(_requestLifetimeService.GetMentions());

            await _context.SaveChangesAsync();

            return(createdComment);
        }
        public async Task <Post> CreateNewPost(Post postModel, Guid authorUser, List <int> attachmentPostList)
        {
            var board = await _context.Board.FirstOrDefaultAsync(x => x.Id == postModel.BoardId);

            if (board == null)
            {
                throw ExceptionFactory.SoftException(ExceptionEnum.BoardNotFound, $"Board {postModel.BoardId} not found");
            }

            postModel.UserId = authorUser;

            postModel.Title = await _userInputService.SanitizeHtml(postModel.Title);

            var insertedPost = await _context.Post.AddAsync(postModel);

            await _context.SaveChangesAsync();

            if (attachmentPostList != null)
            {
                foreach (var attachmentId in attachmentPostList)
                {
                    await AttachFileToPost(insertedPost.Entity.Id, attachmentId);
                }
            }

            await _context.PostTop.AddAsync(new PostTop
            {
                PostId  = postModel.Id,
                Score   = 50,
                BoardId = postModel.BoardId
            });

            await _context.SaveChangesAsync();

            var createdPost = await GetPost(postModel.BoardId, postModel.Id);

            _requestLifetimeService.SetMyId(createdPost.Id, false);
            _requestLifetimeService.SetPost(createdPost);
            _requestLifetimeService.SetBoard(board);

            _httpContext.HttpContext.Items.Add("RequestLifetime", _requestLifetimeService);

            createdPost.Text = await _userInputService.Markdown(postModel.Text);

            await _context.Mention.AddRangeAsync(_requestLifetimeService.GetMentions());

            await _context.SaveChangesAsync();

            return(createdPost);
        }