Ejemplo n.º 1
0
        public async Task <IActionResult> createFallowUser(int id)
        {
            var userId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (await _repo.GetUser(userId) == null)
            {
                return(Unauthorized());
            }

            var fallowUser = new FallowUserForCreateDto();

            fallowUser.DateCreated = DateTime.Now;
            fallowUser.UserId      = userId;
            fallowUser.FollowerId  = id;

            FollowUser newFallowUser = _mapper.Map <FollowUser>(fallowUser);

            _repo.Add(newFallowUser);


            if (await _repo.SaveAll())
            {
                return(StatusCode(201));
            }

            throw new Exception($"the creation for fallowUser fail");
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> UpdateUser(int id, UserForUpdateDto userForUpdateDto)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            if (!string.IsNullOrEmpty(userForUpdateDto.UserName))
            {
                userForUpdateDto.UserName.ToLower();
            }

            var user = await _repo.GetUser(id);



            var userMapDto = _mapper.Map(userForUpdateDto, user);

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"the update fail for user {id}");
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> UpdateRecipe(int id, RecipeForUpdateDto recipeForUpdateDto)
        {
            var recipe = await _repo.GetRecipe(id);

            var updateRecipe = _mapper.Map(recipeForUpdateDto, recipe);

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"the update fail for recipe {id}");
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> AddReview(ReviewForCreateDto reviewClient)
        {
            if (reviewClient.UserId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            if (await _repo.AlreadyUserReview(reviewClient.UserId, reviewClient.RecipeId))
            {
                throw new Exception($"You already review this recipe");
            }

            reviewClient.Created = DateTime.Now;
            reviewClient.Status  = "Acepted";

            var reviewMap = _mapper.Map <Review>(reviewClient);

            _repo.Add(reviewMap);

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            throw new Exception($"the creation for review fail");
        }
Ejemplo n.º 5
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}");
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> addPhotoToUser(int userId, [FromForm] PhotoForCreationDto photoForCreationDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var user = await _repo.GetUser(userId);

            var alreadyExist = 0;

            if (user.PublicId != null)
            {
                alreadyExist = _cloudinary.Search().Expression(string.Format("public_id = {0}", user.PublicId)).Execute().Resources.Count;
            }


            var file = photoForCreationDto.File;

            var uploadResults = new ImageUploadResult();

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

                        uploadResults = _cloudinary.Upload(uploadParams);
                    }
                    else
                    {
                        uploadParams.File           = new FileDescription(file.Name, stream);
                        uploadParams.Transformation = new Transformation()
                                                      .Width(500)
                                                      .Height(500)
                                                      .Crop("fill")
                                                      .Gravity("face");

                        uploadResults = _cloudinary.Upload(uploadParams);
                    }
                };
            }


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

            var usermapDto = _mapper.Map(photoForCreationDto, user);

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

            throw new Exception($"the update fail for user {userId}");
        }