public async Task <IActionResult> DeleteUserPhoto(int userId, int id) { // check if the id is authorized if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var user = await _repo.GetUser(userId); // check if there is any photo for this user if (!user.Photos.Any(p => p.Id == id)) { return(Unauthorized()); } // check if this photo is main var photoFromRepo = await _repo.GetPhoto(id); if (photoFromRepo.IsMain) { return(BadRequest("You are not allowed to delete main photo")); } // delete photo from cloudinary if (photoFromRepo.PublicId != null) { var deleteParams = new DeletionParams(photoFromRepo.PublicId); var result = _cloudinary.Destroy(deleteParams); // check if the deletion is success if (result.Result == "ok") { _repo.Detele(photoFromRepo); } } // delete photo from randomUser if (photoFromRepo.PublicId == null) { _repo.Detele(photoFromRepo); } // save changes to the db if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Failed on deleting the photo")); }
public async Task <IActionResult> DeleteMessage(int msgId, int userId) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var messageFromRepo = await _repo.GetMessage(msgId); if (messageFromRepo.SenderId == userId) { messageFromRepo.SenderDeleted = true; } if (messageFromRepo.RecipientId == userId) { messageFromRepo.RecipientDeleted = true; } if (messageFromRepo.RecipientDeleted && messageFromRepo.SenderDeleted) { _repo.Detele(messageFromRepo); } if (await _repo.SaveAll()) { return(NoContent()); } throw new Exception("Error on deleting the message"); }