public async Task <IActionResult> DeleteMessage(int id, int userId) { if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var message = await _datingRepository.GetMessage(id); if (message.SenderId == userId) { message.SenderDeleted = true; } if (message.RecipientId == userId) { message.RecipientDeleted = true; } if (message.SenderDeleted && message.RecipientDeleted) { _datingRepository.Delete(message); } if (await _datingRepository.SaveAll()) { return(NoContent()); } throw new Exception("Failed to delete this message"); }
public async Task <ActionResult> 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(x => x.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 deletionParams = new DeletionParams(photoFromRepo.PublicId); var result = _cloudinary.Destroy(deletionParams); 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()); } var user = await _datingAppRepository.GetUser(userId); if (!user.Photos.Any(p => p.Id == id)) { return(Unauthorized()); } var photo = await _datingAppRepository.GetPhoto(id); if (photo.IsMain) { return(BadRequest("You cannot delete the main photo")); } if (!string.IsNullOrEmpty(photo.PublicId)) { var deleteParams = new DeletionParams(photo.PublicId); var result = await _cloudinary.DestroyAsync(deleteParams); if (result.Result == "ok") { _datingAppRepository.Delete(photo); } } else { _datingAppRepository.Delete(photo); } if (await _datingAppRepository.SaveAll()) { return(Ok()); } return(BadRequest("Failed to delet the photo")); }