public JsonResult Post([FromBody] RecetteFromViewModel recetteVM)
        {
            try
            {
                #region upload file
                // var pic = this.HttpContext.Request.Form.Count();//.Files["picture"].OpenReadStream();//l'image à charger
                // HttpRequestMessage request = HttpContext.Request;
                //if (!Request.Content.IsMimeMultipartContent())
                //    throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);

                //var provider = new MultipartMemoryStreamProvider();
                //await Request.Form..Content.ReadAsMultipartAsync(provider);
                //foreach (var file in provider.Contents)
                //{
                //    var filename = file.Headers.ContentDisposition.FileName.Trim('\"');
                //    var buffer = await file.ReadAsByteArrayAsync();
                //    //Do whatever you want with filename and its binaray data.
                //}

                //var request = this.HttpContext.Request;
                //var filePath = "C:\\temp\\" + request.Headers["picture"];
                //using (var fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create))
                //{
                //   // request..InputStream.CopyTo(fs);
                //}
                #endregion


                if (ModelState.IsValid)
                {
                    Response.StatusCode = (int)HttpStatusCode.Created;
                    _logger.LogInformation($"adding successfuly:{recetteVM.Name}");
                    var newRecette = Mapper.Map <Recette>(recetteVM);
                    _ngCookingRepository.Add <Recette>(newRecette);
                    RecetteIngredient newRecetteIngredient = null;
                    foreach (var ing in recetteVM.Ingredients)
                    {
                        object ingredient = _ngCookingRepository.FindByName(ing.Name, "Ingredient");
                        newRecetteIngredient = new RecetteIngredient()
                        {
                            IngredientId = ((Ingredient)ingredient).Id, RecetteId = ((Recette)_ngCookingRepository.FindByName(newRecette.Name, "Recette")).Id
                        };
                        _ngCookingRepository.Add <RecetteIngredient>(newRecetteIngredient);
                    }
                    return(Json("it is succesfully added"));
                }
            }
            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 }));
        }
Exemple #2
0
 public JsonResult Post([FromBody] ICollection <IngredientViewModel> ingredientsVM)
 {
     try
     {
         if (ModelState.IsValid)
         {
             Response.StatusCode = (int)HttpStatusCode.Created;
             _logger.LogInformation("adding successfuly");
             foreach (var ingredient in ingredientsVM)
             {
                 var newIngredient = Mapper.Map <Ingredient>(ingredient);
                 newIngredient.Picture = _ngCookingRepository.FileToByteArray("ngCooking/img/ingredients/" + ingredient.Picture);
                 _ngCookingRepository.Add <Ingredient>(newIngredient);
             }
             return(Json("It is succesfully added"));
         }
     }
     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 }));
 }
 public JsonResult Post([FromBody] CommentViewModel commentVM)
 {
     try
     {
         if (ModelState.IsValid)
         {
             Response.StatusCode = (int)HttpStatusCode.Created;
             var newComment = Mapper.Map <Comment>(commentVM);
             //Comment newComment = new Comment() { CommentBody = commentVM.Comments, Mark = commentVM.Mark, Title = commentVM.Title,
             //    UserId = commentVM.UserId,
             //    Recette = (Recette)_ngCookingRepository.FindById(commentVM.RecetteId,"Recette")
             //};
             _ngCookingRepository.Add <Comment>(newComment);
             return(Json("comment successfully added"));
         }
     }
     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 }));
 }
        public JsonResult Post([FromBody] ICollection <CategoryViewModel> categoriesVM)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    //var newRecette = Mapper.Map<Recette>(recetteVM);
                    Response.StatusCode = (int)HttpStatusCode.Created;
                    _logger.LogInformation("adding successfuly");
                    foreach (var category in categoriesVM)
                    {
                        var newCategory = new Category()
                        {
                            Name = category.NameToDisplay
                        };
                        _ngCookingRepository.Add <Category>(newCategory);
                    }
                    return(Json("it is succesfully added"));
                }
            }
            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 }));
        }