From 8d1988ea2dc3b453755ae1f93b91a1b6a1b922a4 Mon Sep 17 00:00:00 2001 From: Talupula Sahithi Date: Sat, 5 Oct 2024 14:48:02 +0530 Subject: [PATCH 1/2] optimized db calls in borrowing,member and notification services --- api-docs/borrowings-api.md | 2 +- .../borrowing/BorrowingService.java | 17 +++++++++++++---- .../libraryman_api/member/MemberService.java | 6 ++++-- .../notification/NotificationService.java | 19 +++++++++++++------ 4 files changed, 31 insertions(+), 13 deletions(-) diff --git a/api-docs/borrowings-api.md b/api-docs/borrowings-api.md index e797bd7..d6a180d 100644 --- a/api-docs/borrowings-api.md +++ b/api-docs/borrowings-api.md @@ -143,7 +143,7 @@ The Borrowings API provides a set of endpoints to manage the borrowing and retur ### 3. **Pay Fine** -**Endpoint:** `/borrowings/{id}/pay-fine` +**Endpoint:** `/borrowings/{id}/pay` **Method:** `PUT` **Description:** Pays the fine associated with a borrowing. diff --git a/src/main/java/com/libraryman_api/borrowing/BorrowingService.java b/src/main/java/com/libraryman_api/borrowing/BorrowingService.java index 3a7d785..2b7efd9 100644 --- a/src/main/java/com/libraryman_api/borrowing/BorrowingService.java +++ b/src/main/java/com/libraryman_api/borrowing/BorrowingService.java @@ -109,7 +109,7 @@ public synchronized Borrowings borrowBook(Borrowings borrowing) { Members memberEntity = member.get(); if (bookEntity.getCopiesAvailable() > 0) { - updateBookCopies(borrowing.getBook().getBookId(), "REMOVE", 1); + updateBookCopies(book, "REMOVE", 1); borrowing.setBorrowDate(new Date()); borrowing.setBook(bookEntity); borrowing.setMember(memberEntity); @@ -146,6 +146,11 @@ public synchronized void returnBook(int borrowingId) { Borrowings borrowing = getBorrowingById(borrowingId) .orElseThrow(() -> new ResourceNotFoundException("Borrowing not found")); + Optional member = memberService.getMemberById(borrowing.getMember().getMemberId()); + if(!member.isPresent()){ + throw new ResourceNotFoundException("Member not found"); + } + if (borrowing.getReturnDate() != null) { throw new ResourceNotFoundException("Book has already been returned"); } @@ -162,7 +167,8 @@ public synchronized void returnBook(int borrowingId) { } borrowing.setReturnDate(new Date()); - updateBookCopies(borrowing.getBook().getBookId(), "ADD", 1); + Optional book = bookService.getBookById(borrowing.getBook().getBookId()); + updateBookCopies(book, "ADD", 1); notificationService.bookReturnedNotification(borrowing); borrowingRepository.save(borrowing); } @@ -192,6 +198,10 @@ private Fines imposeFine(Borrowings borrowing) { public String payFine(int borrowingId) { Borrowings borrowing = getBorrowingById(borrowingId) .orElseThrow(() -> new ResourceNotFoundException("Borrowing not found")); + Optional member = memberService.getMemberById(borrowing.getMember().getMemberId()); + if(!member.isPresent()){ + throw new ResourceNotFoundException("Member not found"); + } Fines fine = borrowing.getFine(); if (fine != null && !fine.isPaid()) { @@ -217,8 +227,7 @@ public String payFine(int borrowingId) { * @param numberOfCopies the number of copies to add or remove * @throws ResourceNotFoundException if the book is not found or if there are not enough copies to remove */ - public void updateBookCopies(int bookId, String operation, int numberOfCopies) { - Optional book = bookService.getBookById(bookId); + public void updateBookCopies(Optional book, String operation, int numberOfCopies) { if (book.isPresent()) { Book bookEntity = book.get(); diff --git a/src/main/java/com/libraryman_api/member/MemberService.java b/src/main/java/com/libraryman_api/member/MemberService.java index e5c4d2a..2968a4a 100644 --- a/src/main/java/com/libraryman_api/member/MemberService.java +++ b/src/main/java/com/libraryman_api/member/MemberService.java @@ -79,7 +79,8 @@ public Optional getMemberById(int memberId) { */ public Members addMember(Members member) { Members currentMember = memberRepository.save(member); - notificationService.accountCreatedNotification(currentMember); + if(currentMember!=null) + notificationService.accountCreatedNotification(currentMember); return currentMember; } @@ -105,7 +106,8 @@ public Members updateMember(int memberId, Members memberDetails) { member.setRole(memberDetails.getRole()); member.setMembershipDate(memberDetails.getMembershipDate()); member = memberRepository.save(member); - notificationService.accountDetailsUpdateNotification(member); + if(member!=null) + notificationService.accountDetailsUpdateNotification(member); return member; } diff --git a/src/main/java/com/libraryman_api/notification/NotificationService.java b/src/main/java/com/libraryman_api/notification/NotificationService.java index 0ad4205..a79b04b 100644 --- a/src/main/java/com/libraryman_api/notification/NotificationService.java +++ b/src/main/java/com/libraryman_api/notification/NotificationService.java @@ -15,6 +15,9 @@ import java.util.Calendar; import java.util.Date; import java.util.List; +import java.util.Optional; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** @@ -29,6 +32,7 @@ public class NotificationService { private final EmailSender emailSender; private final NotificationRepository notificationRepository; private final MemberRepository memberRepository; + private static final Logger LOGGER = LoggerFactory.getLogger(NotificationService.class); /** * Constructs a new {@code NotificationService} with the specified {@link EmailSender}, @@ -201,14 +205,11 @@ public void bookReturnedNotification(Borrowings borrowing) { * @param notification the notification instance containing information about the notification. */ private void sendNotification(Notifications notification) { - Members member = memberRepository.findByMemberId(notification.getMember().getMemberId()) - .orElseThrow(() -> new ResourceNotFoundException("Member not found")); - emailSender.send( - member.getEmail(), + notification.getMember().getEmail(), buildEmail( subject(notification.getNotificationType()), - member.getName(), + notification.getMember().getName(), notification.getMessage() ), subject(notification.getNotificationType()), @@ -234,7 +235,13 @@ public void sendDueDateReminders(){ // Send reminders for each borrowing for (Borrowings borrowing : borrowingsDueSoon) { - reminderNotification(borrowing); + try { + Optional member = memberRepository.findByMemberId(borrowing.getMember().getMemberId()); + if(member.isPresent()) + reminderNotification(borrowing); + } catch (ResourceNotFoundException e) { + LOGGER.error("Member not found for memberId: " + borrowing.getMember().getMemberId(), e); + } } } From ddc6b0d4a974d5f65fc007da7569c92664728dcb Mon Sep 17 00:00:00 2001 From: Talupula Sahithi Date: Mon, 7 Oct 2024 17:20:23 +0530 Subject: [PATCH 2/2] Resolved merge conflict in application.properties --- src/main/resources/application.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index eef557c..53718a7 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,2 +1,2 @@ spring.application.name=libraryman-api -spring.profiles.active=${ENV:development} +spring.profiles.active=${ENV:development} \ No newline at end of file