Create Navigation class to avoid "magic strings" and code duplication
This commit is contained in:
parent
ae58a5b6e4
commit
de96be2471
@ -20,6 +20,10 @@ import java.util.List;
|
|||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import static org.springframework.util.StringUtils.isEmpty;
|
import static org.springframework.util.StringUtils.isEmpty;
|
||||||
|
import static ru.ulstu.grant.controller.Navigation.GRANTS_PAGE;
|
||||||
|
import static ru.ulstu.grant.controller.Navigation.GRANT_PAGE;
|
||||||
|
import static ru.ulstu.grant.controller.Navigation.REDIRECT_TO;
|
||||||
|
import static ru.ulstu.grant.controller.Navigation.hasErrors;
|
||||||
|
|
||||||
|
|
||||||
@Controller()
|
@Controller()
|
||||||
@ -56,36 +60,30 @@ public class GrantController {
|
|||||||
if (grantDto.getDeadlines().isEmpty()) {
|
if (grantDto.getDeadlines().isEmpty()) {
|
||||||
errors.rejectValue("deadlines", "errorCode", "Не может быть пустым");
|
errors.rejectValue("deadlines", "errorCode", "Не может быть пустым");
|
||||||
}
|
}
|
||||||
if (errors.hasErrors()) {
|
hasErrors(errors, GRANT_PAGE);
|
||||||
return "/grants/grant";
|
|
||||||
}
|
|
||||||
grantService.save(grantDto);
|
grantService.save(grantDto);
|
||||||
return "redirect:/grants/grants";
|
return String.format(REDIRECT_TO, GRANTS_PAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(value = "/grant", params = "addDeadline")
|
@PostMapping(value = "/grant", params = "addDeadline")
|
||||||
public String addDeadline(@Valid GrantDto grantDto, Errors errors) {
|
public String addDeadline(@Valid GrantDto grantDto, Errors errors) {
|
||||||
filterEmptyDeadlines(grantDto);
|
filterEmptyDeadlines(grantDto);
|
||||||
if (errors.hasErrors()) {
|
hasErrors(errors, GRANT_PAGE);
|
||||||
return "/grants/grant";
|
|
||||||
}
|
|
||||||
grantDto.getDeadlines().add(new DeadlineDto());
|
grantDto.getDeadlines().add(new DeadlineDto());
|
||||||
return "/grants/grant";
|
return GRANT_PAGE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(value = "/grant", params = "createProject")
|
@PostMapping(value = "/grant", params = "createProject")
|
||||||
public String createProject(@Valid GrantDto grantDto, Errors errors) {
|
public String createProject(@Valid GrantDto grantDto, Errors errors) {
|
||||||
if (errors.hasErrors()) {
|
hasErrors(errors, GRANT_PAGE);
|
||||||
return "/grants/grant";
|
|
||||||
}
|
|
||||||
grantService.createProject(grantDto);
|
grantService.createProject(grantDto);
|
||||||
return "/grants/grant";
|
return GRANT_PAGE;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/delete/{grant-id}")
|
@GetMapping("/delete/{grant-id}")
|
||||||
public String delete(@PathVariable("grant-id") Integer grantId) throws IOException {
|
public String delete(@PathVariable("grant-id") Integer grantId) throws IOException {
|
||||||
grantService.delete(grantId);
|
grantService.delete(grantId);
|
||||||
return "redirect:/grants/grants";
|
return String.format(REDIRECT_TO, GRANTS_PAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ModelAttribute("allStatuses")
|
@ModelAttribute("allStatuses")
|
||||||
|
16
src/main/java/ru/ulstu/grant/controller/Navigation.java
Normal file
16
src/main/java/ru/ulstu/grant/controller/Navigation.java
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package ru.ulstu.grant.controller;
|
||||||
|
|
||||||
|
import org.springframework.validation.Errors;
|
||||||
|
|
||||||
|
public class Navigation {
|
||||||
|
public static final String REDIRECT_TO = "redirect:%s";
|
||||||
|
public static final String GRANTS_PAGE = "/grants/grants";
|
||||||
|
public static final String GRANT_PAGE = "/grants/grant";
|
||||||
|
|
||||||
|
public static String hasErrors(Errors errors, String page) {
|
||||||
|
if (errors.hasErrors()) {
|
||||||
|
return page;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user