public async Task <IActionResult> AddAsync([FromBody] PostCreateRequest postCreateRequest) { var newUser = _mapper.Map <Post>(postCreateRequest); await _postService.AddAsync(newUser); return(CreatedAtAction(nameof(GetByIdAsync), new { id = newUser.Id }, newUser)); }
public async Task AddAsync_ShouldReturnResponseSuccessTrue_WhenPostAdded() { //Arange //_postRepoMock.Setup(x => x.AddAsync()) var id = Guid.NewGuid().ToString(); var name = "Post for test"; var content = "Content for testing"; var post = new Post { Id = id, Name = name, Content = content }; //Act PostResponse pr = await _sut.AddAsync(post); //Assert Assert.True(pr.Success); }
public async Task <IActionResult> Create([FromBody] Post post) { if (post == null) { return(BadRequest()); } if (post.ThreadId == null) { return(BadRequest()); } var thread = _threadService.FindById(post.ThreadId); if (thread.ApprovalStatus != ApprovalStatus.Approved) { return(BadRequest()); } if (thread.Locked) { return(BadRequest()); } thread.LastActivity = DateTime.Now; post.CreatedById = CurrentUserId; var createdBy = _userService.FindById(CurrentUserId); if (createdBy.Role == Role.Administrator || createdBy.Role == Role.Moderator) { _postService.SetApprovalStatus(CurrentUserId, post, ApprovalStatus.Approved); thread.NumberOfPosts += 1; } await _postService.AddAsync(post); await _unitOfWork.SaveChangesAsync(); post = _postService.FindWithCreatedBy(post.Id); var dto = _mapper.Map <PostDto>(post); return(StatusCode(StatusCodes.Status201Created, dto)); }
public void PostAsync_InvalidOperationExceptionTest() { _uowMock.Setup(x => x.Posts.CreateAsync(It.IsAny <Post>())).ReturnsAsync((Post)null); Assert.That(async() => await _service.AddAsync(null), Throws.InstanceOf <OperationException>()); }