public async Task <IActionResult> DeletePhoto(int userId, int id) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var user = await _repo.GetUser(userId); if (!user.Photos.Any(p => p.Id == id)) { return(Unauthorized()); } var photoFromRepo = await _repo.GetPhoto(id); if (photoFromRepo.IsMain) { return(BadRequest("You cant Delete Your main photo")); } if (photoFromRepo.PublicId != null) { var deleParmas = new DeletionParams(photoFromRepo.PublicId); var results = _cloudinary.Destroy(deleParmas); if (results.Result == "ok") { _repo.Delet(photoFromRepo); } } if (photoFromRepo.PublicId == null) { _repo.Delet(photoFromRepo); } if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("This photo can't be Deleted")); }
public async Task <IActionResult> DeletPhoto(int userId, int id) { //if now user id != modify user id ==> no right to change if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var user = await _repository.GetUser(userId); if (!user.Photos.Any(p => p.Id == id)) { return(Unauthorized()); } var photofromRep = await _repository.GetPhotos(id); if (photofromRep.IsMain) { return(BadRequest("can't delete main photo")); } if (photofromRep.PublicId != null) { var deleteParam = new DeletionParams(photofromRep.PublicId); var result = _cloudinary.Destroy(deleteParam); if (result.Result == "ok") { _repository.Delet(photofromRep); } if (await _repository.SaveAll()) { return(Ok()); } } return(BadRequest("Fail to delete the photo")); }