public async Task Update(string commentId, string userId, CreateCommentRequestModel model) { await new CreateOrUpdateCommentValidator().ValidateRequestModelAndThrow(model); Comment registeredComment; string commentRegisteredOnCacheJson = await _cacheDatabase.Get(commentId); if (commentRegisteredOnCacheJson != null) { registeredComment = _jsonUtils.Deserialize <Comment>(commentRegisteredOnCacheJson); } else { registeredComment = await _commentRepository.GetByIdIncludingUser(Guid.Parse(commentId)); if (registeredComment == null) { throw new ResourceNotFoundException("comment not found."); } } ThrowIfAuthenticatedUserNotIsCommentCreator(registeredComment, Guid.Parse(userId)); registeredComment.UpdateText(model.Text); _commentRepository.Update(registeredComment); await _commentRepository.Save(); await _cacheDatabase.Remove(commentId); }
public async Task <ActionResult> Create(CreateCommentRequestModel model) { var userId = this.User.GetId(); var commentId = await this.commentsService .CreateAsync(model.Content, model.AdvertisementId, userId); return(Created(nameof(this.Create), commentId)); }
public async Task <ResultModel <string> > CreateCommentAsync(CreateCommentRequestModel model, string userId) { var user = await this.dbContext .Users .Where(u => u.Id == userId && !u.IsDeleted) .FirstOrDefaultAsync(); if (user == null) { return(new ResultModel <string> { Errors = { UserErrors.InvalidUserId } }); } var story = await this.dbContext .Stories .Where(s => s.Id == model.StoryId && !s.IsDeleted) .FirstOrDefaultAsync(); if (story == null) { return(new ResultModel <string> { Errors = { StoryErrors.NotFoundOrDeletedStory } }); } var isBanned = await this.userService.IsBanned(userId); if (isBanned) { return(new ResultModel <string> { Errors = { CommentErrors.BannedUserCreateComment } }); } var comment = new Comment { Content = model.Content, StoryId = model.StoryId, UserId = userId, Likes = 0, Dislikes = 0, }; await this.dbContext.AddAsync(comment); await this.dbContext.SaveChangesAsync(); return(new ResultModel <string> { Result = comment.Id, Success = true, }); }
public async Task <ActionResult <CommentListingModel> > CreateComment( [FromBody] CreateCommentRequestModel inputModel) { var commentId = await this.commentsService.CreateAsync( inputModel.ArticleId, inputModel.ParentId, inputModel.Content, this.User.GetId()); return(await this.commentsService.GetByIdAsync <CommentListingModel>(commentId)); }
public async Task <ActionResult> Create(CreateCommentRequestModel model) { var loggedUser = this.User.GetId(); var result = await this.commentService.CreateCommentAsync(model, loggedUser); if (!result.Success) { return(BadRequest(result.Errors)); } return(Created(nameof(Create), result.Result)); }
public async Task <IActionResult> Update(string commentId, CreateCommentRequestModel model) { try { await _commentService.Update(commentId, this.GetUserIdFromToken(), model); return(Ok()); } catch (Exception exception) { return(this.HandleExceptionToUserAndLogIfExceptionIsUnexpected(exception)); } }
public async Task ShouldThrowResourceNotFoundExceptionOnCreateCommentWithNotExistsReview() { _cacheDatabaseMock.Get(Arg.Any <string>()).Returns(null as string); _reviewRepositoryMock.AlreadyExists(Arg.Any <Guid>()).Returns(false); CreateCommentRequestModel requestModel = new CreateCommentRequestModel() { Text = "TEXT" }; Exception exception = await Record.ExceptionAsync(() => _commentService.Create(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), requestModel)); Assert.IsType <ResourceNotFoundException>(exception); }
public async Task <IActionResult> Create(string reviewId, CreateCommentRequestModel model) { try { IdResponseModel responseModel = await _commentService.Create(reviewId, this.GetUserIdFromToken(), model); return(CreatedAtRoute("", model, responseModel)); } catch (Exception exception) { return(this.HandleExceptionToUserAndLogIfExceptionIsUnexpected(exception)); } }
public IHttpActionResult Post(CreateCommentRequestModel model) { var userId = this.User.Identity.GetUserId(); var result = this.comments .CreateComment(model.RealEstateId, model.Content, userId); var resultingComment = this.comments.GetCommentById(result.Id) .ProjectTo <CommentsResponseModel>() .FirstOrDefault(); return(this.Ok(resultingComment)); }
public async Task ShouldThrowForbiddenExceptionOnUpdate() { CreateCommentRequestModel requestModel = new CreateCommentRequestModel() { Text = "TEXT" }; _cacheDatabaseMock.Get(Arg.Any <string>()).Returns(null as string); _commentRepositoryMock.GetByIdIncludingUser(Arg.Any <Guid>()).Returns(new Comment("TEXT", Guid.NewGuid(), Guid.NewGuid())); Exception exception = await Record.ExceptionAsync(() => _commentService.Update(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), requestModel)); Assert.IsType <ForbiddenException>(exception); }
public async Task ShouldCreateComment() { _cacheDatabaseMock.Get(Arg.Any <string>()).Returns(null as string); _reviewRepositoryMock.AlreadyExists(Arg.Any <Guid>()).Returns(true); CreateCommentRequestModel requestModel = new CreateCommentRequestModel() { Text = "TEXT" }; Exception exception = await Record.ExceptionAsync(() => _commentService.Create(Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), requestModel)); Assert.Null(exception); await _commentRepositoryMock.Received(1).Create(Arg.Any <Comment>()); await _commentRepositoryMock.Received(1).Save(); }
public async Task ShouldGetAllComments() { User insertedUser = await InsertUserOnDatabase(); Review insertedReview = await InsertReviewOnDatabase(insertedUser.Id); _httpClient.InsertAuthorizationTokenOnRequestHeader(_authorizationTokenHelper.CreateToken(insertedUser.Id)); CreateCommentRequestModel requestModel = new CreateCommentRequestModel() { Text = "TEXT" }; HttpResponseMessage response = await _httpClient.GetAsync($"../reviews/{insertedReview.Id.ToString()}/comments"); Assert.Equal((int)HttpStatusCode.OK, (int)response.StatusCode); }
public IHttpActionResult Post(CreateCommentRequestModel model) { if (realEstatesServices.GetById(model.RealEstateId).FirstOrDefault() == null) { return(this.NotFound()); } int commentId = this.commentServices.Create( model.RealEstateId, model.Content, this.User.Identity.GetUserId()); var response = this.commentServices.GetById(commentId) .ProjectTo <CommentResponseModel>() .FirstOrDefault(); return(this.Created("", response)); }
public async Task <IdResponseModel> Create(string reviewId, string userId, CreateCommentRequestModel model) { await new CreateOrUpdateCommentValidator().ValidateRequestModelAndThrow(model); Guid reviewIdGuid = Guid.Parse(reviewId); await ThrowIfReviewNotExists(reviewIdGuid); Comment comment = new Comment(model.Text, Guid.Parse(userId), reviewIdGuid); await _commentRepository.Create(comment); await _commentRepository.Save(); return(new IdResponseModel() { Id = comment.Id }); }
public async Task ShouldUpdateCommentFromDatabase() { Guid userId = Guid.NewGuid(); CreateCommentRequestModel requestModel = new CreateCommentRequestModel() { Text = "TEXT" }; _cacheDatabaseMock.Get(Arg.Any <string>()).Returns(null as string); _commentRepositoryMock.GetByIdIncludingUser(Arg.Any <Guid>()).Returns(new Comment("TEXT", userId, Guid.NewGuid())); Exception exception = await Record.ExceptionAsync(() => _commentService.Update(Guid.NewGuid().ToString(), userId.ToString(), requestModel)); Assert.Null(exception); _commentRepositoryMock.Received(1).Update(Arg.Any <Comment>()); await _commentRepositoryMock.Received(1).Save(); await _cacheDatabaseMock.Received(1).Remove(Arg.Any <string>()); }
public async Task <IActionResult> CreateComment(CreateCommentRequestModel createCommentRequest) { var funfic = _appDbContext.Funfics.FirstOrDefault(f => f.Id == createCommentRequest.Id); var user = await _userManager.FindByNameAsync(User.Identity.Name); if (funfic == null) { return(StatusCode(404, "No such funfic")); } var date = DateTime.Now; await _appDbContext.Comments.AddAsync(new Comment { FunficId = createCommentRequest.Id, UserId = user.Id, Text = createCommentRequest.Text, Date = date }); try { _appDbContext.SaveChanges(); } catch { return(StatusCode(409)); } return(StatusCode(200, new { author = user.UserName, text = createCommentRequest.Text, createdAt = date })); }
public IHttpActionResult Post(CreateCommentRequestModel model) { string username = this.User.Identity.Name; string userId = this.User.Identity.GetUserId(); var createdComment = this.comments.CreateComment(model.RealEstateId, model.Content, username, userId); return this.Created ( string.Format("api/Comment/{0}", createdComment.Id), Mapper.Map<CreatedCommentResponseModel>(createdComment) ); }
public IHttpActionResult Post(CreateCommentRequestModel model) { if (realEstatesServices.GetById(model.RealEstateId).FirstOrDefault() == null) { return this.NotFound(); } int commentId = this.commentServices.Create( model.RealEstateId, model.Content, this.User.Identity.GetUserId()); var response = this.commentServices.GetById(commentId) .ProjectTo<CommentResponseModel>() .FirstOrDefault(); return this.Created("", response); }
public async Task <ActionResult> Create(CreateCommentRequestModel model) { var commentId = await this.commentService.Create(model.Content, this.UserId, model.ProjectId); return(Created(nameof(this.Create), commentId)); }