Beispiel #1
0
        public async Task <ActionResult> SubmitThread(CreateThreadModel model)
        {
            if (ModelState.IsValid)
            {
                using (var context = new ApplicationDbContext())
                {
                    var thread = new ForumThread
                    {
                        CategoryId = model.CategoryId,
                        Title      = model.Title,
                        UserId     = User.Identity.GetUserId()
                    };

                    var sanitizer = new HtmlSanitizer();
                    var post      = new ForumPost
                    {
                        UserId      = User.Identity.GetUserId(),
                        Message     = sanitizer.Sanitize(model.Message),
                        IsFirstPost = true,
                    };
                    thread.Posts = new List <ForumPost>();
                    thread.Posts.Add(post);
                    context.ForumThreads.Add(thread);
                    await context.SaveChangesAsync().ConfigureAwait(false);

                    return(RedirectToAction("Thread", new { id = thread.Id }));
                }
            }
            return(View("CreateThread", model));
        }
Beispiel #2
0
        // POST api/<controller>
        public async Task<EntityCreatedResult> Post([FromBody]CreateThreadModel createModel)
        {
            if (ModelState.IsValid)
            {
                if (User.Identity.IsAuthenticated)
                {
                    createModel.Username = User.Identity.Name;
                }

                return await _threadsService.Create(createModel);
            }

            throw new HttpResponseException(HttpStatusCode.BadRequest);
        }
Beispiel #3
0
        public async Task Create_DoesntContainUserInformation_WhenUsernameIsNotProvided()
        {
            // Arrange
            _uowMock.Setup(x => x.BeginTransaction()).Returns(new Mock <ITransaction>().Object);
            CreateThreadModel createModel = new CreateThreadModel
            {
                InitialPost = new CreatePostModel {
                    Content = "contzntz"
                }
            };


            // Act
            await _threadsService.Create(createModel);

            _threadsRepositoryMock.Verify(x => x.Add(It.Is <ForumThread>(thread => thread.UserId == null)));
        }
Beispiel #4
0
        public async Task Create_ReturnsCorrectThreadId_WhenThreadWasCreatedSuccessfully()
        {
            // Arrange
            _uowMock.Setup(x => x.BeginTransaction()).Returns(new Mock <ITransaction>().Object);
            CreateThreadModel createModel = new CreateThreadModel
            {
                InitialPost = new CreatePostModel {
                    Content = "contzntz"
                }
            };

            _threadsRepositoryMock.Setup(x => x.Add(It.IsAny <ForumThread>()))
            .Callback <ForumThread>(thread => thread.Id = 12);

            // Act
            EntityCreatedResult result = await _threadsService.Create(createModel);

            Assert.That(result.Id, Is.EqualTo(12));
        }
Beispiel #5
0
        public async Task Create_ProvidesPostsServiceWithCorrectThreadId_WhenThreadWasCreatedSuccessfully()
        {
            // Arrange
            _uowMock.Setup(x => x.BeginTransaction()).Returns(new Mock <ITransaction>().Object);
            CreateThreadModel createModel = new CreateThreadModel
            {
                InitialPost = new CreatePostModel {
                    Content = "contzntz"
                }
            };

            _threadsRepositoryMock.Setup(x => x.Add(It.IsAny <ForumThread>()))
            .Callback <ForumThread>(thread => thread.Id = 12);

            // Act
            await _threadsService.Create(createModel);

            _postsServiceMock.Verify(x => x.CreatePost(It.Is <CreatePostModel>(post => post.ThreadId == 12)));
        }
Beispiel #6
0
        public async Task Create_RetrievesUser_WhenUsernameIsProvided()
        {
            // Arrange
            _uowMock.Setup(x => x.BeginTransaction()).Returns(new Mock <ITransaction>().Object);
            CreateThreadModel createModel = new CreateThreadModel
            {
                Username    = "******",
                InitialPost = new CreatePostModel()
                {
                    Content = "contzntz"
                }
            };
            User user = new User {
                Username = "******", Id = 123
            };

            _userServiceMock.Setup(x => x.GetByUsername(createModel.Username)).ReturnsAsync(user);

            // Act
            await _threadsService.Create(createModel);

            _userServiceMock.Verify(x => x.GetByUsername(createModel.Username));
        }
Beispiel #7
0
        public async Task Create_PersistsUserInfo_WhenUsernameIsProvided()
        {
            // Arrange
            _uowMock.Setup(x => x.BeginTransaction()).Returns(new Mock <ITransaction>().Object);
            CreateThreadModel createModel = new CreateThreadModel
            {
                Username    = "******",
                InitialPost = new CreatePostModel()
                {
                    Content = "contzntz"
                }
            };
            User user = new User {
                Username = "******", Id = 123
            };

            _userServiceMock.Setup(x => x.GetByUsername(createModel.Username)).ReturnsAsync(user);

            // Act
            await _threadsService.Create(createModel);

            _threadsRepositoryMock.Verify(x => x.Add(It.Is <ForumThread>(thread => thread.UserId == user.Id)));
        }