From 0b89e4538649a2d12288162964656f3ae352446b Mon Sep 17 00:00:00 2001 From: t-eguchi <26-t-eguchi-tmn@it-college.co.jp> Date: Tue, 20 Oct 2026 14:24:20 +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 | 64 ++++++++++++++++--- 1 file changed, 55 insertions(+), 9 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 d97a265..603826a 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,23 +5,25 @@ import java.util.logging.Logger; import jakarta.servlet.http.HttpSession; import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.PathVariable; 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.common.BusinessException; +import com.example.demo.common.Const; import com.example.demo.common.LoginUser; +import com.example.demo.dto.BorrowView; 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 String SEVERE_ERROR_MESSAGE = "エラーが発生しました!"; private final LoanService loanService; @@ -31,28 +33,68 @@ public class AdminLoanController { } + @GetMapping("/admin/applications/{id}") + public String showApprovalConfirm(@PathVariable("id") Integer idRequest, HttpSession session, Model model) { + + // セッションからログイン情報のチェック + LoginUser loginUser = (LoginUser) session.getAttribute("LOGIN_USER"); + if (loginUser == null) { + return "redirect:/login"; + } + + // エラーによる差し戻し(Form)がModel内に存在する場合は、二重に作成しない + if (!model.containsAttribute("approvalConfilm")) { + try { + // LoanServiceから現在の承認待ち状態のデータを取得 + BorrowView view = loanService.getView(idRequest, Const.STATUS_WAITING); + + // 取得したデータを画面用の RentalForm(approvalConfilm)に詰め替える + RentalForm form = new RentalForm(); + form.setIdRequest(view.getIdRequest()); + form.setNmEmployee(view.getNmApplicant()); + form.setDateRequest(view.getDateRequest() != null ? view.getDateRequest().toString() : ""); + form.setIsbn(view.getIsbn()); + form.setNmBook(view.getNmBook()); + form.setPublisher(view.getPublisher()); + form.setNote(view.getNote() != null ? view.getNote() : ""); + form.setDateBorrow(view.getDateBorrow() != null ? view.getDateBorrow().toString() : ""); + form.setDateScheduledReturn( + view.getDateScheduledReturn() != null ? view.getDateScheduledReturn().toString() : ""); + + // 画面オブジェクトをモデルに設定 + model.addAttribute("approvalConfilm", form); + + } catch (BusinessException e) { + // 対象データがない、または既に別の人に処理されていた場合は、一覧画面にメッセージ付きで戻す + model.addAttribute("errorMessage", e.getMessage()); + return "redirect:/applicationList"; + } + } + + // 書籍貸出承認確認画面(HTMLファイル名)を呼び出す + return "admin/approvalConfirm"; + } + @PostMapping("/submit") public String executeApproval( @ModelAttribute("approvalConfilm") RentalForm form, - @RequestParam("action") - String action, + @RequestParam("action") String action, HttpSession session, RedirectAttributes redirectAttributes) { - // セッションからログイン中の管理者情報を取得 + // セッションからログイン中の管理者情報を取得 LoginUser loginUser = (LoginUser) session.getAttribute("LOGIN_USER"); if (loginUser == null) { return "redirect:/login"; } try { + // アクション(ボタンのvalue属性)に応じたサービス処理の実行 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", "申請を却下しました。"); @@ -62,7 +104,11 @@ public class AdminLoanController { } catch (BusinessException e) { redirectAttributes.addFlashAttribute("errorMessage", e.getMessage()); - return "redirect:/admin/rental/detail/" + form.getIdRequest(); + + // エラーが起きた入力データをそのまま保持して詳細(確認)画面に戻す + redirectAttributes.addFlashAttribute("approvalConfilm", form); + + return "redirect:/admin/applications/" + form.getIdRequest(); } return "redirect:/applicationList";