Ejemplo n.º 1
0
        public async Task <IActionResult> GetRecipe(int id)
        {
            var recipe = await _repo.GetRecipe(id);

            var recipeMapDto = _mapper.Map <RecipeForDetailedDto>(recipe);

            return(Ok(recipeMapDto));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> addPhotoToRecipe(int recipeId, [FromForm] PhotoForCreationRecipeDto photoForCreationRecipeDto)
        {
            //Need to valida is a correct user
            // if (recipeId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            //     return Unauthorized();
            var userValidation = User.FindFirst(ClaimTypes.NameIdentifier).Value;

            var recipe = await _repo.GetRecipe(recipeId);

            var alreadyExist = _cloudinary.Search().Expression(string.Format("public_id = {0}", recipe.PublicId)).Execute();

            var file = photoForCreationRecipeDto.File;

            var uploadResults = new ImageUploadResult();

            if (file.Length > 0)
            {
                using (var stream = file.OpenReadStream())
                {
                    var uploadParams = new ImageUploadParams()
                    {
                        File           = new FileDescription(file.Name, stream),
                        Transformation = new Transformation()
                                         .Width(500)
                                         .Height(500)
                                         .Crop("fill")
                                         .Gravity("face")
                    };

                    if (alreadyExist.Resources.Count > 0)
                    {
                        uploadParams.PublicId = recipe.PublicId;
                    }

                    uploadResults = _cloudinary.Upload(uploadParams);
                }
            }

            photoForCreationRecipeDto.PhotoUrl = uploadResults.Uri.ToString();
            photoForCreationRecipeDto.PublicId = uploadResults.PublicId;

            var usermapDto = _mapper.Map(photoForCreationRecipeDto, recipe);

            if (await _repo.SaveAll())
            {
                return(Ok(photoForCreationRecipeDto));
            }

            throw new Exception($"the update fail for recipe {recipeId}");
        }