From ff1c43d80b63dbf4635af996716ebd4417b66acf Mon Sep 17 00:00:00 2001 From: t-eguchi <26-t-eguchi-tmn@it-college.co.jp> Date: Tue, 20 Oct 2026 13:45:30 +0900 Subject: [PATCH] =?UTF-8?q?=E9=80=94=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../demo/controller/AdminLoanController.java | 66 +++++++++++++++---- 1 file changed, 55 insertions(+), 11 deletions(-) diff --git a/book-rent/src/main/java/com/example/demo/controller/AdminLoanController.java b/book-rent/src/main/java/com/example/demo/controller/AdminLoanController.java index f2fe673..d97a265 100644 --- a/book-rent/src/main/java/com/example/demo/controller/AdminLoanController.java +++ b/book-rent/src/main/java/com/example/demo/controller/AdminLoanController.java @@ -5,22 +5,66 @@ import java.util.logging.Logger; import jakarta.servlet.http.HttpSession; import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; -import com.example.demo.entity.BookCollection; -import com.example.demo.entity.BorrowBook; +import com.example.demo.common.BusinessException; +import com.example.demo.common.LoginUser; +import com.example.demo.form.RentalForm; +import com.example.demo.service.LoanService; @Controller public class AdminLoanController { - + // ログ出力用インスタンス - private final static Logger LOGGER = Logger.getLogger(AdminLoanController.class.getName()); + private final static Logger LOGGER = Logger.getLogger(AdminLoanController.class.getName()); - // エラーメッセージ用の定数フィールド - private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!"; - - HttpSession httpSession; - BookCollection bookcollection; - BorrowBook borrowBook; - + // エラーメッセージ用の定数フィールド + private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!"; + private final LoanService loanService; + + public AdminLoanController(LoanService loanService) { + this.loanService = loanService; + } + + + @PostMapping("/submit") + public String executeApproval( + @ModelAttribute("approvalConfilm") RentalForm form, + @RequestParam("action") + String action, + HttpSession session, + RedirectAttributes redirectAttributes) { + + // セッションからログイン中の管理者情報を取得 + LoginUser loginUser = (LoginUser) session.getAttribute("LOGIN_USER"); + if (loginUser == null) { + return "redirect:/login"; + } + + try { + if ("approve".equals(action)) { + // 確定したLoanServiceのインスタンスメソッド呼び出し + loanService.approve(form.getIdRequest(), loginUser); + redirectAttributes.addFlashAttribute("successMessage", "申請を承認しました。"); + + } else if ("reject".equals(action)) { + // 確定したLoanServiceのインスタンスメソッドを呼び出し、IDにはidRequestを渡す + loanService.reject(form.getIdRequest(), loginUser); + redirectAttributes.addFlashAttribute("successMessage", "申請を却下しました。"); + + } else { + throw new BusinessException("不正な操作が行われました。"); + } + + } catch (BusinessException e) { + redirectAttributes.addFlashAttribute("errorMessage", e.getMessage()); + return "redirect:/admin/rental/detail/" + form.getIdRequest(); + } + + return "redirect:/applicationList"; + } }