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("Cannot delete your main photo")); } if (photoFromRepo.PublicId != null) { var deleteParams = new DeletionParams(photoFromRepo.PublicId); var result = _cloudinary.Destroy(deleteParams); if (result.Result == "ok") { _repo.Delete(photoFromRepo); } } if (photoFromRepo.PublicId == null) { _repo.Delete(photoFromRepo); } if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Failed to delete the photo")); }
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(NotFound("This photo you are looking for was not found")); } if (photoFromRepo.IsMain) { return(BadRequest("You cannot delete the main photo")); } if (photoFromRepo.PublicId != null) { var deleteParams = new DeletionParams(photoFromRepo.PublicId); var result = _cloudinary.Destroy(deleteParams); if (result.Result == "ok") { _repo.Delete(photoFromRepo); } } if (photoFromRepo.PublicId == null) { _repo.Delete(photoFromRepo); } if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Failed to delete the photo")); }
public async Task <IActionResult> DeletePhoto(int userId, int id) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var user = await _datingRepo.GetUser(userId); if (!user.Photos.Any(p => p.Id == id)) { return(Unauthorized()); } var photoForActive = await _datingRepo.GetPhoto(id); if (photoForActive.IsActive) { return(BadRequest("You can not delete the active photo.")); } if (photoForActive.PublicId != null) { var deleteParam = new DeletionParams(photoForActive.PublicId); var result = _cloudinary.Destroy(deleteParam); if (result.Result == "ok") { _datingRepo.Delete(photoForActive); } } if (photoForActive.PublicId == null) { _datingRepo.Delete(photoForActive); } if (await _datingRepo.SaveAll()) { return(Ok()); } return(BadRequest("Failled to delete photo.")); }
public async Task <IActionResult> DeletePhoto(int userId, int id) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var user = await _datingRepository.GetUser(userId, true); if (!user.Photos.Any(p => p.Id == id)) { return(Unauthorized()); } var photo = await _datingRepository.GetPhoto(id); if (photo.IsMain) { return(BadRequest("Could not delete main photo")); } if (photo.PublicId != null) { var deleteParams = new DeletionParams(photo.PublicId); var result = _cloudinary.Destroy(deleteParams); if (result.Result == "ok") { _datingRepository.Delete(photo); } } else { _datingRepository.Delete(photo); } if (await _datingRepository.SaveAll()) { return(Ok()); } return(BadRequest("Delete photo failed")); }
public async Task <IActionResult> DeletePhoto(int userId, int id) { if (IsUnauthorized(userId)) { return(Unauthorized()); } var photoFromRepo = await repo.GetPhoto(id); if (photoFromRepo == null) { return(NotFound()); } if (photoFromRepo.IsMain) { return(BadRequest("You cannot delete the main photo")); } if (photoFromRepo.PublicId != null) { var deleteParams = new DeletionParams(photoFromRepo.PublicId); var result = cloudinary.Destroy(deleteParams); if (result.Result == "ok") { repo.Delete(photoFromRepo); } } if (photoFromRepo.PublicId == null) { repo.Delete(photoFromRepo); } if (await repo.SaveAll()) { return(Ok()); } return(BadRequest("Failed to delete the photo")); }
public async Task <IActionResult> DeletePhoto(int userId, int id) { // check if the user id editing the page is the one from the token 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()); // if passed photo id does not exist } var photoFromRepo = await repo.GetPhoto(id); if (photoFromRepo.IsMain) { return(BadRequest("You cannot delete your main photo!")); } if (photoFromRepo.PublicId != null) { var deleteParams = new DeletionParams(photoFromRepo.PublicId); var result = cloudinary.Destroy(deleteParams); // for cloudinary deletion if (result.Result == "ok") { repo.Delete(photoFromRepo); } } else { repo.Delete(photoFromRepo); } if (await repo.SaveAll()) { return(Ok()); // return OK for delete methods } return(BadRequest("Failed to delete photo!")); }
public async Task <IActionResult> DeletePhoto(int userId, int id) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await this._repository.GetUser(userId); if (!userFromRepo.Photos.Any(p => (p.Id == id))) { return(Unauthorized()); } var photoFromRepo = await _repository.GetPhoto(id); if (photoFromRepo.IsMain == true) { return(BadRequest("Cannot not delete main photo!!")); } if (photoFromRepo.PublicID != null) { var deleteParams = new DeletionParams(photoFromRepo.PublicID); var result = _cloudinary.Destroy(deleteParams); if (result.Result == "ok") { _repository.Delete(photoFromRepo); } } else { _repository.Delete(photoFromRepo); } if (await _repository.SaveAll()) { return(Ok()); } return(BadRequest("Cannot delete Photo!!")); }
public async Task <IActionResult> DeletePhoto(int userid, int id) { if (userid != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(userid); if (!userFromRepo.Photos.Any(p => p.Id == id)) { return(Unauthorized()); } var photo = await _repo.GetPhoto(id); if (photo.IsMain) { return(BadRequest("You cannot delete your main photo.")); } if (!string.IsNullOrWhiteSpace(photo.PublicId)) { var deleteParams = new DeletionParams(photo.PublicId); var result = _cloudinary.Destroy(deleteParams); if (result.Result == "ok") { _repo.Delete(photo); } } else { _repo.Delete(photo); } if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Error while deleting the photo")); }
public async Task <IActionResult> DeletePhoto(int userId, int id) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) //Check user Authorise Or not { return(Unauthorized()); } var user = await _repo.GetUser(userId); if (!user.Photos.Any(p => p.Id == id)) //cHECK PHOTOS FOR iD IN cOLLECTION { return(Unauthorized()); } var photoFromRepo = await _repo.GetPhoto(id); if (photoFromRepo.IsMain) { return(BadRequest("You can not Delete Youtr Main Photo")); } if (photoFromRepo.PublicId != null) { var deleteParams = new DeletionParams(photoFromRepo.PublicId); var result = _cloudinary.Destroy(deleteParams); //result var return that Deleted ITEM. if (result.Result == "ok") { _repo.Delete(photoFromRepo); } } if (photoFromRepo.PublicId == null) { _repo.Delete(photoFromRepo); // This Condition is For NON-Cloudinary Photo } if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Failed to Delete the Photo")); }
public async Task <IActionResult> DeletePhoto(int userId, int id) { if (!ValidateAuthenticationUserId(userId)) { 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 cannot delete your main photo")); } if (photoFromRepo.PublicId != null) { var result = _cloudinary.Destroy(new DeletionParams(photoFromRepo.PublicId)); if (result.Result == "ok") { _repo.Delete(photoFromRepo); } } else { _repo.Delete(photoFromRepo); } if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Failed to delete the photo")); }
public async Task <IActionResult> DeletePhoto(int userId, int id) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); // sprawdzamy czy użytkownik jest bieżącym użytkownikiem } var user = await _repo.GetUser(userId); if (!user.Photos.Any(p => p.Id == id)) { return(Unauthorized()); // jeśli, żadne id zdjęcia nie pasuje do tego w tabeli zdjęcia zwracamy nieautoryzowany dostęp } var photoFromRepo = await _repo.GetPhoto(id); if (photoFromRepo.IsMain) { return(BadRequest("You cannot delete your main photo")); // sprawdzamy czy zdjęcie jest już głównym } if (photoFromRepo.PublicId != null) { var deleteParams = new DeletionParams(photoFromRepo.PublicId); // parametry, czyli publicID var result = _cloudinary.Destroy(deleteParams); // rezultat z serwisu cloudinary if (result.Result == "ok") { _repo.Delete(photoFromRepo); // jeśli się powiedzie to usuwamy } } if (photoFromRepo.PublicId == null) // dla zdjęć, które nie są w serwisie cloudinary { _repo.Delete(photoFromRepo); } if (await _repo.SaveAll()) // i zapisujemy zmiany { return(Ok()); } return(BadRequest("Failed to delete the photo")); }
public async Task <IActionResult> DeletePhoto(int userId, int id) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(userId, true); if (!userFromRepo.Photos.Any(a => a.Id == id)) { return(Unauthorized()); } var photoToDelete = await _repo.GetPhoto(id); if (photoToDelete.IsMain) { return(BadRequest("You cannot delete your main photo.")); } if (!string.IsNullOrWhiteSpace(photoToDelete.PublicId)) { var result = _cloudinary.Destroy(new DeletionParams(photoToDelete.PublicId)); if (result.StatusCode == System.Net.HttpStatusCode.OK) { _repo.Delete(photoToDelete); } } else { _repo.Delete(photoToDelete); } if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Failed to delete the photo.")); }
public async Task <IActionResult> DeletePhoto(int userid, int id) { 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(BadRequest("There is no photo with the id you provided")); } var photoFromRepo = await _repository.GetPhoto(id); if (photoFromRepo.IsMain) { return(BadRequest("Main Photo can't be deleted")); } if (photoFromRepo.PublicId != null) { var delParams = new DeletionParams(photoFromRepo.PublicId); var resFromCloud = _cloudinary.Destroy(delParams); if (resFromCloud.Result == "ok") { _repository.Delete(photoFromRepo); } } else { _repository.Delete(photoFromRepo); } if (await _repository.SaveAll()) { return(Ok()); } return(BadRequest("Unable to delete the photo")); }
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 => Id == p.Id)) { return(Unauthorized()); } var photoFromRepo = await _Repo.GetPhoto(Id); if (photoFromRepo.IsMain) { return(BadRequest("You cannot delete your main photo")); } if (null != photoFromRepo.PublicId) { var result = _Cloudinary.Destroy(new DeletionParams(photoFromRepo.PublicId)); if ("ok" == result.Result) { _Repo.Delete(photoFromRepo); } } else { _Repo.Delete(photoFromRepo); } if (await _Repo.SaveAll()) { return(Ok()); } return(BadRequest("Failed to dfelete the photo")); }
public async Task <IActionResult> DeletePhoto(int userId, int idPhoto) { if (!IsUserAuthorized(userId)) { return(Unauthorized()); } var dbUser = await _datingRepository.GetUser(userId); if (!dbUser.Photos.Any(p => p.Id == idPhoto)) { return(Unauthorized()); } var dbPhoto = await _datingRepository.GetPhoto(idPhoto); if (dbPhoto.IsMain) { return(BadRequest("You cannot delete yout main photo!")); } if (dbPhoto.PublicId != null) { var deleteParams = new DeletionParams(dbPhoto.PublicId); var result = _cloudinary.Destroy(deleteParams); if (result.Result == "ok") { _datingRepository.Delete(dbPhoto); } } else { _datingRepository.Delete(dbPhoto); } if (await _datingRepository.SaveAll()) { return(Ok()); } return(BadRequest("Failed to delete the photo")); }
public async Task <IActionResult> DeletePhoto(int userId, int id) { //handle error section if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var user = await _repo.GetUser(userId); //check this user had photo with id if (!user.Photos.Any(p => p.Id == id)) { return(Unauthorized()); // status code 401 } var photoFromRepo = await _repo.GetPhoto(id); if (photoFromRepo.IsMain == true) { return(BadRequest("You cannot delete your main photo")); } //end handle error section if (photoFromRepo.PublicId != null) // Seed Data(Generate random pic) has not publicID, so don't use cloudinary { var deletionParams = new DeletionParams(photoFromRepo.PublicId); var result = _cloudinary.Destroy(deletionParams); // return ok if success check on document if (result.Result == "ok") { _repo.Delete(photoFromRepo); } } else // delete seed data { _repo.Delete(photoFromRepo); } if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Failed to delete the photo")); }
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 cannot delete your main photo")); } if (string.IsNullOrWhiteSpace(photoFromRepo.PublicId)) { _repo.Delete(photoFromRepo); } else { var deleteParams = new DeletionParams(photoFromRepo.PublicId); var result = _cloudinary.Destroy(deleteParams); if (string.Equals(result.Result, "ok", StringComparison.OrdinalIgnoreCase)) { _repo.Delete(photoFromRepo); } } if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Failed to delete the photo")); }
public async Task <IActionResult> DeletePhotoForUser(int userId, int id) { //authorization if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } //checking if the user trying to set the photo is among from his own photo. var userFromRepo = await _repo.GetUser(userId); if (!userFromRepo.Photos.Any(p => p.Id == id)) { return(BadRequest("Photo could not found")); } //checking if the photo is already a main photo. var photoFromRepo = await _repo.GetPhoto(id); if (photoFromRepo.IsMain) { return(BadRequest("Cannot delete main photo")); } if (photoFromRepo.PublicId != null) { var deleteParams = new DeletionParams(photoFromRepo.PublicId); var result = _cloudinary.Destroy(deleteParams); if (result.Result == "ok") { _repo.Delete(photoFromRepo); } } _repo.Delete(photoFromRepo); if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("could not delete photo")); }
public async Task <IActionResult> DeletePhoto(Guid userId, Guid id) { var claim = User.FindFirst(ClaimTypes.NameIdentifier).Value; if (userId.ToString() != claim) { return(Unauthorized("Access Denied")); } var user = await _repository.GetUser(userId); if (!user.Photos.Any(x => x.PhotoUniqueIdentifier == id)) { return(Unauthorized()); } var photoFromRepository = await _repository.GetPhoto(id); if (photoFromRepository.IsMain) { return(BadRequest("Cannot delete main photo")); } if (photoFromRepository.PublicId != null) { var deleteParams = new DeletionParams(photoFromRepository.PublicId); var result = await cloudinary.DestroyAsync(deleteParams); if (result.Result == "ok") { _repository.Delete(photoFromRepository); } } if (photoFromRepository.PublicId == null) { _repository.Delete(photoFromRepository); } if (await _repository.SaveAll()) { return(Ok()); } return(BadRequest("Unable to delete photo")); }
public async Task <IActionResult> DeletePhoto(long userId, int id) { if (userId != long.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(StatusCode(StatusCodes.Status401Unauthorized)); } var user = await _datingRepository.GetUser(userId); if (!user.Photos.Any(x => x.Id == id)) { return(StatusCode(StatusCodes.Status401Unauthorized)); } var photo = await this._datingRepository.GetPhoto(id); if (photo.IsProfilePic) { return(StatusCode(StatusCodes.Status400BadRequest, "You cannot delete your profile photo.")); } if (string.IsNullOrWhiteSpace(photo.PublicId)) { _datingRepository.Delete(photo); } else { var deleteParams = new DeletionParams(photo.PublicId); var result = await _cloudinary.DestroyAsync(deleteParams); if (result.Result == "ok") { _datingRepository.Delete(photo); } } if (await _datingRepository.SaveAll()) { return(StatusCode(StatusCodes.Status200OK)); } return(StatusCode(StatusCodes.Status400BadRequest, "Failed to delete photo")); }
public async Task <IActionResult> DeletePhoto(int id, int userid) { int claimid = int.Parse(User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier).Value); if (userid != claimid) { return(Unauthorized()); } var user = await rep.GetUser(userid); var photo = user.Photos.FirstOrDefault(e => e.Id == id); // check to see if the photo exists if (photo == null) { return(Unauthorized()); } var publicid = photo.PublicId; if (publicid == null) // yani tu cloudinary save nis { rep.Delete(photo); } else { //documentation cloudinary tozih dade k chejuri bayad hazf konm DeletionParams p = new DeletionParams(publicid); var result = c.Destroy(p); if (result.Result == "ok") { rep.Delete(photo); } } if (await rep.SaveAll()) { return(NoContent()); } throw new Exception("can not delete photo"); }
public async Task <IActionResult> DeleteUserImage(int userId, int photoId) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } Models.User userFromRepo = await _datingRepo.GetUser(userId); if (!userFromRepo.Photos.Any(p => p.Id == photoId)) { return(Unauthorized()); } Models.Photo photoFromRepo = await _datingRepo.GetPhoto(photoId); if (photoFromRepo.IsMain) { return(BadRequest("You cannot delete your main photo")); } if (photoFromRepo.PublicId != null) { DeletionParams deleteParams = new DeletionParams(photoFromRepo.PublicId); var result = _cloudinary.Destroy(deleteParams); if (result.Result.ToLower() == "ok") { _datingRepo.Delete(photoFromRepo); } } else { _datingRepo.Delete(photoFromRepo); } if (await _datingRepo.SaveAll()) { return(Ok()); } return(BadRequest("Error occurred while deleting photo")); }
public async Task <IActionResult> DeletePhoto(int userId, int id) { var user = await _repo.GetOwnUser(userId); if (!user.Photos.Any(p => p.Id == id)) { return(Unauthorized()); } var photoFromRepo = await _repo.GetPhoto(id); if (photoFromRepo.IsMain) { photoFromRepo.IsMain = false; } if (photoFromRepo.PublicId != null) { var deleteParams = new DeletionParams(photoFromRepo.PublicId); var result = _cloudinary.Destroy(deleteParams); if (result.Result == "ok") { _repo.Delete(photoFromRepo); } } if (photoFromRepo.PublicId == null) { _repo.Delete(photoFromRepo); } if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Failed to delete the photo")); }
public async Task <IActionResult> DeletePhotoAudit(int auditId, int id) { var audit = await _repo.GetAudit(auditId); if (!audit.PhotoAudits.Any(p => p.Id == id)) { return(Unauthorized()); } var photoAuditFromRepo = await _repo.GetPhotoAudit(id); if (photoAuditFromRepo.IsMain) { return(BadRequest("You cannot delete your main photo")); } if (photoAuditFromRepo.PublicId != null) { var deleteParams = new DeletionParams(photoAuditFromRepo.PublicId); var result = _cloudinary.Destroy(deleteParams); if (result.Result == "ok") { _repo.Delete(photoAuditFromRepo); } } if (photoAuditFromRepo.PublicId == null) { _repo.Delete(photoAuditFromRepo); } if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Failed to delete the photo")); }
public async Task <IActionResult> DeletePhoto(int userId, int photoId) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var user = await _repository.GetUser(userId); if (!user.Photos.Any(p => p.Id == photoId)) { return(Unauthorized()); } var photoFromRepo = await _repository.GetPhoto(photoId); if (photoFromRepo.IsMain) { return(BadRequest("You cannot delete main photo")); } if (photoFromRepo.PublicId != null) { var response = _cloudinary.Destroy(new DeletionParams(photoFromRepo.PublicId)); if (response.Result == "ok") { _repository.Delete(photoFromRepo); } } else { _repository.Delete(photoFromRepo); } if (await _repository.SaveAll()) { return(Ok()); } return(Unauthorized()); }
public async Task <IActionResult> DeletePhoto(int userId, int id) { // first it check the current user is valid through token if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(userId); if (!userFromRepo.Photos.Any(p => p.Id == id)) { return(Unauthorized()); } var photoList = userFromRepo.Photos.Where(p => p.Id == id).ToList(); if (photoList[0].PublicId != null) { var deleteParms = new DeletionParams(photoList[0].PublicId); var result = _cloudinary.Destroy(deleteParms); if (result.Result == "ok") { _repo.Delete(photoList[0]); } } if (photoList[0].PublicId == null) { _repo.Delete(photoList[0]); } if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Faild to delete!")); }
public async Task <IActionResult> DeletePhoto(int userId, int photoId) { // check the right user is trying to edit photo if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } // check that photo we want to set as main belongs to user's photos var user = await _datingRepo.GetUserById(userId); if (!user.Photos.Any(p => p.Id == photoId)) { return(Unauthorized()); } var photoFromRepo = await _datingRepo.GetPhoto(photoId); if (photoFromRepo.IsMainPhoto) { return(BadRequest("Cannot delete your main photo")); } // delete from cloudinary if (photoFromRepo.PublicId != null) { var result = _cloudinary.Destroy(new DeletionParams(photoFromRepo.PublicId)); if (result.Result == "ok") { _datingRepo.Delete(photoFromRepo); } } else { _datingRepo.Delete(photoFromRepo); } if (await _datingRepo.SaveAll()) { return(Ok()); } return(BadRequest("Failed to delete photo")); }
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(NotFound()); } if (photoFromRepo.IsMain) { return(BadRequest(" You can't delete main photo ")); } var currentMainPhoto = await _repo.GetMainPhotoForUser(userId); if (photoFromRepo.PublicId != null) { var deleteParams = new DeletionParams(photoFromRepo.PublicId); var result = _cloudinary.Destroy(deleteParams); if (result.Result.ToLower() == "ok") { _repo.Delete(photoFromRepo); } } if (photoFromRepo.PublicId == null) { _repo.Delete(photoFromRepo); } if (await _repo.SaveAll()) { return(Ok("Delete Successfully")); } return(BadRequest("Fail to delete photo")); }
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.All(o => o.Id != id)) { return(NotFound()); } var photoFromRepo = await _repo.GetPhoto(id); if (photoFromRepo.IsMain) { return(BadRequest("Cannot delete main photo.")); } if (photoFromRepo.PublicId != null) { var result = _cloudinary.Destroy(new DeletionParams(photoFromRepo.PublicId)); if (result.Result == "ok") { _repo.Delete(photoFromRepo); } } else { _repo.Delete(photoFromRepo); } return((await _repo.SaveAll()) ? Ok() : throw new ApplicationException("Error deleting photo.")); }
public async Task <IActionResult> DeletePhoto(int userId, int photoId) { var photoFromRepo = await _repository.Get <Photo>(photoId); if (photoFromRepo == null) { return(NotFound("Could not find photo")); } if (photoFromRepo.UserId != userId) { return(Unauthorized()); } if (photoFromRepo.IsMain) { return(BadRequest("You cannot delete the main photo")); } if (photoFromRepo.PublicId != null) { var deleteResult = _cloudinaryService.DeleteImage(photoFromRepo.PublicId); if (deleteResult.Result == "ok") { _repository.Delete(photoFromRepo); } } else { _repository.Delete(photoFromRepo); } if (await _repository.SaveAll()) { return(Ok()); } return(BadRequest("Failed to delete a photo")); }