From de96be2471eeeef34b59b008344182853c466f79 Mon Sep 17 00:00:00 2001 From: T-Midnight Date: Fri, 15 Mar 2019 12:19:01 +0400 Subject: [PATCH] Create Navigation class to avoid "magic strings" and code duplication --- .../grant/controller/GrantController.java | 24 +++++++++---------- .../ru/ulstu/grant/controller/Navigation.java | 16 +++++++++++++ 2 files changed, 27 insertions(+), 13 deletions(-) create mode 100644 src/main/java/ru/ulstu/grant/controller/Navigation.java diff --git a/src/main/java/ru/ulstu/grant/controller/GrantController.java b/src/main/java/ru/ulstu/grant/controller/GrantController.java index 415dd0b..8310f84 100644 --- a/src/main/java/ru/ulstu/grant/controller/GrantController.java +++ b/src/main/java/ru/ulstu/grant/controller/GrantController.java @@ -20,6 +20,10 @@ import java.util.List; import java.util.stream.Collectors; 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() @@ -56,36 +60,30 @@ public class GrantController { if (grantDto.getDeadlines().isEmpty()) { errors.rejectValue("deadlines", "errorCode", "Не может быть пустым"); } - if (errors.hasErrors()) { - return "/grants/grant"; - } + hasErrors(errors, GRANT_PAGE); grantService.save(grantDto); - return "redirect:/grants/grants"; + return String.format(REDIRECT_TO, GRANTS_PAGE); } @PostMapping(value = "/grant", params = "addDeadline") public String addDeadline(@Valid GrantDto grantDto, Errors errors) { filterEmptyDeadlines(grantDto); - if (errors.hasErrors()) { - return "/grants/grant"; - } + hasErrors(errors, GRANT_PAGE); grantDto.getDeadlines().add(new DeadlineDto()); - return "/grants/grant"; + return GRANT_PAGE; } @PostMapping(value = "/grant", params = "createProject") public String createProject(@Valid GrantDto grantDto, Errors errors) { - if (errors.hasErrors()) { - return "/grants/grant"; - } + hasErrors(errors, GRANT_PAGE); grantService.createProject(grantDto); - return "/grants/grant"; + return GRANT_PAGE; } @GetMapping("/delete/{grant-id}") public String delete(@PathVariable("grant-id") Integer grantId) throws IOException { grantService.delete(grantId); - return "redirect:/grants/grants"; + return String.format(REDIRECT_TO, GRANTS_PAGE); } @ModelAttribute("allStatuses") diff --git a/src/main/java/ru/ulstu/grant/controller/Navigation.java b/src/main/java/ru/ulstu/grant/controller/Navigation.java new file mode 100644 index 0000000..bf1626f --- /dev/null +++ b/src/main/java/ru/ulstu/grant/controller/Navigation.java @@ -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; + } +}