From 349e7373ffe829ad7a84081575beba6bec0ade36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D1=81=D0=B8=D0=BD=20=D0=90=D0=BD=D1=82=D0=BE?= =?UTF-8?q?=D0=BD?= Date: Mon, 22 Apr 2019 14:19:01 +0300 Subject: [PATCH] #97 save function --- .../project/controller/ProjectController.java | 25 +++++++++++++++++++ .../ulstu/project/service/ProjectService.java | 8 +++++- .../resources/templates/projects/project.html | 15 +++++++---- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/main/java/ru/ulstu/project/controller/ProjectController.java b/src/main/java/ru/ulstu/project/controller/ProjectController.java index 3244ccb..7033e6d 100644 --- a/src/main/java/ru/ulstu/project/controller/ProjectController.java +++ b/src/main/java/ru/ulstu/project/controller/ProjectController.java @@ -2,8 +2,10 @@ package ru.ulstu.project.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.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import ru.ulstu.project.model.Project; @@ -11,7 +13,13 @@ import ru.ulstu.project.model.ProjectDto; import ru.ulstu.project.service.ProjectService; 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.hasErrors; @Controller() @RequestMapping(value = "/projects") @@ -46,4 +54,21 @@ public class ProjectController { public List getProjectStatuses() { return projectService.getProjectStatuses(); } + + @PostMapping(value = "/project", params = "save") + public String save(@Valid ProjectDto projectDto, Errors errors) throws IOException { + filterEmptyDeadlines(projectDto); + if (projectDto.getDeadlines().isEmpty()) { + errors.rejectValue("deadlines", "errorCode", "Не может быть пустым"); + } + hasErrors(errors, "/projects/project"); + projectService.save(projectDto); + return String.format("redirect:%s", "/projects/projects"); + } + + private void filterEmptyDeadlines(ProjectDto projectDto) { + projectDto.setDeadlines(projectDto.getDeadlines().stream() + .filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription())) + .collect(Collectors.toList())); + } } diff --git a/src/main/java/ru/ulstu/project/service/ProjectService.java b/src/main/java/ru/ulstu/project/service/ProjectService.java index 99d8919..49a6688 100644 --- a/src/main/java/ru/ulstu/project/service/ProjectService.java +++ b/src/main/java/ru/ulstu/project/service/ProjectService.java @@ -3,16 +3,20 @@ package ru.ulstu.project.service; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.thymeleaf.util.StringUtils; +import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.service.DeadlineService; +import ru.ulstu.grant.model.Grant; import ru.ulstu.project.model.Project; import ru.ulstu.project.model.ProjectDto; import ru.ulstu.project.repository.ProjectRepository; import java.util.Arrays; +import java.util.Date; import java.util.List; import static org.springframework.util.ObjectUtils.isEmpty; import static ru.ulstu.core.util.StreamApiUtils.convert; +import static ru.ulstu.project.model.Project.ProjectStatus.APPLICATION; @Service public class ProjectService { @@ -53,8 +57,10 @@ public class ProjectService { } private Project copyFromDto(Project project, ProjectDto projectDto) { + project.setDescription(projectDto.getDescription()); + project.setStatus(projectDto.getStatus() == null ? APPLICATION : projectDto.getStatus()); project.setTitle(projectDto.getTitle()); - project.setDeadlines(deadlineService.saveOrCreate(projectDto.getDeadlines())); + project.setRepository(projectDto.getRepository()); return project; } diff --git a/src/main/resources/templates/projects/project.html b/src/main/resources/templates/projects/project.html index 437927a..a716ff2 100644 --- a/src/main/resources/templates/projects/project.html +++ b/src/main/resources/templates/projects/project.html @@ -70,16 +70,21 @@
-
- +
+
- +
- +
-