Exemplo n.º 1
0
        public async Task <IActionResult> AddComment(
            int postId,
            [FromBody] PostCommentInputModel inputModel
            )
        {
            try
            {
                context.Comments.Add(new PostComment
                {
                    Content         = inputModel.Content,
                    CommentedDate   = DateTime.UtcNow,
                    LastUpdatedDate = DateTime.UtcNow,
                    PostID          = postId,
                    UserID          = HttpContext.Session.GetInt32("USER_LOGIN_KEY").Value
                });

                await context.SaveChangesAsync();

                return(Ok());
            }
            catch (DbUpdateException e)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
        }
        public IActionResult AddComment([FromBody] PostCommentInputModel input)
        {
            var cookie = this.HttpContext.Request.Headers["X-User-Token"];
            var userId = this.GetUserId(cookie);

            var result = this.postService.AddComment(input.PostId, input.Comment, userId);

            if (!result)
            {
                return(BadRequest());
            }

            return(Ok("Successfully added comment"));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> AddComment(PostCommentInputModel input)
        {
            if (this.ModelState.IsValid)
            {
                var userId    = this.User.FindFirstValue(ClaimTypes.NameIdentifier);
                var profileId = this.profilesService.GetId(userId);

                var viewModel = await this.commentsService.AddCommentAsync <PostCommentInputModel>(profileId, input);

                return(new JsonResult(viewModel));
            }

            return(this.BadRequest());
        }
Exemplo n.º 4
0
        public async Task <PostCommentViewModel> AddCommentAsync <T>(int profileId, PostCommentInputModel input)
        {
            var comment = new Comment()
            {
                PostId    = input.PostId,
                ProfileId = profileId,
                Text      = input.Text,
            };

            await this.commentsRepository.AddAsync(comment);

            await this.commentsRepository.SaveChangesAsync();

            return(this.commentsRepository.AllAsNoTracking().Where(x => x.Id == comment.Id).To <PostCommentViewModel>().FirstOrDefault());
        }
Exemplo n.º 5
0
        [HttpPut("comment/{id}")] //Edit Post
        public async Task <IActionResult> UpdateCommentByID([FromBody] PostCommentInputModel inputModel, int id)
        {
            var foundComment = context.Comments.SingleOrDefault(c => c.CommentID == id);

            if (foundComment == null)
            {
                return(BadRequest("Invalid comment Id"));
            }


            foundComment.Content = inputModel.Content;

            await context.SaveChangesAsync();

            return(Ok(foundComment));
        }
Exemplo n.º 6
0
        public async Task <ActionResult <PostCommentResponceModel> > AddComment(PostCommentInputModel input)
        {
            if (this.ModelState.IsValid)
            {
                var userId    = this.User.FindFirstValue(ClaimTypes.NameIdentifier);
                var profileId = this.profilesService.GetId(userId);

                var sanitizer = new HtmlSanitizer();
                input.Text = sanitizer.Sanitize(input.Text);

                var viewModel = await this.commentsService.AddCommentAsync <PostCommentResponceModel>(profileId, input);

                return(viewModel);
            }

            return(this.BadRequest());
        }