Załóżmy, że mam dwa typy kont, które mają dostęp do tej samej operacji w systemie. Mam dwa kontrolery kontroler dla admina i dla pracownika. Więc muszę dodać w obu kontrolerach praktycznie takie same metody, różniące się tylko tym, że w requestMapping dostają admin lub employee, tak samo w zwracanych widokach zwracają z admin lub employee. Jaki jest najlepszy sposób na ominięcie tego żeby nie powielać praktycznie tego samego kodu?
@RequestMapping(value = "/admin/createStudentAccount", method = RequestMethod.POST)
public String createStudentAccount(@ModelAttribute("user") UserDto userDto, BindingResult result) {
AddClientAccountValidator addClientAccountValidator = new AddClientAccountValidator();
addClientAccountValidator.validate(userDto, result);
if (result.hasErrors()) {
return "admin/createstudentaccount";
} else {
if ((userAccountService.createPersonalAccout(userDto, "Konto student").getError()) != null) {
return "admin/createstudentaccount";
} else {
return "admin/createaccount";
}
}
}
@RequestMapping(value = "/employee/createStudentAccount", method = RequestMethod.POST)
public String createStudentAccount(@ModelAttribute("user") UserDto userDto, BindingResult result) {
AddClientAccountValidator addClientAccountValidator = new AddClientAccountValidator();
addClientAccountValidator.validate(userDto, result);
if (result.hasErrors()) {
return "employee/createstudentaccount";
} else {
if ((userAccountService.createPersonalAccout(userDto, "Konto student").getError()) != null) {
return "employee/createstudentaccount";
} else {
return "employee/createaccount";
}
}
}