99 unique name

This commit is contained in:
a.vasin 2019-06-05 08:41:55 +04:00
parent bdff81d25e
commit 26c1b78960
5 changed files with 31 additions and 8 deletions

View File

@ -67,7 +67,7 @@ public class ProjectController {
if (projectDto.getDeadlines().isEmpty()) { if (projectDto.getDeadlines().isEmpty()) {
errors.rejectValue("deadlines", "errorCode", "Не может быть пустым"); errors.rejectValue("deadlines", "errorCode", "Не может быть пустым");
} }
if (errors.hasErrors()) { if (errors.hasErrors() || !projectService.save(projectDto, errors)) {
return "/projects/project"; return "/projects/project";
} }
projectService.save(projectDto); projectService.save(projectDto);

View File

@ -7,6 +7,7 @@ import org.thymeleaf.util.StringUtils;
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.grant.model.GrantDto; import ru.ulstu.grant.model.GrantDto;
import ru.ulstu.name.NameContainer;
import ru.ulstu.user.model.User; import ru.ulstu.user.model.User;
import ru.ulstu.user.model.UserDto; import ru.ulstu.user.model.UserDto;
@ -18,7 +19,7 @@ import java.util.stream.Collectors;
import static ru.ulstu.core.util.StreamApiUtils.convert; import static ru.ulstu.core.util.StreamApiUtils.convert;
public class ProjectDto { public class ProjectDto extends NameContainer {
private Integer id; private Integer id;
@NotEmpty @NotEmpty

View File

@ -1,8 +1,13 @@
package ru.ulstu.project.repository; package ru.ulstu.project.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.name.BaseRepository;
import ru.ulstu.project.model.Project; import ru.ulstu.project.model.Project;
public interface ProjectRepository extends JpaRepository<Project, Integer> { public interface ProjectRepository extends JpaRepository<Project, Integer>, BaseRepository {
@Override
@Query("SELECT title FROM Project c WHERE (c.title = :name) AND (:id IS NULL OR c.id != :id) ")
String findByNameAndNotId(@Param("name") String name, @Param("id") Integer id);
} }

View File

@ -2,12 +2,14 @@ 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.springframework.validation.Errors;
import org.thymeleaf.util.StringUtils; import org.thymeleaf.util.StringUtils;
import ru.ulstu.deadline.service.DeadlineService; import ru.ulstu.deadline.service.DeadlineService;
import ru.ulstu.file.model.FileDataDto; import ru.ulstu.file.model.FileDataDto;
import ru.ulstu.file.service.FileService; import ru.ulstu.file.service.FileService;
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.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;
@ -26,7 +28,7 @@ import static ru.ulstu.core.util.StreamApiUtils.convert;
import static ru.ulstu.project.model.Project.ProjectStatus.TECHNICAL_TASK; import static ru.ulstu.project.model.Project.ProjectStatus.TECHNICAL_TASK;
@Service @Service
public class ProjectService { public class ProjectService extends BaseService {
private final static int MAX_DISPLAY_SIZE = 40; private final static int MAX_DISPLAY_SIZE = 40;
private final ProjectRepository projectRepository; private final ProjectRepository projectRepository;
@ -48,6 +50,7 @@ public class ProjectService {
this.fileService = fileService; this.fileService = fileService;
this.eventService = eventService; this.eventService = eventService;
this.userService = userService; this.userService = userService;
this.baseRepository = projectRepository;
} }
public List<Project> findAll() { public List<Project> findAll() {
@ -118,6 +121,19 @@ public class ProjectService {
return project; return project;
} }
public Boolean save(ProjectDto projectDto, Errors errors) throws IOException {
projectDto.setName(projectDto.getTitle());
checkUniqueName(projectDto,
errors,
projectDto.getId(),
"title",
"Проект с таким именем уже существует");
if (errors.hasErrors()) {
return false;
}
return true;
}
public Project save(ProjectDto projectDto) throws IOException { public Project save(ProjectDto projectDto) throws IOException {
if (isEmpty(projectDto.getId())) { if (isEmpty(projectDto.getId())) {
return create(projectDto); return create(projectDto);

View File

@ -30,10 +30,11 @@
<input class="form-control" id="title" type="text" <input class="form-control" id="title" type="text"
placeholder="Название проекта" placeholder="Название проекта"
th:field="*{title}"/> th:field="*{title}"/>
</div>
<p th:if="${#fields.hasErrors('title')}" th:errors="*{title}" <p th:if="${#fields.hasErrors('title')}" th:errors="*{title}"
class="alert alert-danger">Incorrect title</p> class="alert alert-danger">Incorrect title</p>
<p class="help-block text-danger"></p> <p class="help-block text-danger"></p>
</div>
<div class="form-group"> <div class="form-group">
<label for="status">Статус:</label> <label for="status">Статус:</label>