Merge branch 'h-hein2' of https://gitea.it-college.co.jp/2026_0910_AI_5G_Java_MUN/last-group-delevelopment-team2 into o-matsuda
This commit is contained in:
@@ -1,5 +1,178 @@
|
||||
package com.example.demo.controller;
|
||||
|
||||
public class EmployeeController {
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
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.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
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.entity.Employee;
|
||||
import com.example.demo.form.EmployeeForm;
|
||||
import com.example.demo.service.EmployeeService;
|
||||
|
||||
/**
|
||||
* EMP-01 社員登録(管理者のみ:/admin/** で拒否)
|
||||
* EMP-02 社員編集(管理者:メールアドレスで検索して全項目 / 一般:自分のパスワードのみ)
|
||||
*/
|
||||
@Controller
|
||||
public class EmployeeController {
|
||||
private static final String NEW = "/admin/employees/new";
|
||||
private static final String EDIT = "/employees/edit";
|
||||
|
||||
private final EmployeeService employeeService;
|
||||
|
||||
public EmployeeController(EmployeeService employeeService) {
|
||||
this.employeeService = employeeService;
|
||||
}
|
||||
|
||||
// ===== EMP-01 社員登録 =====
|
||||
@GetMapping(NEW)
|
||||
public String registInput(Model model) {
|
||||
return registView(new EmployeeForm(), List.of(), model);
|
||||
}
|
||||
|
||||
@PostMapping(NEW + "/input")
|
||||
public String registBack(@ModelAttribute("form") EmployeeForm form, Model model) {
|
||||
return registView(form, List.of(), model);
|
||||
}
|
||||
|
||||
@PostMapping(NEW + "/confirm")
|
||||
public String registConfirm(@ModelAttribute("form") EmployeeForm form, Model model) {
|
||||
List<String> errors = employeeService.validateRegist(form);
|
||||
if (!errors.isEmpty())
|
||||
return registView(form, errors, model);
|
||||
return "employeeRegistConfirm";
|
||||
}
|
||||
|
||||
@PostMapping(NEW + "/execute")
|
||||
public String registExecute(@ModelAttribute("form") EmployeeForm form,
|
||||
@SessionAttribute(Const.SESSION_LOGIN_USER) LoginUser admin,
|
||||
Model model, RedirectAttributes ra) {
|
||||
try {
|
||||
employeeService.register(form, admin);
|
||||
} catch (BusinessException e) {
|
||||
return registView(form, List.of(e.getMessage()), model);
|
||||
}
|
||||
ra.addFlashAttribute("message", "社員登録が完了しました。");
|
||||
return "redirect:" + NEW + "/complete";
|
||||
}
|
||||
|
||||
@GetMapping(NEW + "/complete")
|
||||
public String registComplete(Model model) {
|
||||
return complete(model);
|
||||
}
|
||||
|
||||
private String registView(EmployeeForm form, List<String> errors, Model model) {
|
||||
model.addAttribute("form", form);
|
||||
model.addAttribute("errors", errors);
|
||||
return "employeeRegistInput";
|
||||
}
|
||||
|
||||
// ===== EMP-02 社員編集 =====
|
||||
|
||||
/** メニュー「社員編集」:管理者は検索画面へ、一般ユーザーは検索せず自分の編集画面へ */
|
||||
@GetMapping("/employees/search")
|
||||
public String search(@SessionAttribute(Const.SESSION_LOGIN_USER) LoginUser user) {
|
||||
return user.isAdmin() ? "employeeSearch" : "redirect:" + EDIT;
|
||||
}
|
||||
|
||||
@PostMapping("/employees/search")
|
||||
public String doSearch(@RequestParam(defaultValue = "") String mailAddress,
|
||||
@SessionAttribute(Const.SESSION_LOGIN_USER) LoginUser user, Model model) {
|
||||
if (!user.isAdmin())
|
||||
return "redirect:/menu";
|
||||
Optional<Employee> e = employeeService.findByMail(mailAddress);
|
||||
if (e.isEmpty()) {
|
||||
model.addAttribute("mailAddress", mailAddress);
|
||||
model.addAttribute("errorMessage", "該当する社員が見つかりません。");
|
||||
return "employeeSearch";
|
||||
}
|
||||
return "redirect:" + EDIT + "?idEmployee=" + e.get().getIdEmployee();
|
||||
}
|
||||
|
||||
@GetMapping(EDIT)
|
||||
public String editInput(@RequestParam(required = false) Integer idEmployee,
|
||||
@SessionAttribute(Const.SESSION_LOGIN_USER) LoginUser user,
|
||||
Model model, RedirectAttributes ra) {
|
||||
// 一般ユーザーは常に自分自身(リクエストの社員IDは使わない)
|
||||
Integer target = user.isAdmin() ? idEmployee : user.getIdEmployee();
|
||||
if (target == null)
|
||||
return "redirect:/employees/search";
|
||||
try {
|
||||
return editView(employeeService.toForm(employeeService.find(target)), List.of(), user, model);
|
||||
} catch (BusinessException e) {
|
||||
ra.addFlashAttribute("errorMessage", e.getMessage());
|
||||
return "redirect:/menu";
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping(EDIT + "/input")
|
||||
public String editBack(@ModelAttribute("form") EmployeeForm form,
|
||||
@SessionAttribute(Const.SESSION_LOGIN_USER) LoginUser user, Model model) {
|
||||
fixTarget(form, user);
|
||||
return editView(form, List.of(), user, model);
|
||||
}
|
||||
|
||||
@PostMapping(EDIT + "/confirm")
|
||||
public String editConfirm(@ModelAttribute("form") EmployeeForm form,
|
||||
@SessionAttribute(Const.SESSION_LOGIN_USER) LoginUser user, Model model) {
|
||||
fixTarget(form, user);
|
||||
List<String> errors = user.isAdmin()
|
||||
? employeeService.validateUpdateByAdmin(form)
|
||||
: employeeService.validateUpdateBySelf(form);
|
||||
if (!errors.isEmpty())
|
||||
return editView(form, errors, user, model);
|
||||
model.addAttribute("admin", user.isAdmin());
|
||||
return "employeeUpdateConfirm";
|
||||
}
|
||||
|
||||
@PostMapping(EDIT + "/execute")
|
||||
public String editExecute(@ModelAttribute("form") EmployeeForm form,
|
||||
@SessionAttribute(Const.SESSION_LOGIN_USER) LoginUser user,
|
||||
Model model, RedirectAttributes ra) {
|
||||
fixTarget(form, user);
|
||||
try {
|
||||
if (user.isAdmin())
|
||||
employeeService.updateByAdmin(form, user);
|
||||
else
|
||||
employeeService.updateBySelf(form, user);
|
||||
} catch (BusinessException e) {
|
||||
return editView(form, List.of(e.getMessage()), user, model);
|
||||
}
|
||||
ra.addFlashAttribute("message", "社員編集が完了しました。");
|
||||
return "redirect:" + EDIT + "/complete";
|
||||
}
|
||||
|
||||
@GetMapping(EDIT + "/complete")
|
||||
public String editComplete(Model model) {
|
||||
return complete(model);
|
||||
}
|
||||
|
||||
/** 一般ユーザーの対象は常にセッションの社員ID(URL/フォーム値の書き換えを無視) */
|
||||
private static void fixTarget(EmployeeForm form, LoginUser user) {
|
||||
if (!user.isAdmin())
|
||||
form.setIdEmployee(user.getIdEmployee());
|
||||
}
|
||||
|
||||
private String editView(EmployeeForm form, List<String> errors, LoginUser user, Model model) {
|
||||
model.addAttribute("form", form);
|
||||
model.addAttribute("errors", errors);
|
||||
model.addAttribute("admin", user.isAdmin());
|
||||
return "employeeUpdateInput";
|
||||
}
|
||||
|
||||
private String complete(Model model) {
|
||||
model.addAttribute("backUrl", "/menu");
|
||||
model.addAttribute("backLabel", "メニューに戻る");
|
||||
return "complete";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user