Skip to content

Commit 3dc2594

Browse files
feat(user): extend duedate borrow book and add extend request UI
1 parent 635a4f7 commit 3dc2594

21 files changed

+947
-49
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
3+
* Click nbfs://nbhost/SystemFileSystem/Templates/JSP_Servlet/Servlet.java to edit this template
4+
*/
5+
package com.library.controller.admin;
6+
7+
import com.library.enums.RequestStatus;
8+
import com.library.factory.ServiceFactory;
9+
import com.library.model.dto.BorrowedBookDTO;
10+
import com.library.model.dto.ExtendRequestDTO;
11+
import com.library.model.entity.Borrowing;
12+
import com.library.model.entity.ExtendRequest;
13+
import com.library.model.entity.User;
14+
import com.library.service.BorrowingService;
15+
import com.library.service.ExtendBookService;
16+
import com.library.service.UserService;
17+
import com.library.util.MailTransfer;
18+
import java.io.IOException;
19+
import java.io.PrintWriter;
20+
import jakarta.servlet.ServletException;
21+
import jakarta.servlet.annotation.WebServlet;
22+
import jakarta.servlet.http.HttpServlet;
23+
import jakarta.servlet.http.HttpServletRequest;
24+
import jakarta.servlet.http.HttpServletResponse;
25+
import jakarta.servlet.http.HttpSession;
26+
import java.time.LocalDateTime;
27+
28+
/**
29+
*
30+
* @author hieuchu
31+
*/
32+
@WebServlet(name = "ConfirmExtendController", urlPatterns = {"/user/request-extend-book"})
33+
public class ConfirmExtendController extends HttpServlet {
34+
35+
private final ExtendBookService extendSerivce = ServiceFactory.getExtendBookService();
36+
private final BorrowingService borrowService = ServiceFactory.getBorrowService();
37+
private final UserService userSerivce = ServiceFactory.getUserService();
38+
39+
@Override
40+
protected void doGet(HttpServletRequest request, HttpServletResponse response)
41+
throws ServletException, IOException {
42+
HttpSession session = request.getSession(false);
43+
BorrowedBookDTO dto = (BorrowedBookDTO) session.getAttribute("bookExtend");
44+
request.setAttribute("dto", dto);
45+
request.getRequestDispatcher("/WEB-INF/views/user/request_extend_book.jsp").forward(request, response);
46+
}
47+
48+
@Override
49+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
50+
throws ServletException, IOException {
51+
52+
HttpSession session = request.getSession(false);
53+
BorrowedBookDTO dto = (BorrowedBookDTO) session.getAttribute("bookExtend");
54+
int extensionDays = Integer.parseInt(request.getParameter("extensionDays"));
55+
String account = request.getParameter("account");
56+
String fullName = request.getParameter("fullName");
57+
int borrowingID = borrowService.getBorrowingID(dto.getBookID(), account);
58+
Borrowing borrow = new Borrowing();
59+
borrow.setBorrowingID(borrowingID);
60+
int userID = userSerivce.getUserIDByAccount(account);
61+
User u = new User();
62+
u.setUserID(userID);
63+
ExtendRequestDTO e = new ExtendRequestDTO();
64+
e.setBorrowing(borrow);
65+
e.setUser(u);
66+
if (extendSerivce.limitExtend(dto.getBookID(), account) > 4) {
67+
extendSerivce.insertData(e);
68+
session.removeAttribute("bookExtend");
69+
session.removeAttribute("targetBookID");
70+
response.sendRedirect(request.getContextPath() + "/user/dashboard");
71+
}
72+
73+
}
74+
75+
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ public class BorrowedBooksListController extends HttpServlet {
3131
protected void doGet(HttpServletRequest request, HttpServletResponse response)
3232
throws ServletException, IOException {
3333
HttpSession session = request.getSession(false);
34-
if (session == null || session.getAttribute("account") == null) {
35-
response.sendRedirect(request.getContextPath() + "/Login");
36-
return ;
37-
}
34+
3835
String account = (String) session.getAttribute("account");
3936

4037
List<BorrowedBookDTO> borrowedBooks = borrowDao.borrowedBooksList(account);

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,26 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
4343
throws ServletException, IOException {
4444

4545
HttpSession session = request.getSession(false);
46-
if (session == null || session.getAttribute("account") == null) {
47-
response.sendRedirect(request.getContextPath() + "/Login");
48-
return;
49-
}
46+
5047
String account = (String) session.getAttribute("account");
5148
int bookID = Integer.valueOf(request.getParameter("bookID"));
5249

5350
LocalDate borrowDate = borrowService.getBorrowDate(bookID);
5451
String dueDate = request.getParameter("newDueDate");
5552
LocalDate newDueDate = LocalDate.parse(dueDate);
56-
List<BorrowedBookDTO> borrowedBooks = borrowService.borrowedBooksList(account);
57-
request.setAttribute("borrowedBooks", borrowedBooks);
53+
54+
List<BorrowedBookDTO> borrowedBooks = borrowService.borrowedBooksList(account);
55+
56+
BorrowedBookDTO dto = extendBookService.getBorrowdBookFromList(borrowedBooks);
57+
58+
request.setAttribute("borrowedBooks", borrowedBooks);
59+
60+
String title = "Extend DueDate - Library System";
61+
5862
long day = ChronoUnit.DAYS.between(borrowDate, newDueDate); //take day
5963
if (borrowService.getExtendCount(bookID, account) > 4) {
60-
session.setAttribute("error", "You’re out of renewals");
64+
session.setAttribute("bookExtend", dto);
65+
session.setAttribute("error", "You’re out of renewal");
6166
session.setAttribute("targetBookID", bookID); // show popup when update book is failed
6267
request.getRequestDispatcher("/WEB-INF/views/borrowing/borrowedbooks.jsp").forward(request, response);
6368
return;

src/java/com/library/controller/filter/AutholizationURLController.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,12 @@ public static final String[] pageForUser() {
1818
"/borrowing/returned",
1919
"/borrowing/return",
2020
"/borrowing/extend",
21+
"/user/request-extend-book",
2122
// Favorites
2223
"/favorite/add-book",
2324
"/favorite/books",
2425
// Books
25-
"/book/list",
26-
"/book/detail",
27-
"/book/search",
28-
"/book/category",
26+
2927
// User info
3028
"/user/dashboard",
3129
"/user/setting",

src/java/com/library/controller/filter/AutholizationUserController.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,37 +44,41 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
4444

4545
String[] publicPaths = {
4646
"/login", "/register", "/book/list",
47-
"/resource/", "/images/", ".css", ".js", ".png", ".jpg", "/user/forgot-password"
47+
"/resource/", "/images/", ".css", ".js", ".png", ".jpg", "/user/forgot-password",
48+
"/book/list",
49+
"/book/detail",
50+
"/book/search",
51+
"/book/category",
4852
};
4953
for (String p : publicPaths) {
5054
if (path.contains(p)) {
5155
chain.doFilter(servletRequest, servletResponse);
5256
return;
5357
}
5458
}
55-
59+
5660
if (user == null) {
5761
response.sendRedirect(request.getContextPath() + "/user/login");
5862
return;
5963
}
60-
if (user.getRole().equals("user")) {
64+
if (user.getRole().equals("user")) {
6165
for (String url : AutholizationURLController.pageForUser()) {
62-
if(path.contains(url)){
63-
chain.doFilter(servletRequest, servletResponse);
64-
return ;
65-
}
66+
if (path.contains(url)) {
67+
chain.doFilter(servletRequest, servletResponse);
68+
return;
69+
}
6670
}
6771
}
68-
69-
if (user.getRole().equals("admin")) {
72+
73+
if (user.getRole().equals("admin")) {
7074
for (String url : AutholizationURLController.pageForAdmin()) {
71-
if(path.contains(url)){
72-
chain.doFilter(servletRequest, servletResponse);
73-
return ;
74-
}
75+
if (path.contains(url)) {
76+
chain.doFilter(servletRequest, servletResponse);
77+
return;
78+
}
7579
}
76-
}
77-
80+
}
81+
7882
}
7983

8084
}

src/java/com/library/controller/user/ForgotPassword.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import com.library.exception.AccountNotExistException;
99
import com.library.exception.ValidationException;
1010
import com.library.factory.ServiceFactory;
11-
import com.library.service.MailService;
11+
import com.library.util.MailTransfer;
1212
import com.library.service.TrackingUserService;
1313
import com.library.service.UserService;
1414
import com.library.util.HashPassword;
@@ -52,9 +52,9 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
5252
userService.isAccountExist(account);
5353
String tmp = RandomPassword.generatePassword();
5454
String title = "Password Recovery - Library System";
55-
String message = "<p>Your New Pass : <b>" + tmp + "</b> </p>";
55+
String newPassword= "<p>Your New Pass : <b>" + tmp + "</b> </p>";
5656
userService.updatePassword(account, HashPassword.hash(tmp));
57-
MailService.send(account, title, message);
57+
MailTransfer.send(account, title, newPassword);
5858
session.setAttribute("message", "we have sent your password via email");
5959
response.sendRedirect(request.getContextPath() + "/user/forgot-password");
6060
} catch (ValidationException e) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,6 @@ public interface BorrowingDao {
5050
int getExtendCount(int bookId, String account);
5151

5252
boolean incrementExtendCount(int bookId, String account);
53+
54+
int getBorrowingIdByBookId(int bookId, String account);
5355
}

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public int totalReturnedBooks(String account) {
7272
@Override
7373
public List<BorrowedBookDTO> borrowedBooksList(String account) {
7474
List<BorrowedBookDTO> list = new ArrayList<>();
75-
String sql = "SELECT bk.cover_image, b.borrow_date, b.due_date, bk.slug , bk.book_id "
75+
String sql = "SELECT bk.cover_image, b.borrow_date, b.due_date, bk.slug , bk.book_id , bk.title "
7676
+ "FROM borrowings b "
7777
+ "JOIN users u ON u.user_id = b.user_id "
7878
+ "JOIN books bk ON bk.book_id = b.book_id "
@@ -88,6 +88,7 @@ public List<BorrowedBookDTO> borrowedBooksList(String account) {
8888
dto.setBorrowDate(rs.getDate("borrow_date").toLocalDate());
8989
dto.setDueDate(rs.getDate("due_date").toLocalDate());
9090
dto.setCoverImage(rs.getString("cover_image"));
91+
dto.setName(rs.getString("title"));
9192
list.add(dto);
9293
}
9394
} catch (SQLException s) {
@@ -341,5 +342,26 @@ public boolean incrementExtendCount(int bookId, String account) {
341342
}
342343
return false;
343344
}
345+
346+
@Override
347+
public int getBorrowingIdByBookId(int bookId, String account) {
348+
String sql = "SELECT b.borrowing_id " +
349+
"FROM borrowings b " +
350+
"JOIN users u ON b.user_id = u.user_id " +
351+
"WHERE b.book_id = ? AND u.account = ? AND b.status = 'borrowing'";
352+
353+
try (Connection conn = DBConnection.getInstance().getConnection();
354+
PreparedStatement ps = conn.prepareStatement(sql)) {
355+
ps.setInt(1, bookId);
356+
ps.setString(2, account);
357+
ResultSet rs = ps.executeQuery();
358+
if (rs.next()) {
359+
return rs.getInt("borrowing_id");
360+
}
361+
} catch (SQLException e) {
362+
e.printStackTrace();
363+
}
364+
return -1;
365+
}
344366

345367
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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.dao;
6+
7+
import com.library.model.dto.ExtendRequestDTO;
8+
import com.library.model.entity.ExtendRequest;
9+
10+
/**
11+
*
12+
* @author hieuchu
13+
*/
14+
public interface ExtendRequestDao {
15+
boolean insertExtendRequest(ExtendRequestDTO request) ;
16+
}
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.dao;
6+
7+
import com.library.model.dto.ExtendRequestDTO;
8+
import com.library.model.entity.ExtendRequest;
9+
import com.library.util.DBConnection;
10+
import java.sql.Connection;
11+
import java.sql.PreparedStatement;
12+
import java.sql.SQLException;
13+
14+
/**
15+
*
16+
* @author hieuchu
17+
*/
18+
public class ExtendRequestDaoImpl implements ExtendRequestDao {
19+
20+
@Override
21+
public boolean insertExtendRequest(ExtendRequestDTO dto) {
22+
String sql = "INSERT INTO extend_requests (borrowing_id, user_id, status) VALUES (?, ?, ?)";
23+
try (
24+
Connection conn = DBConnection.getInstance().getConnection();
25+
PreparedStatement ps = conn.prepareStatement(sql)) {
26+
ps.setInt(1, dto.getBorrowing().getBorrowingID());
27+
ps.setInt(2, dto.getUser().getUserID());
28+
ps.setString(3, dto.getStatus().PENDING.getValue());
29+
return ps.executeUpdate() > 0;
30+
} catch (SQLException e) {
31+
e.printStackTrace();
32+
return false;
33+
}
34+
}
35+
36+
}

0 commit comments

Comments
 (0)