public async Task <IActionResult> DeleteMessage(int id, int userId) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var message = await _repo.GetMessage(id); if (message.SenderId == userId) { message.SenderDeleted = true; } if (message.RecipientId == userId) { message.RecipientDeleted = true; } if (message.SenderDeleted && message.RecipientDeleted) { _repo.Delete(message); } if (await _repo.SaveAll()) { return(NoContent()); } throw new Exception("we have proplem when message delete"); }
public async Task <IActionResult> DeletePhoto(int userId, int id) { // should user id from token == user is from claim var userIdFromToken = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); if (userIdFromToken != userId) { return(Unauthorized()); } // should user photo id == id var userFromRepo = await _repo.GetUser(userId); if (!userFromRepo.Photos.Any(p => p.Id == id)) { return(Unauthorized()); } // get desired Photo var photo = await _repo.GetPhoto(id); if (photo.IsMain) { return(BadRequest("this image already is main")); } if (photo.CloudinaryId != null) { var deleteParams = new DeletionParams(photo.CloudinaryId); var result = this._cloudinary.Destroy(deleteParams); if (result.Result == "ok") { _repo.Delete(photo); } } else { _repo.Delete(photo); } if (await _repo.SaveAll()) { return(Ok("the photo is deleted")); } return(BadRequest("Error for photo delete")); }
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(p => p.Id == id)) { return(Unauthorized()); } var photo = await _repo.GetPhoto(id); if (photo.IsMain) { return(BadRequest("لا يمكن حذف الصوره الاساسيه")); } if (photo.PublicId != null) { var deleteParams = new DeletionParams(photo.PublicId); var result = this._cloudinary.Destroy(deleteParams); if (result.Result == "ok") { _repo.Delete(photo); } } if (photo.PublicId == null) { _repo.Delete(photo); } if (await _repo.SaveAll()) { return(Ok()); } else { return(BadRequest("فشل حذف الصوره")); } }
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("C'est ta photo principale")); } if (Photo.PublicId != null) { var deleteParams = new DeletionParams(Photo.PublicId); var result = this._cloudinary.Destroy(deleteParams); if (result.Result == "ok") { _repo.Delete(Photo); } } if (Photo.PublicId == null) { _repo.Delete(Photo); } if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest(" la photo n'est pas supprimer ")); }