#120 check unique grant name

This commit is contained in:
T-Midnight 2019-05-20 20:49:03 +04:00
parent c0e66e81a2
commit bb9f412480
4 changed files with 47 additions and 22 deletions

View File

@ -20,9 +20,7 @@ import springfox.documentation.annotations.ApiIgnore;
import javax.validation.Valid; import javax.validation.Valid;
import java.io.IOException; 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.GRANTS_PAGE; 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.GRANT_PAGE;
import static ru.ulstu.core.controller.Navigation.REDIRECT_TO; import static ru.ulstu.core.controller.Navigation.REDIRECT_TO;
@ -62,17 +60,9 @@ public class GrantController {
@PostMapping(value = "/grant", params = "save") @PostMapping(value = "/grant", params = "save")
public String save(@Valid GrantDto grantDto, Errors errors) public String save(@Valid GrantDto grantDto, Errors errors)
throws IOException { throws IOException {
filterEmptyDeadlines(grantDto); if (!grantService.save(grantDto, errors)) {
if (grantDto.getDeadlines().isEmpty()) {
errors.rejectValue("deadlines", "errorCode", "Не может быть пусто");
}
if (grantDto.getLeaderId().equals(-1)) {
errors.rejectValue("leaderId", "errorCode", "Укажите руководителя");
}
if (errors.hasErrors()) {
return GRANT_PAGE; return GRANT_PAGE;
} }
grantService.save(grantDto);
return String.format(REDIRECT_TO, GRANTS_PAGE); return String.format(REDIRECT_TO, GRANTS_PAGE);
} }
@ -89,7 +79,7 @@ public class GrantController {
@PostMapping(value = "/grant", params = "addDeadline") @PostMapping(value = "/grant", params = "addDeadline")
public String addDeadline(@Valid GrantDto grantDto, Errors errors) { public String addDeadline(@Valid GrantDto grantDto, Errors errors) {
filterEmptyDeadlines(grantDto); grantService.filterEmptyDeadlines(grantDto);
if (errors.hasErrors()) { if (errors.hasErrors()) {
return GRANT_PAGE; return GRANT_PAGE;
} }
@ -133,10 +123,4 @@ public class GrantController {
public List<PaperDto> getAllPapers() { public List<PaperDto> getAllPapers() {
return grantService.getAllUncompletedPapers(); return grantService.getAllUncompletedPapers();
} }
private void filterEmptyDeadlines(GrantDto grantDto) {
grantDto.setDeadlines(grantDto.getDeadlines().stream()
.filter(dto -> dto.getDate() != null || !isEmpty(dto.getDescription()))
.collect(Collectors.toList()));
}
} }

View File

@ -6,6 +6,7 @@ import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.NotEmpty; import org.hibernate.validator.constraints.NotEmpty;
import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.file.model.FileDataDto; import ru.ulstu.file.model.FileDataDto;
import ru.ulstu.name.NameContainer;
import ru.ulstu.paper.model.PaperDto; import ru.ulstu.paper.model.PaperDto;
import ru.ulstu.project.model.ProjectDto; import ru.ulstu.project.model.ProjectDto;
import ru.ulstu.user.model.UserDto; import ru.ulstu.user.model.UserDto;
@ -17,7 +18,7 @@ import java.util.stream.Collectors;
import static ru.ulstu.core.util.StreamApiUtils.convert; import static ru.ulstu.core.util.StreamApiUtils.convert;
public class GrantDto { public class GrantDto extends NameContainer {
private final static int MAX_AUTHORS_LENGTH = 60; private final static int MAX_AUTHORS_LENGTH = 60;
private Integer id; private Integer id;

View File

@ -1,11 +1,18 @@
package ru.ulstu.grant.repository; package ru.ulstu.grant.repository;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import ru.ulstu.grant.model.Grant; import ru.ulstu.grant.model.Grant;
import ru.ulstu.name.BaseRepository;
import java.util.List; import java.util.List;
public interface GrantRepository extends JpaRepository<Grant, Integer> { public interface GrantRepository extends JpaRepository<Grant, Integer>, BaseRepository {
List<Grant> findByStatus(Grant.GrantStatus status); List<Grant> findByStatus(Grant.GrantStatus status);
@Override
@Query("SELECT title FROM Grant g WHERE (g.title = :name) AND (:id IS NULL OR g.id != :id) ")
String findByNameAndNotId(@Param("name") String name, @Param("id") Integer id);
} }

View File

@ -3,6 +3,7 @@ package ru.ulstu.grant.service;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.Errors;
import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.deadline.service.DeadlineService; import ru.ulstu.deadline.service.DeadlineService;
import ru.ulstu.file.model.FileDataDto; import ru.ulstu.file.model.FileDataDto;
@ -10,6 +11,7 @@ import ru.ulstu.file.service.FileService;
import ru.ulstu.grant.model.Grant; import ru.ulstu.grant.model.Grant;
import ru.ulstu.grant.model.GrantDto; import ru.ulstu.grant.model.GrantDto;
import ru.ulstu.grant.repository.GrantRepository; import ru.ulstu.grant.repository.GrantRepository;
import ru.ulstu.name.BaseService;
import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.model.Paper;
import ru.ulstu.paper.model.PaperDto; import ru.ulstu.paper.model.PaperDto;
import ru.ulstu.paper.service.PaperService; import ru.ulstu.paper.service.PaperService;
@ -27,6 +29,7 @@ import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
import static org.springframework.util.ObjectUtils.isEmpty; import static org.springframework.util.ObjectUtils.isEmpty;
@ -34,7 +37,7 @@ import static ru.ulstu.core.util.StreamApiUtils.convert;
import static ru.ulstu.grant.model.Grant.GrantStatus.APPLICATION; import static ru.ulstu.grant.model.Grant.GrantStatus.APPLICATION;
@Service @Service
public class GrantService { public class GrantService extends BaseService {
private final static int MAX_DISPLAY_SIZE = 50; private final static int MAX_DISPLAY_SIZE = 50;
private final GrantRepository grantRepository; private final GrantRepository grantRepository;
@ -55,6 +58,7 @@ public class GrantService {
EventService eventService, EventService eventService,
GrantNotificationService grantNotificationService) { GrantNotificationService grantNotificationService) {
this.grantRepository = grantRepository; this.grantRepository = grantRepository;
this.baseRepository = grantRepository;
this.fileService = fileService; this.fileService = fileService;
this.deadlineService = deadlineService; this.deadlineService = deadlineService;
this.projectService = projectService; this.projectService = projectService;
@ -176,12 +180,35 @@ public class GrantService {
return grant; return grant;
} }
public void save(GrantDto grantDto) throws IOException { public boolean save(GrantDto grantDto, Errors errors) throws IOException {
grantDto.setName(grantDto.getTitle());
filterEmptyDeadlines(grantDto);
checkEmptyDeadlines(grantDto, errors);
checkEmptyLeader(grantDto, errors);
checkUniqueName(grantDto, errors, grantDto.getId(), "title", "Грант с таким именем уже существует");
if (errors.hasErrors()) {
return false;
}
if (isEmpty(grantDto.getId())) { if (isEmpty(grantDto.getId())) {
create(grantDto); create(grantDto);
} else { } else {
update(grantDto); update(grantDto);
} }
return true;
}
private void checkEmptyLeader(GrantDto grantDto, Errors errors) {
if (grantDto.getLeaderId().equals(-1)) {
errors.rejectValue("leaderId", "errorCode", "Укажите руководителя");
}
}
private void checkEmptyDeadlines(GrantDto grantDto, Errors errors) {
if (grantDto.getDeadlines().isEmpty()) {
errors.rejectValue("deadlines", "errorCode", "Не может быть пусто");
}
} }
public List<User> getGrantAuthors(GrantDto grantDto) { public List<User> getGrantAuthors(GrantDto grantDto) {
@ -261,4 +288,10 @@ public class GrantService {
.filter(author -> Collections.frequency(authors, author) > 3) .filter(author -> Collections.frequency(authors, author) > 3)
.collect(toList()); .collect(toList());
} }
public void filterEmptyDeadlines(GrantDto grantDto) {
grantDto.setDeadlines(grantDto.getDeadlines().stream()
.filter(dto -> dto.getDate() != null || !org.springframework.util.StringUtils.isEmpty(dto.getDescription()))
.collect(Collectors.toList()));
}
} }