Create Navigation class to avoid "magic strings" and code duplication

This commit is contained in:
T-Midnight 2019-03-15 12:19:01 +04:00
parent ae58a5b6e4
commit de96be2471
2 changed files with 27 additions and 13 deletions

View File

@ -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")

View 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;
}
}