Skip to content

Commit

Permalink
Merge pull request #51 from shreyamaheshwari1/Library2
Browse files Browse the repository at this point in the history
@23made final edits for rectifying null values
  • Loading branch information
ajaynegi45 authored Oct 2, 2024
2 parents eb50835 + a734df4 commit 99e765c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
<scope>runtime</scope>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/com/libraryman_api/borrowing/BorrowingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.libraryman_api.fine.Fines;
import com.libraryman_api.exception.ResourceNotFoundException;
import com.libraryman_api.fine.FineRepository;
import com.libraryman_api.member.MemberService;
import com.libraryman_api.member.Members;
import com.libraryman_api.notification.NotificationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -39,6 +41,7 @@ public class BorrowingService {
private final FineRepository fineRepository;
private final NotificationService notificationService;
private final BookService bookService;
private final MemberService memberService;

/**
* Constructs a new {@code BorrowingService} with the specified repositories and services.
Expand All @@ -48,11 +51,12 @@ public class BorrowingService {
* @param notificationService the service for sending notifications
* @param bookService the service for managing book records
*/
public BorrowingService(BorrowingRepository borrowingRepository, FineRepository fineRepository, NotificationService notificationService, BookService bookService) {
public BorrowingService(BorrowingRepository borrowingRepository, FineRepository fineRepository, NotificationService notificationService, BookService bookService, MemberService memberService) {
this.borrowingRepository = borrowingRepository;
this.fineRepository = fineRepository;
this.notificationService = notificationService;
this.bookService = bookService;
this.memberService=memberService;
}

/**
Expand Down Expand Up @@ -89,13 +93,16 @@ public Optional<Borrowings> getBorrowingById(int borrowingId) {
@Transactional
public synchronized Borrowings borrowBook(Borrowings borrowing) {
Optional<Book> book = bookService.getBookById(borrowing.getBook().getBookId());

if (book.isPresent()) {
Optional<Members> member = memberService.getMemberById(borrowing.getMember().getMemberId());
if (book.isPresent() && member.isPresent()) {
Book bookEntity = book.get();
Members memberEntity = member.get();

if (bookEntity.getCopiesAvailable() > 0) {
updateBookCopies(borrowing.getBook().getBookId(), "REMOVE", 1);
borrowing.setBorrowDate(new Date());
borrowing.setBook(bookEntity);
borrowing.setMember(memberEntity);
borrowing.setDueDate(calculateDueDate());

Borrowings savedBorrowing = borrowingRepository.save(borrowing);
Expand All @@ -107,7 +114,10 @@ public synchronized Borrowings borrowBook(Borrowings borrowing) {
throw new ResourceNotFoundException("Not enough copies available");
}
} else {
throw new ResourceNotFoundException("Book not found");
if (book.isEmpty()) {
throw new ResourceNotFoundException("Book not found");
}
throw new ResourceNotFoundException("Member not found");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application-production.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ spring.mail.username=${MAIL_SERVICE_USERNAME}
spring.mail.password=${MAIL_SERVICE_PASSWORD}
spring.mail.properties.mail.smtp.auth=${MAIL_SERVICE_SMTP}
spring.mail.properties.mail.starttls.enable=${MAIL_SERVICE_STARTTLS}
spring.mail.properties.domain_name=${MAIL_SERVICE_DOMAIN_NAME}
spring.mail.properties.domain_name=${MAIL_SERVICE_DOMAIN_NAME}
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
spring.application.name=libraryman-api
spring.profiles.active=${ENV:dev}
spring.profiles.active=${ENV:development}

0 comments on commit 99e765c

Please sign in to comment.