This commit is contained in:
2026-10-20 14:59:26 +09:00
parent b58a996e84
commit b6ada277a2

View File

@@ -1,116 +1,137 @@
package com.example.demo.controller;
//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";
// }
//}
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";
}
}
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";
}
}