public async Task <IActionResult> DeleteMessage(int id, int userId) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var messageFromRepo = await _repo.GetMessage(id); if (messageFromRepo.SenderId == userId) { messageFromRepo.SenderDeleted = true; } if (messageFromRepo.RecipientId == userId) { messageFromRepo.RecipientDeleted = true; } if (messageFromRepo.SenderDeleted && messageFromRepo.RecipientDeleted) { _repo.Delete(messageFromRepo); } if (await _repo.SaveAll()) { return(NoContent()); } throw new Exception("Error while deleting message"); }
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("this photo is main, you can not delete it")); } var deleteParams = new DeletionParams(photoFromRepo.PublicId); var response = _cloudinary.Destroy(deleteParams); if (response.Result == "ok") { _repo.Delete(photoFromRepo); } if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Error while deleting photo")); }