public async Task <IActionResult> Delete(int petId, int photoId) { if (petId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var user = await petsRepository.GetPet(petId); if (!user.Photos.Any(p => p.Id == photoId)) { return(BadRequest("The photo does not belong to the user")); } var photo = await petsRepository.GetPhoto(photoId); if (photo.PublicId != null) { var deleteParams = new DeletionParams(photo.PublicId); var result = cloud.Destroy(deleteParams); if (result.Result.Equals("ok")) { petsRepository.DeletePhoto(photo); } } else { petsRepository.DeletePhoto(photo); } if (await petsRepository.Save()) { return(Ok()); } return(BadRequest("Error when deleting photo")); }