示例#1
0
        public async Task CommentOnPost()
        {
            // Create Post
            PostModel model = new PostModel
            {
                Title     = "This is a new book",
                Body      = "The body of the post",
                Posted_At = DateTime.Now,
                ISBN      = "9780553573404"
            };

            model = await postService.CreatePost(authUser, model, openLibraryService);

            // Add a comment under the post
            CommentModel commentModel = new CommentModel
            {
                Post_Id = model.Id,
                Body    = "This is a comment on a post"
            };

            commentModel = commentService.CreateComment(authUser, commentModel);

            Comment comment = context.Comments
                              .Where(com => com.Id == commentModel.Id)
                              .FirstOrDefault();

            // Verify the comment has the correct information
            Assert.AreEqual(commentModel.Body, comment.Body);
            Assert.AreEqual(commentModel.Commented_By, comment.Commented_By.Username);
            Assert.AreEqual(commentModel.Post_Id, comment.Commented_OnId);
        }
示例#2
0
        public void CreateCommentShouldCreateCommentAndIncreaseTheCommentCountOnThePost()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "CreateCategoryShouldCreateCategoryDB")
                          .Options;

            var dbContext = new ApplicationDbContext(options);

            var commentService = new CommentService(dbContext);

            var post = new Post
            {
                CommentCount = 4,
            };

            dbContext.Posts.Add(post);
            dbContext.SaveChanges();

            var comment = new Comment
            {
                PostId = post.Id
            };

            commentService.CreateComment(comment);

            var comments = dbContext.Comments.ToList();

            Assert.Single(comments);
            Assert.Equal(5, post.CommentCount);
        }
        private static AnswerStatus CreateValidComment()
        {
            var dto = new CommentDto
            {
                CommentText  = "test",
                CreationDate = DateTime.Now,
                CreatedBy    = new BlogUserDto {
                    Id = 1
                },
                RelatedTo = new PostDto {
                    Id = 1
                }
            };

            return(_commentService.CreateComment(dto));
        }
示例#4
0
        public ActionResult Create(int ShopId, string Comment, int rating)
        {
            var user = UserManager.GetUserId(User);

            CommentService.CreateComment(int.Parse(user), ShopId, Comment, rating);
            return(RedirectToAction("Details", "Shop", new{ Id = ShopId }));
        }
        private async void OnNewComment()
        {
            if (ActiveUser.IsActive == true && !String.IsNullOrWhiteSpace(NewComment))
            {
                CreateCommentDTO createCommentDTO = new CreateCommentDTO()
                {
                    Text = NewComment, CardId = cardId
                };
                BasicCommentDTO basicCommentDTO = await CommentService.CreateComment(ActiveUser.Instance.LoggedUser.Token, createCommentDTO);

                if (basicCommentDTO != null)
                {
                    var comment = new ReadComment(basicCommentDTO);
                    Comments.Add(comment);
                    NewComment = "";
                }
                else
                {
                    ShowMessageBox(null, "Error creating list.");
                }
            }
            else
            {
                ShowMessageBox(null, "Error getting user.");
            }
        }
        public async Task AddCommentShoutAddItInDB()
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: Guid.NewGuid().ToString()).Options;

            var dbContext = new ApplicationDbContext(options);

            var postRepository = new EfDeletableEntityRepository <Post>(dbContext);

            var commentRepository = new EfDeletableEntityRepository <Comment>(dbContext);

            var service = new CommentService(postRepository, commentRepository);

            dbContext.Posts.Add(new Post
            {
                Id        = "postId",
                Text      = "text",
                CreatedOn = DateTime.UtcNow,
                UserId    = "userId",
                UserName  = "******",
            });

            await dbContext.SaveChangesAsync();

            await service.CreateComment("userId", "postId", "david", "commentText");

            Assert.Equal(1, dbContext.Comments.Count());
        }
        public async void CreateCommentWithPostLocked()
        {
            // Arrange
            var comment = new CommentViewModel
            {
                Author = "id",
                Body   = "body",
                Post   = 1
            };
            var post = new Post
            {
                Author = "id",
                Id     = 1,
                Body   = "body",
                Locked = true
            };
            var expectedErrorMessage = "PostLocked";

            _postRepository.GetPost(Arg.Any <int>()).ReturnsForAnyArgs(post);
            CommentService service = new CommentService(_repository, _identityService, _postRepository, _notificationService);

            // Act
            var response = await service.CreateComment(comment, "username");

            // Assert
            Assert.False(response.Succeeded);
            Assert.Equal(response.Messages[0], expectedErrorMessage);
        }
        public async void CreateCommentWithTrueResponse()
        {
            // Arrange
            var comment = new CommentViewModel
            {
                Author = "id",
                Body   = "body",
                Post   = 1
            };
            var post = new Post
            {
                Author = "id",
                Id     = 1,
                Body   = "body",
                Locked = false
            };
            var user = new User
            {
                Id       = "id=",
                UserName = "******"
            };

            _postRepository.GetPost(Arg.Any <int>()).ReturnsForAnyArgs(post);
            _identityService.GetUserByUsername(Arg.Any <string>()).ReturnsForAnyArgs(user);
            CommentService service = new CommentService(_repository, _identityService, _postRepository, _notificationService);

            // Act
            var response = await service.CreateComment(comment, "username");

            // Assert
            Assert.True(response.Succeeded);
        }
示例#9
0
        public ActionResult CreateComment(CommentViewModel commentViewModel)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    commentViewModel.UserName = commentViewModel.ActiveUser;
                    CommentService.CreateComment(commentViewModel, commentViewModel.PostId);

                    commentViewModel.CommentText = null;
                    return(RedirectToAction("Index", "Comment", new PostViewModel {
                        Id = commentViewModel.PostId
                    }));
                }
                catch (Exception)
                {
                    return(RedirectToAction("Index", "Comment", new PostViewModel {
                        Id = commentViewModel.PostId
                    }));
                }
            }
            else
            {
                return(RedirectToAction("Index", "Comment", new PostViewModel {
                    Id = commentViewModel.PostId
                }));
            }
        }
示例#10
0
        public IActionResult CreateComment([FromForm(Name = "message")] string message, [FromForm(Name = "postId")] int postId)
        {
            // username missing
            CommentService cService = new CommentService();
            string         username = HttpContext.User.FindFirst("Username").Value.ToString();

            cService.CreateComment(username, message, postId);

            return(RedirectToAction("Index", "Home"));
        }
示例#11
0
        public IActionResult AddComment(Comment comment)
        {
            if (ModelState.IsValid)
            {
                Comment tempComment = _commentService.CreateComment(comment);

                return(RedirectToAction("Details", "Mangas", new { id = tempComment.MangaId }));
            }

            return(RedirectToAction("Details", "Mangas", new { id = comment.MangaId }));
        }
        public async void CreateCommentWithNullArgs()
        {
            // Arrange
            CommentService service = new CommentService(_repository, _identityService, _postRepository, _notificationService);

            // Act
            var result = await service.CreateComment(null, null);

            // Assert
            Assert.False(result.Succeeded);
        }
示例#13
0
        public void CreateifcommentNull()
        {
            _commentRepositoruMock.Setup(x => x.CreateComment(It.IsAny <Comment>()));
            _unitOfWorkMock.Setup(x => x.SaveAsync());
            var commentService = new CommentService(_unitOfWorkMock.Object,
                                                    _commentFinderMock.Object,
                                                    _userFinderMock.Object,
                                                    _commentRepositoruMock.Object
                                                    );

            var result = commentService.CreateComment(comment);
        }
示例#14
0
        public IHttpActionResult PostComment(CommentCreate model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            CommentService service = CreateCommentService();

            if (!service.CreateComment(model))
            {
                return(InternalServerError());
            }
            return(Ok());
        }
示例#15
0
        public ActionResult Create([Bind(Include = "Author, Comment, PostId, PageId")] CommentViewModel commentViewModel)
        {
            if (!_cs.CreateComment(commentViewModel))
            {
                return(RedirectToAction("Index"));
            }
            ViewBag.ExComments = commentViewModel.PostId != null
                ? _cs.GetAllComments().Where(m => m.PostId == commentViewModel.PostId)
                : _cs.GetAllComments().Where(m => m.PageId == commentViewModel.PageId);

            return(commentViewModel.PostId != null
                ? RedirectToAction("Details", "Admin", new { id = commentViewModel.PostId.Value })
                : RedirectToAction("Details", "Pages", new { id = commentViewModel.PageId.Value }));
        }
示例#16
0
        private async Task GenerateComments()
        {
            var users = await _context.Users.ToListAsync();

            var debates = await _context.Debates.Take(5).ToListAsync();

            var comments = new CommentRequestDto[] {
                new CommentRequestDto()
                {
                    Text      = $"Texty {debates[0].Topic}",
                    CreatedBy = users[0],
                    DebateId  = debates[0].Id,
                    Parent    = null
                },
                new CommentRequestDto()
                {
                    Text      = $"Texty {debates[1].Topic}",
                    CreatedBy = users[1],
                    DebateId  = debates[1].Id,
                    Parent    = null
                },
                new CommentRequestDto()
                {
                    Text      = $"Texty {debates[2].Topic}",
                    CreatedBy = users[2],
                    DebateId  = debates[2].Id,
                    Parent    = null
                },
                new CommentRequestDto()
                {
                    Text      = $"Texty {debates[3].Topic}",
                    CreatedBy = users[3],
                    DebateId  = debates[3].Id,
                    Parent    = null
                },
                new CommentRequestDto()
                {
                    Text      = $"Texty {debates[4].Topic}",
                    CreatedBy = users[4],
                    DebateId  = debates[4].Id,
                    Parent    = null
                }
            };

            foreach (var comment in comments)
            {
                await _commentService.CreateComment(comment);
            }
            await _context.SaveChangesAsync();
        }
        protected async Task HandleValidSubmit()
        {
            Comment.PublishDate = DateTime.Now;
            Comment.Mem_Id      = Id;

            var result = await CommentService.CreateComment(Comment);

            if (result != null)
            {
                Comments = await CommentService.GetComments(Id);

                StateHasChanged();
            }
        }
示例#18
0
 public ActionResult Create(CommentViewModel comment)
 {
     try
     {
         comment.AuthorId = int.Parse(Session["StudentId"].ToString());
         comment.Created  = DateTime.Now;
         comment.PostId   = int.Parse(Session["PostId"].ToString());
         service.CreateComment(comment);
         return(RedirectToAction("Index", "Post"));
     }
     catch (Exception exception)
     {
         return(View());
     }
 }
示例#19
0
        public void CreateComment()
        {
            var commentCommand = new CommentCreated()
            {
                UserId = 1010,
                RelatedDataEntityType = "Test Product",
                RelatedDataEntityId   = 1,
                Type = 0,
                Body = "TestBody"
            };

            var response = commentService.CreateComment(commentCommand);

            Assert.AreEqual(response.Type, Common.ServiceResponseTypes.Success);
        }
示例#20
0
        public void Should_Create_New_Comment()
        {
            // Setup
            var posts = new List <Post>
            {
                new Post {
                    Id = 1, Wall = new Wall {
                        OrganizationId = 2
                    }
                }
            };

            _postsDbSet.SetDbSetData(posts.AsQueryable());

            var users = new List <ApplicationUser>()
            {
                new ApplicationUser
                {
                    Id = _userId
                }
            };

            _usersDbSet.SetDbSetData(users.AsQueryable());

            var expectedDateTime = DateTime.UtcNow;

            _systemClock.UtcNow.Returns(expectedDateTime);

            var newCommentDto = new NewCommentDTO
            {
                MessageBody    = "test",
                OrganizationId = 2,
                PictureId      = "pic",
                PostId         = 1,
                UserId         = _userId
            };

            // Act
            _commentService.CreateComment(newCommentDto);

            // Assert
            _commentsDbSet.Received(1)
            .Add(Arg.Is <Comment>(c =>
                                  c.AuthorId == _userId &&
                                  c.MessageBody == "test" &&
                                  c.PostId == 1));
            Assert.AreEqual(_postsDbSet.First().LastActivity, expectedDateTime);
        }
示例#21
0
        //CRUD METHODS

        //========CREATE========//

        //POST
        public IHttpActionResult Post(CommentCreate comment)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            //instantitate service
            CommentService service = CreateCommentService();

            if (!service.CreateComment(comment))
            {
                return(InternalServerError());
            }

            return(Ok());
        }
示例#22
0
        public ActionResult Create(CommentCreate model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            bool UserIsLoggedIn = User.Identity.IsAuthenticated;

            if (!UserIsLoggedIn)
            {
                return(RedirectToAction("Login", "Account"));
            }
            var userId  = User.Identity.GetUserId();
            var service = new CommentService(userId);

            service.CreateComment(model);
            return(RedirectToAction("Index"));
        }
示例#23
0
        public async Task <IActionResult> Post([FromBody] CommentRequest commentReq)
        {
            Guid id = new Guid();

            if (commentReq.UserId == null || commentReq.PostId == null)
            {
                return(BadRequest());
            }

            Comment commentRes = await CommentService.CreateComment(id, commentReq.UserId, commentReq.PostId, commentReq.Content);

            if (commentRes == null)
            {
                return(BadRequest());
            }

            return(Ok());
        }
示例#24
0
        public void CreateANewComment()
        {
            // Arrange
            bool result;

            // Act
            result = commentService.CreateComment(commentPool[0]);

            // Assert
            Assert.IsTrue(result);

            commentService.PurgeData();
        }
        public async void CreateCommendWithPostNotFound()
        {
            // Arrange
            var comment = new CommentViewModel
            {
                Author = "id",
                Body   = "body",
                Post   = 1
            };
            var            expectedErrorMessage = "PostNotFound";
            CommentService service = new CommentService(_repository, _identityService, _postRepository, _notificationService);

            // Act
            var response = await service.CreateComment(comment, "username");

            // Assert
            Assert.False(response.Succeeded);
            Assert.Equal(response.Messages[0], expectedErrorMessage);
        }
示例#26
0
        public ActionResult Create(CommentCreate comment)
        {
            if (!ModelState.IsValid)
            {
                return(View(comment));
            }

            var userId  = Guid.Parse(User.Identity.GetUserId());
            var service = new CommentService(userId, comment.MovieId);

            if (service.CreateComment(comment))
            {
                TempData["SaveResult"] = "Your comment was created.";
                return(RedirectToAction("Details", "Lesson", new { id = comment.MovieId }));
            }

            ModelState.AddModelError("", "Comment could not be created");
            return(View(comment));
        }
示例#27
0
        public ActionResult Create(CommentCreate model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            CommentService service = CreateCommentService();

            if (service.CreateComment(model))
            {
                TempData["SaveResult"] = "Your comment was created.";
                return(RedirectToAction("Index"));
            }
            ;

            ModelState.AddModelError("", "Comment could not be created.");

            return(View(model));
        }
示例#28
0
 public IActionResult OnPostCreateComment()
 {
     if (ModelState.IsValid)
     {
         try
         {
             NewComment.UserId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier));
             commentService.CreateComment(NewComment);
             return(RedirectToPage("/Post", new { Id = NewComment.PostId }));
         }
         catch (Exception ex)
         {
             // TODO: Log
             ModelState.AddModelError("", "An error occurred while creating your comment");
         }
     }
     Id = NewComment.PostId;
     LoadModel();
     return(Page());
 }
示例#29
0
        public ActionResult Create(CommentCreate comment)
        {
            if (!ModelState.IsValid)
            {
                return(View(comment));
            }

            var userId  = Guid.Parse(User.Identity.GetUserId());
            var service = new CommentService(userId);

            comment.UserName = User.Identity.GetUserName();
            if (service.CreateComment(comment))
            {
                TempData["SaveResult"] = "Your comment was created.";
                return(RedirectToAction("Index"));
            }

            ModelState.AddModelError("", "Comment could not be created");
            return(View(comment));
        }
示例#30
0
        public async Task Init()
        {
            DbContextOptions <Context> options = new DbContextOptionsBuilder <Context>()
                                                 .UseInMemoryDatabase("bookish")
                                                 .Options;

            context            = new Context(options);
            commentService     = new CommentService(context, new MessageService(context));
            postService        = new PostService(context, commentService);
            ratingService      = new RatingService(context);
            openLibraryService = new OpenLibraryService(new System.Net.Http.HttpClient());
            authUser           = new AuthUserModel
            {
                Id       = 1,
                Username = "******"
            };
            context.Users.Add(new User {
                Id       = authUser.Id,
                Username = authUser.Username
            });

            postModel = new PostModel
            {
                Title     = "This is a new book",
                Body      = "The body of the post",
                Posted_At = DateTime.Now,
                ISBN      = "9780553573404",
                Posted_By = authUser.Username
            };

            postModel = await postService.CreatePost(authUser, postModel, openLibraryService);

            commentModel = new CommentModel
            {
                Post_Id = postModel.Id,
                Body    = "This is a comment on a post"
            };

            commentModel = commentService.CreateComment(authUser, commentModel);
        }