Example #1
0
        public async Task <IActionResult> DeletePhoto(int userId, int id)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var photoFromRepo = await _repo.GetPhoto(id);

            if (photoFromRepo == null)
            {
                return(null);
            }

            if (photoFromRepo.IsMain)
            {
                return(BadRequest("You cannot delete main photo"));
            }

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

                if (results.Result == "ok")
                {
                    _repo.Delele(photoFromRepo);
                }
            }

            if (photoFromRepo == null)
            {
                _repo.Delele(photoFromRepo);
            }

            if (await _repo.SaveAll())
            {
                return(Ok());
            }
            return(BadRequest("Failed to delete the photo"));
        }