public async Task <IActionResult> DeleteMessage(int id, int userId) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var messagesFromRepo = await _repo.GetMessage(id); if (messagesFromRepo.SenderId == userId) { messagesFromRepo.SenderDeleted = true; } if (messagesFromRepo.RecipientId == userId) { messagesFromRepo.ReceiverDeleted = true; } if (messagesFromRepo.SenderDeleted && messagesFromRepo.ReceiverDeleted) { _repo.Delete(messagesFromRepo); } if (await _repo.SaveAll()) { return(NoContent()); } throw new Exception("Error deleting the message"); }
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 photoFromRepo = await _repo.GetPhoto(id); if (photoFromRepo.IsMain) { return(BadRequest("You cannot delete your main photo.")); } if (photoFromRepo.PublicId == null) { _repo.Delete(photoFromRepo); } var deleteParams = new DeletionParams( publicId: photoFromRepo.PublicId ); var result = _cloudinary.Destroy(deleteParams); if (result.Result == "ok") { _repo.Delete(photoFromRepo); } if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Failed to delete photo.")); }