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

Fixed a bug for null values while borrowing a book #23 #42

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 6 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.3</version>
<version>3.3.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com</groupId>
Expand All @@ -27,7 +27,7 @@
<url/>
</scm>
<properties>
<java.version>17</java.version>
<java.version>21</java.version>
</properties>
<dependencies>

Expand Down Expand Up @@ -57,7 +57,10 @@
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@ComponentScan(basePackages = {"com.libraryman_api"})
@EnableAsync
public class LibrarymanApiApplication {

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
33 changes: 23 additions & 10 deletions src/main/resources/application-development.properties
Original file line number Diff line number Diff line change
@@ -1,23 +1,36 @@
## Database Setup First

## Create a Database and add database name where "Add_Your_Database_Name" is below
## Make this file as it was earlier before commiting code

# Change this connection string to this format: jdbc:mysql://{ip_address}:{port}/{database_name}
spring.datasource.url=jdbc:mysql://localhost:3306/Add_Your_Database_Name
# MySQL Database Connection
spring.datasource.url=jdbc:mysql://localhost:3306/library
spring.datasource.username=root
spring.datasource.password=<Add_Your_Password>
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

## Add your Database Username and Password
spring.datasource.username= Add_Your_UserName
spring.datasource.password= Add_Your_Password
# Hibernate Dialect for MySQL 8
spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# Prevent early database interaction
spring.jpa.properties.hibernate.boot.allow_jdbc_metadata_access=false
spring.jpa.hibernate.ddl-auto=update
spring.sql.init.mode=never

# Format SQL
spring.jpa.properties.hibernate.format_sql=true

# Logging for Spring Security
logging.level.org.springframework.security=TRACE

# Error Handling
server.error.include-binding-errors=always
server.error.include-message=always
server.error.include-stacktrace=never
server.error.include-exception=true

logging.level.org.springframework.security = TRACE
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=<YOUR_EMAIL>
spring.mail.password=<YOUR_PASS>
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.domain_name=gmail.com

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}
Loading