예제 #1
0
        public async Task <IActionResult> DeletePhoto(long userId, long id)
        {
            if (!CheckUserId(userId))
            {
                return(Unauthorized());
            }

            if (!await _photoRepo.Any(userId, id))
            {
                return(BadRequest());
            }

            var photoFromRepo = await _photoRepo.GetPhoto(id);

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

            if (string.IsNullOrWhiteSpace(photoFromRepo.PublicId))
            {
                _photoRepo.Delete(photoFromRepo);
            }
            else
            {
                var deleteParams = new DeletionParams(photoFromRepo.PublicId);
                var resutl       = _cloudinary.Destroy(deleteParams);

                if (resutl.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    _photoRepo.Delete(photoFromRepo);
                }
            }

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

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