public async Task <IActionResult> DeleteMessage(int userId, int id) { 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 deleting 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 can not delete your main photo")); } if (photoFromRepo.PublicID != null) { var deleteParams = new DeletionParams(photoFromRepo.PublicID); var result = _cloudinary.Destroy(deleteParams); if (result.Result == "ok") { _repo.Delete(photoFromRepo); } } else { _repo.Delete(photoFromRepo); } if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Could not delete the Photo !")); }
public async Task <IActionResult> DeleteSession(int id) { var sessionInDb = await _repo.GetSession(id); if (sessionInDb == null) { return(NotFound()); } _repo.Delete(sessionInDb); if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Could not delete!")); }
public async Task <IActionResult> DeleteReview(ReviewDetailDto dto) { int reviewerId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); var user = await _repo.GetUser(reviewerId); var isAdmin = await _userManager.IsInRoleAsync(user, "Admin"); var reviewInDb = await _repo.GetReview(dto.PenName, dto.BookAsin, dto.ReviewDate, dto.ReviewTitle); if (reviewerId != reviewInDb.ReviewerId && !isAdmin) { return(Unauthorized()); } _repo.Delete(reviewInDb); if (await _repo.SaveAll()) { return(Ok()); } return(BadRequest("Could not delete the review")); }