2段階_途中まで

This commit is contained in:
2026-10-14 16:50:59 +09:00
parent 1a09ef83be
commit b08d51d187
8 changed files with 193 additions and 64 deletions

View File

@@ -0,0 +1,5 @@
package com.example.rhizome.app.domain.service;
public interface DeleteDepartment {
void deleteDepartment(Integer idDepartment);
}

View File

@@ -0,0 +1,22 @@
package com.example.rhizome.app.domain.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.rhizome.app.domain.service.DeleteDepartment;
import com.example.rhizome.app.infra.repository.DepartmentRepository;
public class DeleteDepartmentImpl implements DeleteDepartment {
DepartmentRepository departmentRepository;
@Autowired
public DeleteDepartmentImpl(DepartmentRepository departmentRepository) {
this.departmentRepository = departmentRepository;
}
public void deleteDepartment(Integer idDepartment) {
// ★考える:Repository のどのメソッドで「IDを指定して削除」できる?
// (お手本の DeleteEmployeeImpl を見る)
departmentRepository.deleteById(idDepartment);
}
}

View File

@@ -1,11 +1,13 @@
package com.example.rhizome.app.domain.service.impl; package com.example.rhizome.app.domain.service.impl;
import com.example.rhizome.app.domain.service.DeleteEmployee;
import com.example.rhizome.app.infra.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import com.example.rhizome.app.domain.service.DeleteEmployee;
import com.example.rhizome.app.infra.repository.EmployeeRepository;
/** /**
* Employee レコード削除関連の機能を持つサービス * Employee レコード削除関連の機能を持つサービス
*/ */

View File

@@ -0,0 +1,72 @@
package com.example.rhizome.app.presentation.controller;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.example.rhizome.app.domain.service.DeleteDepartment;
import com.example.rhizome.app.domain.service.SearchDepartment;
import com.example.rhizome.app.domain.service.SearchEmployee;
import com.example.rhizome.app.presentation.controller.pageproperty.PageReturnAttributeKeyword;
import com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword;
import com.example.rhizome.app.presentation.form.DepartmentForm;
import com.example.rhizome.app.presentation.form.SearchConditionDepartmentForm;
@Controller
public class DeleteDepartmentController {
private final static Logger LOGGER = Logger.getLogger(DeleteDepartmentController.class.getName());
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
HttpSession httpSession;
DeleteDepartment deleteDepartment;
SearchDepartment searchDepartment;
SearchEmployee searchEmployee; // 所属社員の確認に使う(既存)
//コンストラクタ
@Autowired
public DeleteDepartmentController(HttpSession httpSession, DeleteDepartment deleteDepartment,
SearchDepartment searchDepartment, SearchEmployee searchEmployee) {
this.httpSession = httpSession;
this.deleteDepartment = deleteDepartment;
this.searchDepartment = searchDepartment;
this.searchEmployee = searchEmployee;
}
@RequestMapping(value = TransitionTargetPageNameKeyword.DELETE_DEPARTMENT, method = RequestMethod.POST)
public String deleteDepartment(@RequestParam(value = "idDepartment", required = false) Integer idDepartment, Model model) {
final String UNKNOWN_ID = "削除対象のIDが不明です。";
final String HAS_EMPLOYEE = "1名以上が所属している部署は削除出来ません。";
final String DELETE_COMPLETE = "削除しました。";
try {
// ★① idDepartment が null、または searchDepartment.searchBy(new SearchConditionDepartment(idDepartment, null)) が空
// → MESSAGE_ERROR に UNKNOWN_ID を載せて一覧へ
deleteDepartment.deleteDepartment(idDepartment);
// ★② searchEmployee.searchEmployeeByIdDepartment(idDepartment) が空でない
// → MESSAGE_ERROR に HAS_EMPLOYEE を載せて一覧へ
model.addAttribute(PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST,DepartmentForm.convertFrom(searchDepartment.selectAll()));
// ★③ deleteDepartment.deleteDepartment(idDepartment) → MESSAGE_INFO に DELETE_COMPLETE を載せて一覧へ
model.addAttribute("searchConditinDepartmentForm", new SearchConditionDepartmentForm());
// return "一覧へ戻る(ステップ 2-5 で決める)";
return TransitionTargetPageNameKeyword.SELECT_DEPARTMENT_HTML;
} catch (Exception e) {
httpSession.invalidate();
e.printStackTrace();
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR,"部署を削除できませんでした");
// return TransitionTargetPageNameKeyword.ERROR_HTML;
return TransitionTargetPageNameKeyword.SELECT_DEPARTMENT_HTML;
}
}
}

View File

@@ -1,6 +1,5 @@
package com.example.rhizome.app.presentation.controller; package com.example.rhizome.app.presentation.controller;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
//import java.util.List; //import java.util.List;
@@ -33,14 +32,15 @@ public class SelectDepartmentController {
// エラーメッセージ用の定数フィールド // エラーメッセージ用の定数フィールド
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!"; private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
// private static final String DEPARTMENT_HTML = "/department"; // public static final String SELECT_DEPARTMENT_FORM_EMPTY = "/selectDepartmentAll";
// private static final String DEPARTMENT_HTML = "/department";
HttpSession httpSession; HttpSession httpSession;
SearchDepartment searchDepartment; SearchDepartment searchDepartment;
@Autowired @Autowired
public SelectDepartmentController (HttpSession httpSession, SearchDepartment serchDepartment) { public SelectDepartmentController(HttpSession httpSession, SearchDepartment serchDepartment) {
this.httpSession = httpSession; this.httpSession = httpSession;
this.searchDepartment = serchDepartment; this.searchDepartment = serchDepartment;
} }
@@ -57,15 +57,12 @@ public class SelectDepartmentController {
// ★考える:①検索サービスを呼ぶ(Form→Model変換 → 部分一致のメソッド) // ★考える:①検索サービスを呼ぶ(Form→Model変換 → 部分一致のメソッド)
// ②結果(List<Department>)を DepartmentForm.convertFrom(...) で画面用に変換 // ②結果(List<Department>)を DepartmentForm.convertFrom(...) で画面用に変換
// ③ model に PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST という名前で載せる // ③ model に PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST という名前で載せる
model.addAttribute(PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST,DepartmentForm model.addAttribute(PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST, DepartmentForm
.convertFrom(searchDepartment.searchByWithLike(SearchConditionDepartmentForm .convertFrom(searchDepartment.searchByWithLike(SearchConditionDepartmentForm
.convertTo(searchConditionDepartmentForm) .convertTo(searchConditionDepartmentForm))));
)
)
);
return TransitionTargetPageNameKeyword.SELECT_DEPARTMENT_HTML; return TransitionTargetPageNameKeyword.SELECT_DEPARTMENT_HTML;
}catch(Exception e) { } catch (Exception e) {
//httpSessionを破棄 //httpSessionを破棄
httpSession.invalidate(); httpSession.invalidate();
@@ -79,11 +76,24 @@ public class SelectDepartmentController {
return TransitionTargetPageNameKeyword.ERROR_HTML; return TransitionTargetPageNameKeyword.ERROR_HTML;
} }
} }
@RequestMapping(value = TransitionTargetPageNameKeyword.SELECT_DEPARTMENT_FORM_EMPTY, method = RequestMethod.POST)
public String select(Model model) {
try {
// ★全件取得 → DepartmentForm に変換 → DEPARTMENT_FORM_LIST で model へ
return TransitionTargetPageNameKeyword.SELECT_DEPARTMENT_HTML;
} catch (Exception e) {
httpSession.invalidate();
e.printStackTrace();
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
return TransitionTargetPageNameKeyword.ERROR_HTML;
}
}
} }
/**
/**
* 社員検索画面表示処理コントローラ * 社員検索画面表示処理コントローラ
* *
* @param searchConditionEmployeeForm 検索画面のフォーム * @param searchConditionEmployeeForm 検索画面のフォーム
@@ -109,12 +119,9 @@ public class SelectDepartmentController {
// SearchConditionEmployeeForm.convertTo(searchConditionEmployeeForm()) // SearchConditionEmployeeForm.convertTo(searchConditionEmployeeForm())
// ); // );
// /** // /**
// * 社員検索画面表示処理コントローラ // * 社員検索画面表示処理コントローラ
// * 検索条件無しの状態を作るため、空の検索条件で検索した結果を画面に返す // * 検索条件無しの状態を作るため、空の検索条件で検索した結果を画面に返す
// * // *
// * @param model ビューにデータを渡すためのコンテナ // * @param model ビューにデータを渡すためのコンテナ
// * @return 遷移先の画面 // * @return 遷移先の画面

View File

@@ -33,6 +33,8 @@ public class SelectEmployeeController {
// エラーメッセージ用の定数フィールド // エラーメッセージ用の定数フィールド
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!"; private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
public static final String SELECT_DEPARTMENT_FORM_EMPTY = "/selectDepartmentAll";
HttpSession httpSession; HttpSession httpSession;
SearchDepartment searchDepartment; SearchDepartment searchDepartment;
SearchEmployeeInfo searchEmployeeInfo; SearchEmployeeInfo searchEmployeeInfo;

View File

@@ -56,9 +56,15 @@ public class TransitionTargetPageNameKeyword {
// Department // Department
// HTML 実装手順書ステップ1-2通りの定数追加 // HTML 実装手順書ステップ1-2通りの定数追加
public static final String SELECT_DEPARTMENT_HTML = "selectDepartment"; public static final String SELECT_DEPARTMENT_HTML = "selectDepartment";
// Controller 実装手順書ステップ1-2通りの定数追加 -> 管理者向けと重複、エラーでたため一旦コメアウト // Controller 実装手順書ステップ1-2通りの定数追加 -> 管理者向けと重複、エラーでたため一旦コメアウト
public static final String SELECT_DEPARTMENT = "/selectDepartment"; public static final String SELECT_DEPARTMENT = "/selectDepartment";
// ステップ2-3*TransitionTargetPageNameKeyword の Department(Controller) の枠に追記
public static final String DELETE_DEPARTMENT = "/deleteDepartment";
public static final String SELECT_DEPARTMENT_FORM_EMPTY = "/selectDepartmentAll";
/** /**
* 管理者向けのページリストを返す * 管理者向けのページリストを返す
* *

View File

@@ -1,4 +1,17 @@
<!DOCTYPE html> <!DOCTYPE html>
<!--<html> HTML文書の開始タグ-->
<!--<head> ページの設定情報を含む部分-->
<!--<title> ページのタイトルを指定するタグ-->
<!--<body> 実際に表示される内容を記述する部分-->
<!--<h1> 見出し最大サイズ・<p>段落の基本タグ-->
<!--<div> 汎用のブロック要素 複数の要素をまとめてレイアウトするためのコンテナ-->
<!--<table> 表全体を囲む-->
<!--<tr> 行全体を囲む-->
<!--<th> 行見出しセルを囲む-->
<!--<td> 行データセルを囲む-->
<!--<a href ="URL"> 飛び先のURLを指定"> -->
<html xmlns:th="http://www.thymeleaf.org"> <html xmlns:th="http://www.thymeleaf.org">
<head> <head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type">