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 b101600..d453b82 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 @@ -1,137 +1,118 @@ -//package com.example.demo.controller; -// -//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; -// -// public AdminLoanController(LoanService loanService) { -// this.loanService = loanService; -// } -// -// -// @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, -// HttpSession session, -// RedirectAttributes redirectAttributes) { -// -// // セッションからログイン中の管理者情報を取得 -// LoginUser loginUser = (LoginUser) session.getAttribute("LOGIN_USER"); -// if (loginUser == null) { -// return "redirect:/login"; -// } -// -// try { -// // アクション(ボタンのvalue属性)に応じたサービス処理の実行 -// if ("approve".equals(action)) { -// loanService.approve(form.getIdRequest(), loginUser); -// redirectAttributes.addFlashAttribute("successMessage", "申請を承認しました。"); -// -// } else if ("reject".equals(action)) { -// loanService.reject(form.getIdRequest(), loginUser); -// redirectAttributes.addFlashAttribute("successMessage", "申請を却下しました。"); -// -// } else { -// throw new BusinessException("不正な操作が行われました。"); -// } -// -// } catch (BusinessException e) { -// redirectAttributes.addFlashAttribute("errorMessage", e.getMessage()); -// -// // エラーが起きた入力データをそのまま保持して詳細(確認)画面に戻す -// redirectAttributes.addFlashAttribute("approvalConfilm", form); -// -// return "redirect:/admin/applications/" + form.getIdRequest(); -// } -// -// return "redirect:/applicationList"; -// } -//} - +package com.example.demo.controller; +import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.SessionAttribute; +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.service.LoanService; +/** + * 管理者用:LOAN-02 申請一覧、LOAN-03 承認/却下、LOAN-04 貸出中一覧、LOAN-05 返却。 + * /admin/** は LoginCheckInterceptor で管理者以外を拒否する。 + */ +@Controller public class AdminLoanController { private final LoanService loanService; - + public AdminLoanController(LoanService loanService) { this.loanService = loanService; } - + // ===== LOAN-02 書籍申請一覧(承認待) ===== @GetMapping("/admin/applications") public String applications(Model model) { model.addAttribute("list", loanService.listByStatus(Const.STATUS_WAITING)); return "applicationList"; } -} \ No newline at end of file + + // ===== LOAN-03 承認/却下 ===== + @GetMapping("/admin/applications/{id}") + public String approvalConfirm(@PathVariable Integer id, Model model, RedirectAttributes ra) { + try { + model.addAttribute("r", loanService.getView(id, Const.STATUS_WAITING)); + return "approvalConfirm"; + } catch (BusinessException e) { + ra.addFlashAttribute("errorMessage", e.getMessage()); + return "redirect:/admin/applications"; + } + } + + @PostMapping("/admin/applications/{id}/approve") + public String approve(@PathVariable Integer id, + @SessionAttribute(Const.SESSION_LOGIN_USER) LoginUser admin, RedirectAttributes ra) { + try { + loanService.approve(id, admin); + ra.addFlashAttribute("message", "書籍貸出申請を承認しました。"); + return "redirect:/admin/approval/complete"; + } catch (BusinessException e) { + ra.addFlashAttribute("errorMessage", e.getMessage()); + return "redirect:/admin/applications"; + } + } + + @PostMapping("/admin/applications/{id}/reject") + public String reject(@PathVariable Integer id, + @SessionAttribute(Const.SESSION_LOGIN_USER) LoginUser admin, RedirectAttributes ra) { + try { + loanService.reject(id, admin); + ra.addFlashAttribute("message", "書籍貸出申請を却下しました。"); + return "redirect:/admin/approval/complete"; + } catch (BusinessException e) { + ra.addFlashAttribute("errorMessage", e.getMessage()); + return "redirect:/admin/applications"; + } + } + + @GetMapping("/admin/approval/complete") + public String approvalComplete(Model model) { + model.addAttribute("backUrl", "/admin/applications"); + model.addAttribute("backLabel", "承認待ち一覧に戻る"); + return "complete"; + } + + // ===== LOAN-04 貸出中書籍一覧 ===== + @GetMapping("/admin/lendings") + public String lendings(Model model) { + model.addAttribute("list", loanService.listByStatus(Const.STATUS_LENDING)); + return "lendingList"; + } + + // ===== LOAN-05 返却 ===== + @GetMapping("/admin/lendings/{id}") + public String returnConfirm(@PathVariable Integer id, Model model, RedirectAttributes ra) { + try { + model.addAttribute("r", loanService.getView(id, Const.STATUS_LENDING)); + return "returnConfirm"; + } catch (BusinessException e) { + ra.addFlashAttribute("errorMessage", e.getMessage()); + return "redirect:/admin/lendings"; + } + } + + @PostMapping("/admin/lendings/{id}/return") + public String doReturn(@PathVariable Integer id, + @SessionAttribute(Const.SESSION_LOGIN_USER) LoginUser admin, RedirectAttributes ra) { + try { + loanService.returnBook(id, admin); + ra.addFlashAttribute("message", "書籍の返却が完了しました。"); + return "redirect:/admin/return/complete"; + } catch (BusinessException e) { + ra.addFlashAttribute("errorMessage", e.getMessage()); + return "redirect:/admin/lendings"; + } + } + + @GetMapping("/admin/return/complete") + public String returnComplete(Model model) { + model.addAttribute("backUrl", "/admin/lendings"); + model.addAttribute("backLabel", "貸出中書籍一覧に戻る"); + return "complete"; + } +} +