package ru.ulstu.grant.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.validation.Errors; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import ru.ulstu.deadline.model.Deadline; import ru.ulstu.grant.model.Grant; import ru.ulstu.grant.model.GrantDto; import ru.ulstu.grant.service.GrantService; import ru.ulstu.paper.model.PaperDto; import ru.ulstu.user.model.User; import springfox.documentation.annotations.ApiIgnore; import javax.validation.Valid; import java.io.IOException; import java.util.List; import static ru.ulstu.core.controller.Navigation.GRANTS_PAGE; import static ru.ulstu.core.controller.Navigation.GRANT_PAGE; import static ru.ulstu.core.controller.Navigation.REDIRECT_TO; @Controller() @RequestMapping(value = "/grants") @ApiIgnore public class GrantController { private final GrantService grantService; public GrantController(GrantService grantService) { this.grantService = grantService; } @GetMapping("/grants") public void getGrants(ModelMap modelMap) { modelMap.put("grants", grantService.findAllDto()); } @GetMapping("/dashboard") public void getDashboard(ModelMap modelMap) { modelMap.put("grants", grantService.findAllActiveDto()); } @GetMapping("/grant") public void getGrant(ModelMap modelMap, @RequestParam(value = "id") Integer id) { if (id != null && id > 0) { GrantDto grantDto = grantService.findOneDto(id); attachPaper(grantDto); modelMap.put("grantDto", grantDto); } else { modelMap.put("grantDto", new GrantDto()); } } @PostMapping(value = "/grant", params = "save") public String save(@Valid GrantDto grantDto, Errors errors) throws IOException { if (!grantService.save(grantDto, errors)) { return GRANT_PAGE; } return String.format(REDIRECT_TO, GRANTS_PAGE); } @PostMapping(value = "/grant", params = "filterUsers") public String filterUsers() { return GRANT_PAGE; } @PostMapping(value = "/grant", params = "attachPaper") public String attachPaper(GrantDto grantDto) { grantService.attachPaper(grantDto); return GRANT_PAGE; } @PostMapping(value = "/grant", params = "addDeadline") public String addDeadline(@Valid GrantDto grantDto, Errors errors) { grantService.filterEmptyDeadlines(grantDto); if (errors.hasErrors()) { return GRANT_PAGE; } grantDto.getDeadlines().add(new Deadline()); return GRANT_PAGE; } @PostMapping(value = "/grant", params = "removeDeadline") public String removeDeadline(GrantDto grantDto, @RequestParam(value = "removeDeadline") Integer deadlineId) { grantService.removeDeadline(grantDto, deadlineId); return GRANT_PAGE; } @PostMapping(value = "/grant", params = "createProject") public String createProject(@Valid GrantDto grantDto, Errors errors) throws IOException { if (errors.hasErrors()) { return GRANT_PAGE; } grantService.createProject(grantDto); return GRANT_PAGE; } @GetMapping("/delete/{grant-id}") public String delete(@PathVariable("grant-id") Integer grantId) throws IOException { grantService.delete(grantId); return String.format(REDIRECT_TO, GRANTS_PAGE); } @ModelAttribute("allStatuses") public List getGrantStatuses() { return grantService.getGrantStatuses(); } @ModelAttribute("allAuthors") public List getAllAuthors(GrantDto grantDto) { return grantService.getGrantAuthors(grantDto); } @ModelAttribute("allPapers") public List getAllPapers() { return grantService.getAllUncompletedPapers(); } @ResponseBody @PostMapping(value = "/ping") public void ping(@RequestParam("grantId") int grantId) throws IOException { grantService.ping(grantId); } }