示例#1
0
        public async Task <IActionResult> DeleteFromUserArchive(int userId, int id)
        {
            //Determine if the user adding the photo is authorized:
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) //access the current user's token and compare Ids with the profile being accessed
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repo.GetUser(userId);

            if (!userFromRepo.Archived.Any(a => a.Id == id))
            {
                return(Unauthorized());
            }

            var archivedPhotoFromRepo = await _repo.GetArchivedPhoto(id);

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

            return(BadRequest("Failed to delete archived photo"));
        }
示例#2
0
        public async Task <IActionResult> DeletePhoto(int userId, int id)
        {
            //Determine if the user adding the photo is authorized:
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) //access the current user's token and compare Ids with the profile being accessed
            {
                return(Unauthorized());
            }

            var userFromRepo = await _repo.GetUser(userId);

            //Determine if any posts from the user correlate with the id of the photo we want to delete
            if (!userFromRepo.Posts.Any(p => p.Id == id))
            {
                return(Unauthorized());
            }

            //Get the photo
            var photoFromRepo = await _repo.GetPhoto(id);

            //Check if our photo is on our cloudinary database:
            if (photoFromRepo.PublicId != null)
            {
                //Now delete the photo from cloudinary, and delete the url from our database:
                var deleteParams = new DeletionParams(photoFromRepo.PublicId);

                var result = _cloudinary.Destroy(deleteParams);

                if (result.Result == "ok") // if we have successfully destroyed the cloudinary photo, then it MUST be deleted from our db as well
                {
                    _repo.Delete(photoFromRepo);
                }
            }

            //If our photo is NOT in our cloudinary db, then delete it from repo:
            if (photoFromRepo.PublicId == null)
            {
                _repo.Delete(photoFromRepo);
            }

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

            return(BadRequest("Failed to delete photo"));
        }