public ProductComment CreateComment(CommentCreateInDto dto, AppUser user)
        {
            var comment = mapper.Map <ProductComment>(dto);

            comment.DateOfCreation = DateTime.UtcNow;
            comment.Author         = user;
            return(comment);
        }
Exemple #2
0
        public async Task <ActionResult <ResultOutDto <Comment> > > PostComment([FromBody] CommentCreateInDto createOptions)
        {
            try
            {
                var comment = await _commentService.Create(createOptions);

                return(Ok(ResultOutDtoBuilder.Success(comment)));
            }
            catch (NotExistedException e)
            {
                return(BadRequest(ResultOutDtoBuilder.Fail <Comment>(e, "No that post.")));
            }
        }
Exemple #3
0
        public async Task <IActionResult> Add(CommentCreateInDto dto)
        {
            if (ModelState.IsValid)
            {
                var user = await userManager.GetUserAsync(this.User);

                string lastCommentorId = commentService.GetLastCommentorId(dto.ProductId);
                if (user.Id == lastCommentorId)
                {
                    TempData["OldComment"]       = dto.Comment;
                    TempData["CommentAddErrors"] = new string[] { "Not allowed To post comments in a sequence!" };
                    return(RedirectToAction("Details", "Products", new { id = dto.ProductId }));
                }
                var comment = commentService.CreateComment(dto, user);
                await commentService.SaveCommentAsync(comment);

                return(RedirectToAction("GoToProductDetails", new { productId = dto.ProductId }));
            }
            TempData["OldComment"]       = dto.Comment;
            TempData["CommentAddErrors"] = ModelState.Values.SelectMany(x => x.Errors).Select(e => e.ErrorMessage).ToArray();
            return(RedirectToAction("Details", "Products", new { id = dto.ProductId }));
        }
Exemple #4
0
        public async Task <Comment> Create(CommentCreateInDto createOptions)
        {
            if (!await _context.Posts.AnyAsync(p => p.Id == createOptions.PostId))
            {
                throw new NotExistedException();
            }
            var now     = DateTime.Now;
            var comment = new Comment()
            {
                ParentId   = createOptions.ParentId,
                CategoryId = createOptions.CategoryId,
                PostId     = createOptions.PostId,
                UserId     = createOptions.UserId,
                StatusId   = createOptions.StatusId,
                Content    = createOptions.Content,
                CreateTime = now,
                UpdateTime = now,
            };
            await _context.Comments.AddAsync(comment);

            await _context.SaveChangesAsync();

            return(comment);
        }