From d3189b27aaa7c6f7dd1dc5b0cffaab2c1e09399a Mon Sep 17 00:00:00 2001 From: Divyansh Singh Solanki <111523290+Divyansh123bn@users.noreply.github.com> Date: Thu, 3 Oct 2024 09:18:40 +0530 Subject: [PATCH] Update MemberService.java Implemented logic to check if the member has any outstanding fines or borrowed books and further deleted all related notifications, borrowings and fines. --- .../libraryman_api/member/MemberService.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/libraryman_api/member/MemberService.java b/src/main/java/com/libraryman_api/member/MemberService.java index 93c8843..96d1172 100644 --- a/src/main/java/com/libraryman_api/member/MemberService.java +++ b/src/main/java/com/libraryman_api/member/MemberService.java @@ -116,10 +116,29 @@ public void deleteMember(int memberId) { Members member = memberRepository.findById(memberId) .orElseThrow(() -> new ResourceNotFoundException("Member not found")); - // TODO: Implement logic to check if the member has any outstanding fines or borrowed books. - // If there are no pending obligations, delete all related notifications, borrowings, and fines. + // Check for outstanding fines + List outstandingFines = fineRepository.findByMemberIdAndIsPaidFalse(memberId); + if (!outstandingFines.isEmpty()) { + throw new MemberDeletionException("Cannot delete member with outstanding fines."); + } + // Check for borrowed books + List borrowedBooks = borrowingRepository.getAllBorrowingsOfAMember(memberId); + if (!borrowedBooks.isEmpty()) { + throw new MemberDeletionException("Cannot delete member with borrowed books."); + } + + // Delete related notifications + notificationRepository.deleteByMemberId(memberId); + + // Delete related borrowings and fines + borrowingRepository.deleteByMemberId(memberId); + fineRepository.deleteByMemberId(memberId); + + // Send account deletion notification notificationService.accountDeletionNotification(member); + + // Delete the member memberRepository.delete(member); } }