#97 save function

This commit is contained in:
Васин Антон 2019-04-22 14:19:01 +03:00
parent f1f7c1f0f6
commit 349e7373ff
3 changed files with 42 additions and 6 deletions

View File

@ -2,8 +2,10 @@ package ru.ulstu.project.controller;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap; import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute; 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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import ru.ulstu.project.model.Project; import ru.ulstu.project.model.Project;
@ -11,7 +13,13 @@ import ru.ulstu.project.model.ProjectDto;
import ru.ulstu.project.service.ProjectService; import ru.ulstu.project.service.ProjectService;
import springfox.documentation.annotations.ApiIgnore; import springfox.documentation.annotations.ApiIgnore;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List; 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() @Controller()
@RequestMapping(value = "/projects") @RequestMapping(value = "/projects")
@ -46,4 +54,21 @@ public class ProjectController {
public List<Project.ProjectStatus> getProjectStatuses() { public List<Project.ProjectStatus> getProjectStatuses() {
return projectService.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()));
}
} }

View File

@ -3,16 +3,20 @@ package ru.ulstu.project.service;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils; import org.thymeleaf.util.StringUtils;
import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.deadline.service.DeadlineService; import ru.ulstu.deadline.service.DeadlineService;
import ru.ulstu.grant.model.Grant;
import ru.ulstu.project.model.Project; import ru.ulstu.project.model.Project;
import ru.ulstu.project.model.ProjectDto; import ru.ulstu.project.model.ProjectDto;
import ru.ulstu.project.repository.ProjectRepository; import ru.ulstu.project.repository.ProjectRepository;
import java.util.Arrays; import java.util.Arrays;
import java.util.Date;
import java.util.List; import java.util.List;
import static org.springframework.util.ObjectUtils.isEmpty; import static org.springframework.util.ObjectUtils.isEmpty;
import static ru.ulstu.core.util.StreamApiUtils.convert; import static ru.ulstu.core.util.StreamApiUtils.convert;
import static ru.ulstu.project.model.Project.ProjectStatus.APPLICATION;
@Service @Service
public class ProjectService { public class ProjectService {
@ -53,8 +57,10 @@ public class ProjectService {
} }
private Project copyFromDto(Project project, ProjectDto projectDto) { private Project copyFromDto(Project project, ProjectDto projectDto) {
project.setDescription(projectDto.getDescription());
project.setStatus(projectDto.getStatus() == null ? APPLICATION : projectDto.getStatus());
project.setTitle(projectDto.getTitle()); project.setTitle(projectDto.getTitle());
project.setDeadlines(deadlineService.saveOrCreate(projectDto.getDeadlines())); project.setRepository(projectDto.getRepository());
return project; return project;
} }

View File

@ -70,16 +70,21 @@
<div class="form-group"> <div class="form-group">
<label>Дедлайны показателей:</label> <label>Дедлайны показателей:</label>
<div class="row"> <div class="row" th:each="deadline, rowStat : *{deadlines}">
<input type="hidden"/> <input type="hidden" th:field="*{deadlines[__${rowStat.index}__].id}"/>
<div class="col-6"> <div class="col-6">
<input type="date" class="form-control" name="deadline"/> <input type="date" class="form-control" name="deadline"
th:field="*{deadlines[__${rowStat.index}__].date}"/>
</div> </div>
<div class="col-4"> <div class="col-4">
<input class="form-control" type="text" placeholder="Описание"/> <input class="form-control" type="text" placeholder="Описание"
th:field="*{deadlines[__${rowStat.index}__].description}"/>
</div> </div>
<div class="col-2"> <div class="col-2">
<a class="btn btn-danger float-right"><span <a class="btn btn-danger float-right"
th:onclick="|$('#deadlines${rowStat.index}\\.description').val('');
$('#deadlines${rowStat.index}\\.date').val('');
$('#addDeadline').click();|"><span
aria-hidden="true"><i class="fa fa-times"/></span> aria-hidden="true"><i class="fa fa-times"/></span>
</a> </a>
</div> </div>