Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix optimizing db calls (#24) #63

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api-docs/borrowings-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
17 changes: 13 additions & 4 deletions src/main/java/com/libraryman_api/borrowing/BorrowingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -146,6 +146,11 @@ public synchronized void returnBook(int borrowingId) {
Borrowings borrowing = getBorrowingById(borrowingId)
.orElseThrow(() -> new ResourceNotFoundException("Borrowing not found"));

Optional<Members> 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");
}
Expand All @@ -162,7 +167,8 @@ public synchronized void returnBook(int borrowingId) {
}

borrowing.setReturnDate(new Date());
updateBookCopies(borrowing.getBook().getBookId(), "ADD", 1);
Optional<Book> book = bookService.getBookById(borrowing.getBook().getBookId());
updateBookCopies(book, "ADD", 1);
notificationService.bookReturnedNotification(borrowing);
borrowingRepository.save(borrowing);
}
Expand Down Expand Up @@ -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<Members> member = memberService.getMemberById(borrowing.getMember().getMemberId());
if(!member.isPresent()){
throw new ResourceNotFoundException("Member not found");
}
Fines fine = borrowing.getFine();

if (fine != null && !fine.isPaid()) {
Expand All @@ -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> book = bookService.getBookById(bookId);
public void updateBookCopies(Optional<Book> book, String operation, int numberOfCopies) {

if (book.isPresent()) {
Book bookEntity = book.get();
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/libraryman_api/member/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ public Optional<Members> getMemberById(int memberId) {
*/
public Members addMember(Members member) {
Members currentMember = memberRepository.save(member);
notificationService.accountCreatedNotification(currentMember);
if(currentMember!=null)
notificationService.accountCreatedNotification(currentMember);

return currentMember;
}
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand All @@ -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},
Expand Down Expand Up @@ -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()),
Expand All @@ -234,7 +235,13 @@ public void sendDueDateReminders(){

// Send reminders for each borrowing
for (Borrowings borrowing : borrowingsDueSoon) {
reminderNotification(borrowing);
try {
Optional<Members> 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);
}
}
}

Expand Down