ng-tracker/src/main/java/ru/ulstu/grant/controller/GrantController.java

143 lines
4.9 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 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 java.util.stream.Collectors;
import static org.springframework.util.StringUtils.isEmpty;
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.findAllDto());
}
@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 {
filterEmptyDeadlines(grantDto);
if (grantDto.getDeadlines().isEmpty()) {
errors.rejectValue("deadlines", "errorCode", "Не может быть пусто");
}
if (grantDto.getLeaderId().equals(-1)) {
errors.rejectValue("leaderId", "errorCode", "Укажите руководителя");
}
if (errors.hasErrors()) {
return GRANT_PAGE;
}
grantService.save(grantDto);
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) {
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<Grant.GrantStatus> getGrantStatuses() {
return grantService.getGrantStatuses();
}
@ModelAttribute("allAuthors")
public List<User> getAllAuthors(GrantDto grantDto) {
return grantService.getGrantAuthors(grantDto);
}
@ModelAttribute("allPapers")
public List<PaperDto> getAllPapers() {
return grantService.getAllUncompletedPapers();
}
private void filterEmptyDeadlines(GrantDto grantDto) {
grantDto.setDeadlines(grantDto.getDeadlines().stream()
.filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription()))
.collect(Collectors.toList()));
}
}