public async Task <ActionResult <Users> > DeleteUser(int id) { var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value); if (currentUserId == id) { var user = await _repo.GetUser(id); if (user == null) { return(NotFound("No user found for this Id")); } var messageFromRepo = await _repo.GetMessage(id); if (messageFromRepo != null) { if (messageFromRepo.SenderId == id) { messageFromRepo.SenderDeleted = true; } if (messageFromRepo.RecipientId == id) { messageFromRepo.RecipientDeleted = true; } if (messageFromRepo.SenderDeleted && messageFromRepo.RecipientDeleted) { _repo.Delete(messageFromRepo); } await _repo.SaveAll(); } var likesFrom = await _repo.GetLike(currentUserId); if (likesFrom != null) { if (likesFrom.LikeeId == currentUserId || likesFrom.LikerId == currentUserId) { _repo.Delete <Like>(likesFrom); } await _repo.SaveAll(); } //user.Likees = null; //user.Likers = null; _repo.Delete <Users>(user); await _repo.SaveAll(); return(NoContent()); } return(BadRequest("Problem to delete User")); }
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 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("You can't delete you main photo!")); } if (photoFromRepo.PublicId != null) { var deleteParams = new DeletionParams(photoFromRepo.PublicId); var result = _cloudinary.Destroy(deleteParams); if (result.Result.Equals("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 repo.GetUser(userId); if (!user.Photos.Any(p => p.Id == id)) { return(Unauthorized()); } var photoFromRepo = await repo.GetPhoto(id); if (photoFromRepo.IsMain) { return(BadRequest("მთავარს ვერ წაშლი")); } if (photoFromRepo.PublicId != null) { var deleteParams = new DeletionParams(photoFromRepo.PublicId); var result = cloudinary.Destroy(deleteParams); if (result.Result == "ok") { repo.Delete(photoFromRepo); } } if (photoFromRepo.PublicId == null) { repo.Delete(photoFromRepo); } if (await repo.SaveAll()) { return(Ok()); } return(BadRequest("ვერ წაიშალა ფოტო")); }
public async Task <IActionResult> DeletePhoto(int userid, int photoid) { if (userid != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var dbUser = await _datingrepo.GetUser(userid); var photo = dbUser.Photos.Where(x => x.Id == photoid).FirstOrDefault(); if (photo == null) { return(Unauthorized()); } if (photo.IsMain) { return(BadRequest("You cannot delete main photo")); } if (string.IsNullOrEmpty(photo.PublicID)) { _datingrepo.Delete(photo); } else { var deleteParams = new DeletionParams(photo.PublicID); var result = _cloudinary.Destroy(deleteParams); if (result.Result == "ok") { _datingrepo.Delete(photo); } } if (await _datingrepo.SaveAll()) { return(Ok()); } return(BadRequest("Error setting main photo")); }
public async Task<IActionResult> DeletePhoto(int userId, int id) { // authorization if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)){ return Unauthorized(); } // getting the user var userFromRepo = await _repo.GetUser(userId); if (!userFromRepo.Photos.Any(p => p.Id == id)) { // we can only update the photo if he has that photo return Unauthorized(); } var photoFromRepo = await _repo.GetPhoto(id); if (photoFromRepo.IsMain) { return BadRequest("You cannot delete your main photo"); } // if we have photo in cloud and db if statmement will exec otherwise else 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("Failed to delete the photo"); }
public async Task <IActionResult> Delete(int userid, int messageid) { try { if (userid != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value)) { return(Unauthorized()); } var message = await _datingrepo.GetMessage(messageid); if (message.SenderID == userid) { message.SenderDeleted = true; } if (message.ReceiverID == userid) { message.ReceiverDeleted = true; } if (message.ReceiverDeleted && message.SenderDeleted) { _datingrepo.Delete(message); } if (await _datingrepo.SaveAll()) { return(NoContent()); } throw new Exception("Error while deleting message"); } catch (Exception ex) { throw new Exception("Error while deleting message"); } }