Esempio n. 1
0
        public async Task <IActionResult> Add([FromRoute] Guid recipeId, [Microsoft.AspNetCore.Mvc.FromBody] CreateRecipeCommentModel model)
        {
            var userId = Guid.Parse(_accessor.HttpContext.User.Claims.First(c => c.Type == "IdUser").Value);
            var result = await _commentsService.Add(userId, recipeId, model);

            return(Ok(result));
        }
        public async Task <RecipeCommentModel> Add(Guid idUser, Guid idRecipe, CreateRecipeCommentModel model)
        {
            var comment = _mapper.Map <RecipeComment>(model);

            comment.IdUser   = idUser;
            comment.IdRecipe = idRecipe;

            var recipe = await _repository.GetById(idRecipe);

            recipe.AddComment(comment);
            _repository.Update(recipe);

            await _repository.SaveChanges();

            return(_mapper.Map <RecipeCommentModel>(comment));
        }
Esempio n. 3
0
        public async Task Post_Comment()
        {
            //Arrange
            var recipe = await AddRecipe();

            var comment = new CreateRecipeCommentModel()
            {
                Comment = "foarte gustoasa",
                IdUser  = AuthenticatedUserId,
                Review  = 5,
            };

            //Act
            var response = await HttpClient.PostAsJsonAsync($"api/v1/recipe/{recipe.Id}/comments", comment);

            //Assert
            response.IsSuccessStatusCode.Should().BeTrue();

            var body = await response.Content.ReadAsStringAsync();

            var commentId = Extract_Guid(body);

            RecipeComment existingComment = null;

            await ExecuteDatabaseAction(async (tastyBoutiqueContext) =>
            {
                existingComment = await tastyBoutiqueContext.RecipeComments
                                  .FirstOrDefaultAsync(c => c.Id == commentId);
            });

            existingComment.Should().NotBeNull();
            existingComment.IdRecipe.Should().Be(recipe.Id);

            await ExecuteDatabaseAction(async (tastyBoutiqueContext) =>
            {
                recipe = await tastyBoutiqueContext.Recipes
                         .FirstOrDefaultAsync(r => r.Id == recipe.Id);
            });

            recipe.ReviewCount.Should().Be(1);
            recipe.AverageReview.Should().Be(comment.Review);
        }
Esempio n. 4
0
 public static CreateRecipeCommentModel WithComment(this CreateRecipeCommentModel model, string comment)
 {
     model.Comment = comment;
     return(model);
 }
Esempio n. 5
0
 public static CreateRecipeCommentModel WithReview(this CreateRecipeCommentModel model, int review)
 {
     model.Review = review;
     return(model);
 }