public async Task <IActionResult> CreateComment(CreateCommentDto createCommentDto) { if (ModelState.IsValid) { var newComment = new Comment { ParentId = createCommentDto.ParentId, FullName = createCommentDto.FullName, Email = createCommentDto.Email, Content = createCommentDto.Content, }; await _db.Comments.AddAsync(newComment); await _db.SaveChangesAsync(); if (createCommentDto.ParentId.HasValue && createCommentDto.ParentId.Value > 0) { Comment parentComment = await _db.Comments.FindAsync(createCommentDto.ParentId); List <int> childrenIdsOfParent = new(); if (string.IsNullOrEmpty(parentComment.ChildrenIds)) { childrenIdsOfParent.Add(newComment.CommentId); } else { List <int> currentChildrenIdsOfParent = parentComment.ChildrenIds.CommaSeparatedStringToIntList(); childrenIdsOfParent.AddRange(currentChildrenIdsOfParent); childrenIdsOfParent.Add(newComment.CommentId); } parentComment.ChildrenIds = string.Join <int>(",", childrenIdsOfParent); await _db.SaveChangesAsync(); } return(RedirectToAction(nameof(Index))); } // TODO: Throw some error return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> CreateComment(CommentCreationDto commentCDto) { if (ModelState.IsValid) { var newComment = new Comment { ParentId = commentCDto.ReplyToCommentId, FullName = commentCDto.FullName, Email = commentCDto.Email, Content = commentCDto.Content, DateCreated = DateTimeOffset.Now, DateModified = DateTimeOffset.Now }; await _db.Comments.AddAsync(newComment); await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } // TODO: Throw some error return(RedirectToAction(nameof(Index))); }