66
77import com .library .dao .BorrowingDao ;
88import com .library .dao .BorrowingDaoImpl ;
9+ import com .library .factory .ServiceFactory ;
910import com .library .model .dto .BorrowedBookDTO ;
11+ import com .library .service .BorrowingService ;
12+ import com .library .service .ExtendBookService ;
1013import java .io .IOException ;
1114import java .io .PrintWriter ;
1215import jakarta .servlet .ServletException ;
2629@ WebServlet (name = "ExtendBook" , urlPatterns = {"/borrowing/extend" })
2730public 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 }
0 commit comments