public IHttpActionResult Edit(CommentApiModel model) { if (string.IsNullOrEmpty(model.Comment)) { return(BadRequest("Your comment can't null")); } var comment = commentRepository.FindById(model.Id.Value); if (comment == null) { return(BadRequest()); } comment.Content = model.Comment; comment.ModifiedDate = DateTime.Now; try { commentRepository.Update(comment); commentRepository.SaveChanges(); } catch (Exception ex) { return(BadRequest(ex.Message)); } return(Created("", model)); }
public async Task <IActionResult> Post(CommentApiModel apiModel) { try { var email = User.FindFirst(ct => ct.Type.Contains("nameidentifier")).Value; var user = await _userRepo.GetUserByEmailAsync(email); apiModel.User = ApiModelConverter.ToUserApiModel(user); Comment comment = ApiModelConverter.ToComment(_commentRepo, _userRepo, _postRepo, apiModel); int result = await _commentRepo.CreateAsync(comment); if (result != -1) { return(Ok()); } else { throw new ArgumentException("Could not create comment"); } } catch (ArgumentException ex) { return(BadRequest(ex.Message)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public IHttpActionResult Post(CommentApiModel model) { if (string.IsNullOrEmpty(model.Comment)) { return(BadRequest("Your comment can't null")); } var comment = new Comment() { Content = model.Comment, Status = Status.IsAvtive, CreatedDate = DateTime.Now, PostId = Convert.ToInt32(model.PostId), UserId = model.UserId, ParentId = Convert.ToInt32(model.ParentId) }; try { commentRepository.Add(comment); commentRepository.SaveChanges(); } catch (Exception ex) { return(BadRequest(ex.Message)); } return(Created("", model)); }
public void UserAddsComment() { var userId = Guid.NewGuid(); var testTicket = new Ticket { Text = "Test_Text", Name = "Test_Name" }; var testComment = new CommentApiModel { Text = "Test_text", Date = DateTime.UtcNow, User = new UserApiModel() }; _communicationServiceMock.Setup( x => x.GetAsync <UserApiModel>(It.IsAny <string>(), null, It.IsAny <IHeaderDictionary>(), It.IsAny <string>())) .ReturnsAsync(new UserApiModel { Id = userId }); this.Given(s => s.GivenExistingTicket(testTicket)) .When(s => s.WhenUserAddsComment(testComment)) .Then(s => s.ThenCommentIsAddedWithTheSameId()) .And(s => s.AndWithTheSameText()) .And(s => s.AndWithTheSameUserId()) .BDDfy <UserManagesComments>(); }
private async Task WhenUserAddsComment(CommentApiModel comment) { var okObjectResult = await _sut.Post(StubTeamId, _stubTicketForAddingComment.Id, comment) as OkObjectResult; if (okObjectResult != null) { comment.Id = (Guid)okObjectResult.Value; } _addedComment = comment; }
public async Task Post_ReturnsBadRequest_WhenInValidCommentViewModel() { var model = new CommentApiModel(); var ticketId = Guid.NewGuid(); _sut.ModelState.AddModelError("Model", "Invalid"); var result = await _sut.Post(It.IsAny <Guid>(), ticketId, model); Assert.Equal(typeof(BadRequestResult), result.GetType()); }
public async Task <IActionResult> Post(Guid teamId, Guid id, [FromBody] CommentApiModel model) { if (ModelState.IsValid) { var commentDto = _mapper.Map <CommentDto>(model); var createdCommentId = await _commentService.CreateAsync(teamId, id, commentDto); _logger.LogInformation($"Comment was succesfully created with text {model.Text}"); return(Ok(createdCommentId)); } return(BadRequest()); }
public IHttpActionResult Delete(CommentApiModel model) { var comment = commentRepository.FindById(model.Id.Value); if (comment == null) { return(BadRequest()); } try { commentRepository.Remove(comment); commentRepository.SaveChanges(); } catch (Exception ex) { return(BadRequest(ex.Message)); } return(Created("", model)); }
public async Task Post_ReturnsOk_WhenValidCommentViewModel() { var httpContextMock = new Mock <HttpContext>(); var model = new CommentApiModel(); var ticketId = Guid.NewGuid(); var mockPrincipal = new Mock <ClaimsPrincipal>(); mockPrincipal.Setup(x => x.FindFirst(It.IsAny <string>())).Returns(new Claim(string.Empty, string.Empty)); httpContextMock.Setup(m => m.User).Returns(mockPrincipal.Object); var headersMock = new HeaderDictionary { new KeyValuePair <string, StringValues>(ContentTypeHeaderKey, "Content-Type_Header_Test_Value"), new KeyValuePair <string, StringValues>(AuthorizationHeaderKey, "Authorization_Header_Test_Value"), new KeyValuePair <string, StringValues>(CorrelationIdHeaderKey, "CorrelationId_Header_Test_Value") }; httpContextMock.SetupGet(x => x.Request.Headers).Returns(headersMock); var actionContext = new ActionContext( httpContextMock.Object, new Mock <RouteData>().Object, new Mock <ActionDescriptor>().Object); _sut.ControllerContext = new ControllerContext(new ActionContext() { RouteData = new RouteData(), HttpContext = actionContext.HttpContext, ActionDescriptor = new ControllerActionDescriptor() }); _communicationServiceMock.Setup( method => method.GetAsync <UserApiModel>(It.IsAny <string>(), It.IsAny <Dictionary <string, string> >(), It.IsAny <IHeaderDictionary>(), It.IsAny <string>())) .ReturnsAsync(new UserApiModel()); var result = await _sut.Post(It.IsAny <Guid>(), ticketId, model); Assert.Equal(typeof(OkObjectResult), result.GetType()); }