This commit is contained in:
2026-10-20 14:24:20 +09:00
parent 694cb0f621
commit 0b89e45386

View File

@@ -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";