public JsonResult Get() { List <Object> recettes = _ngCookingRepository.GetAll <Recette>(_recette).ToList(); List <RecetteViewModel> recettesVM = new List <RecetteViewModel>(); foreach (Recette recette in recettes) { List <Ingredient> recetteIngredients = _ngCookingRepository.GetIngredientsByRecetteId(recette.Id).ToList(); List <Comment> recetteComments = _ngCookingRepository.GetCommentsByRecetteId(recette.Id).ToList(); var recetteVM = Mapper.Map <RecetteViewModel>(recette); _logger.LogInformation("Le mappage est fait"); recetteVM.Ingredients = new List <Ingredient>(); recetteVM.Comments = new List <Comment>(); //formattage de ingredients foreach (Ingredient ingredient in recetteIngredients) { recetteVM.Ingredients.Add((Ingredient)_ngCookingRepository.FindById(ingredient.Id, "Ingredient")); } //formattage de comments foreach (Comment comment in recetteComments) { recetteVM.Comments.Add(comment); } recettesVM.Add(recetteVM); } _logger.LogInformation($"ça marhe pour les recettesVM:{recettesVM.Count}"); return(Json(recettesVM)); }
public JsonResult Get(string id) { try { if (ModelState.IsValid) { Response.StatusCode = (int)HttpStatusCode.Created; var res = _ngCookingRepository.GetCommentsByRecetteId(int.Parse(id)); _logger.LogInformation($"Ajout reussi {res.Count()}"); return(Json(res)); } } catch (Exception ex) { Response.StatusCode = (int)HttpStatusCode.BadRequest; _logger.LogError("failed to save infos"); return(Json(new { Message = ex.Message, ModelState = ModelState })); } Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json(new { Message = "Failed.", ModelState = ModelState })); }