Esempio 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");
        }
Esempio n. 2
0
        public async Task <IActionResult> GetUser(int id)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var user = await _repo.GetUser(id);

            var userMapDto = _mapper.Map <UserForDetailedDto>(user);

            return(Ok(userMapDto));
        }
Esempio n. 3
0
        public async Task <IActionResult> deleteRecipe(int id)
        {
            var user = await _repo.GetUser(int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value));

            if (!user.Recipes.Any(x => x.Id == id))
            {
                return(Unauthorized());
            }

            var recipe = await _repo.GetRecipe(id);

            if (recipe.PublicId != null)
            {
                var deleteParams = new DeletionParams(recipe.PublicId);
                var result       = _cloudinary.Destroy(deleteParams);

                if (result.Result == "ok")
                {
                    recipe.PublicId = null;
                    recipe.PhotoUrl = null;
                }
                else
                {
                    return(BadRequest("Failed to delete Photo"));
                }
            }


            _repo.Delete(recipe);

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

            return(BadRequest("Failed to delete Recipe"));
        }
Esempio n. 4
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}");
        }