Skip to content

Commit 635a4f7

Browse files
refactor: improve extend return book
1 parent 081bb07 commit 635a4f7

File tree

7 files changed

+472
-350
lines changed

7 files changed

+472
-350
lines changed

src/java/com/library/controller/borrowing/ExtendBookController.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
import com.library.dao.BorrowingDao;
88
import com.library.dao.BorrowingDaoImpl;
9+
import com.library.factory.ServiceFactory;
910
import com.library.model.dto.BorrowedBookDTO;
11+
import com.library.service.BorrowingService;
12+
import com.library.service.ExtendBookService;
1013
import java.io.IOException;
1114
import java.io.PrintWriter;
1215
import jakarta.servlet.ServletException;
@@ -26,7 +29,8 @@
2629
@WebServlet(name = "ExtendBook", urlPatterns = {"/borrowing/extend"})
2730
public class ExtendBookController extends HttpServlet {
2831

29-
BorrowingDao borrowDao = new BorrowingDaoImpl();
32+
BorrowingService borrowService = ServiceFactory.getBorrowService();
33+
ExtendBookService extendBookService = ServiceFactory.getExtendBookService();
3034

3135
@Override
3236
protected void doGet(HttpServletRequest request, HttpServletResponse response)
@@ -46,21 +50,28 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
4650
String account = (String) session.getAttribute("account");
4751
int bookID = Integer.valueOf(request.getParameter("bookID"));
4852

49-
LocalDate borrowDate = borrowDao.getBorrowDate(bookID);
53+
LocalDate borrowDate = borrowService.getBorrowDate(bookID);
5054
String dueDate = request.getParameter("newDueDate");
5155
LocalDate newDueDate = LocalDate.parse(dueDate);
56+
List<BorrowedBookDTO> borrowedBooks = borrowService.borrowedBooksList(account);
57+
request.setAttribute("borrowedBooks", borrowedBooks);
5258
long day = ChronoUnit.DAYS.between(borrowDate, newDueDate); //take day
59+
if (borrowService.getExtendCount(bookID, account) > 4) {
60+
session.setAttribute("error", "You’re out of renewals");
61+
session.setAttribute("targetBookID", bookID); // show popup when update book is failed
62+
request.getRequestDispatcher("/WEB-INF/views/borrowing/borrowedbooks.jsp").forward(request, response);
63+
return;
64+
}
5365
if (day > 60) {
54-
request.setAttribute("error", "you must not extend the due date by more than 2 months ");
55-
// load list
56-
List<BorrowedBookDTO> borrowedBooks = borrowDao.borrowedBooksList(account);
57-
request.setAttribute("borrowedBooks", borrowedBooks);
58-
request.setAttribute("targetBookID", bookID); // show popup when update book is failed
66+
session.setAttribute("error", "you must not extend the due date by more than 2 months ");
67+
// load list
68+
session.setAttribute("targetBookID", bookID); // show popup when update book is failed
5969
request.getRequestDispatcher("/WEB-INF/views/borrowing/borrowedbooks.jsp").forward(request, response);
6070
return;
6171
}
62-
boolean commitExtraDate = borrowDao.extendDueDay(bookID, newDueDate, account);
72+
boolean commitExtraDate = extendBookService.extendDueDay(bookID, newDueDate, account);
6373
if (commitExtraDate) {
74+
borrowService.incrementExtendCount(bookID, account);
6475
session.setAttribute("extendSuccess", " Due date updated successfully!");
6576
response.sendRedirect(request.getContextPath() + "/borrowing/borrowed?bookID=" + bookID);
6677
}

src/java/com/library/dao/BorrowingDao.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,8 @@ public interface BorrowingDao {
4646
int numberOfBorrowBookOnPerUser();
4747

4848
void approveBorrowing(Connection conn, int borrowId, int adminId);
49+
50+
int getExtendCount(int bookId, String account);
51+
52+
boolean incrementExtendCount(int bookId, String account);
4953
}

src/java/com/library/dao/BorrowingDaoImpl.java

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,11 @@ public boolean hasUserBorrowedBook(int bookID, int userID) {
218218
ps.setString(3, "borrowing");
219219
ResultSet rs = ps.executeQuery();
220220
if (rs.next()) {
221-
return false;
221+
return true;
222222
}
223223
} catch (Exception e) {
224224
}
225-
return true;
225+
return false;
226226
}
227227

228228
@Override
@@ -304,4 +304,42 @@ public void approveBorrowing(Connection conn, int borrowId, int adminId) {
304304

305305
}
306306

307+
@Override
308+
public int getExtendCount(int bookId, String account) {
309+
String sql = "SELECT b.extend_count\n"
310+
+ " FROM borrowings b\n"
311+
+ " JOIN users u ON b.user_id = u.user_id \n"
312+
+ " WHERE b.book_id = ? AND u.account = ?";
313+
314+
try (Connection conn = DBConnection.getInstance().getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
315+
ps.setInt(1, bookId);
316+
ps.setString(2, account);
317+
try (ResultSet rs = ps.executeQuery()) {
318+
if (rs.next()) {
319+
return rs.getInt("extend_count");
320+
}
321+
}
322+
} catch (SQLException e) {
323+
e.printStackTrace();
324+
}
325+
return 0;
326+
}
327+
328+
@Override
329+
public boolean incrementExtendCount(int bookId, String account) {
330+
String sql = "UPDATE b\n"
331+
+ "SET b.extend_count = b.extend_count + 1\n"
332+
+ "FROM borrowings b\n"
333+
+ "JOIN users u ON b.user_id = u.user_id \n"
334+
+ "WHERE b.book_id = ? AND u.account = ?";
335+
try (Connection conn = DBConnection.getInstance().getConnection(); PreparedStatement ps = conn.prepareStatement(sql)) {
336+
ps.setInt(1, bookId);
337+
ps.setString(2, account);
338+
return ps.executeUpdate() > 0;
339+
} catch (SQLException e) {
340+
e.printStackTrace();
341+
}
342+
return false;
343+
}
344+
307345
}

src/java/com/library/factory/ServiceFactory.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.library.service.ActivityService;
88
import com.library.service.BookService;
99
import com.library.service.BorrowingService;
10+
import com.library.service.ExtendBookService;
1011
import com.library.service.FavoriteService;
1112
import com.library.service.RemoveBookService;
1213
import com.library.service.RemoveUserService;
@@ -29,6 +30,7 @@ public final class ServiceFactory {
2930
private static TrackingUserService trackService;
3031
private static UserService userService;
3132
private static RemoveUserService removeUserService;
33+
private static ExtendBookService extendBookSerivce ;
3234

3335
private ServiceFactory() {
3436

@@ -132,4 +134,14 @@ public static RemoveUserService getRemoveUserService() {
132134
}
133135
return removeUserService;
134136
}
137+
138+
public static ExtendBookService getExtendBookService(){
139+
if(extendBookSerivce == null){
140+
extendBookSerivce = new ExtendBookService(
141+
DaoFactory.getBookDao(),
142+
DaoFactory.getBorrowingDao()
143+
);
144+
}
145+
return extendBookSerivce;
146+
}
135147
}

src/java/com/library/service/BorrowingService.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@
1010
import com.library.dao.BorrowingDaoImpl;
1111
import com.library.dao.UserDao;
1212
import com.library.dao.UserDaoImpl;
13+
import com.library.model.dto.BorrowedBookDTO;
1314
import com.library.util.DBConnection;
1415
import java.sql.Connection;
1516
import java.sql.SQLException;
17+
import java.time.LocalDate;
18+
import java.util.List;
1619
import org.slf4j.Logger;
1720
import org.slf4j.LoggerFactory;
1821

@@ -43,7 +46,7 @@ public int getUserIDByAccount(String account) {
4346
}
4447

4548
public boolean canBorrowBook(int bookID, int userID) {
46-
if (this.borrowDao.hasUserBorrowedBook(bookID, userID) && this.borrowDao.numberOfBorrowBookOnPerUser() < 10) {
49+
if (!this.borrowDao.hasUserBorrowedBook(bookID, userID) && this.borrowDao.numberOfBorrowBookOnPerUser() < 10) {
4750
return true;
4851
}
4952
return false;
@@ -54,7 +57,7 @@ public void borrowBook(String slug, int bookID, int userID) {
5457
conn.setAutoCommit(false);
5558
try {
5659
if (this.bookDao.getCurrentQuantity(conn, bookID) > 0) {
57-
this.borrowDao.insertBook(conn, bookID, userID);
60+
this.borrowDao.insertBook(conn, bookID, userID);
5861
conn.commit();
5962
}
6063
} catch (SQLException s) {
@@ -69,7 +72,7 @@ public void approveBorrowRequest(int borrowId, int adminId, int bookID) {
6972
try (Connection conn = DBConnection.getInstance().getConnection()) {
7073
conn.setAutoCommit(false);
7174
try {
72-
this.borrowDao.approveBorrowing(conn, borrowId, adminId);
75+
this.borrowDao.approveBorrowing(conn, borrowId, adminId);
7376
this.bookDao.decreaseQuantity(conn, bookID);
7477
conn.commit();
7578
logger.info("Borrow request {} approved by admin {}", borrowId, adminId);
@@ -82,4 +85,20 @@ public void approveBorrowRequest(int borrowId, int adminId, int bookID) {
8285
}
8386
}
8487

88+
public LocalDate getBorrowDate(int bookID) {
89+
return this.borrowDao.getBorrowDate(bookID);
90+
}
91+
92+
public int getExtendCount(int bookID, String account) {
93+
return this.borrowDao.getExtendCount(bookID, account);
94+
}
95+
96+
public List<BorrowedBookDTO> borrowedBooksList(String account) {
97+
return this.borrowDao.borrowedBooksList(account);
98+
}
99+
100+
public void incrementExtendCount(int bookId, String account) {
101+
this.borrowDao.incrementExtendCount(bookId, account);
102+
}
103+
85104
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
3+
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
4+
*/
5+
package com.library.service;
6+
7+
import com.library.dao.BookDao;
8+
import com.library.dao.BorrowingDao;
9+
import com.library.factory.DaoFactory;
10+
import java.time.LocalDate;
11+
12+
/**
13+
*
14+
* @author hieuchu
15+
*/
16+
public class ExtendBookService {
17+
18+
private final BookDao bookDao;
19+
private final BorrowingDao borrowDao ;
20+
21+
public ExtendBookService(BookDao bookDao, BorrowingDao borrowDao) {
22+
this.bookDao = bookDao;
23+
this.borrowDao = borrowDao;
24+
}
25+
26+
public int limitExtend(int bookID, String account) {
27+
int extendCount = borrowDao.getExtendCount(bookID, account);
28+
return extendCount;
29+
}
30+
31+
public boolean extendDueDay(int bookID, LocalDate newDueDate, String account){
32+
return this.borrowDao.extendDueDay(bookID, newDueDate, account) ;
33+
}
34+
35+
36+
}

0 commit comments

Comments
 (0)