diff --git a/src/main/java/ru/ulstu/project/controller/ProjectController.java b/src/main/java/ru/ulstu/project/controller/ProjectController.java index 87ae8df..120d419 100644 --- a/src/main/java/ru/ulstu/project/controller/ProjectController.java +++ b/src/main/java/ru/ulstu/project/controller/ProjectController.java @@ -67,7 +67,7 @@ public class ProjectController { if (projectDto.getDeadlines().isEmpty()) { errors.rejectValue("deadlines", "errorCode", "Не может быть пустым"); } - if (errors.hasErrors()) { + if (errors.hasErrors() || !projectService.save(projectDto, errors)) { return "/projects/project"; } projectService.save(projectDto); diff --git a/src/main/java/ru/ulstu/project/model/ProjectDto.java b/src/main/java/ru/ulstu/project/model/ProjectDto.java index fb60703..d87c748 100644 --- a/src/main/java/ru/ulstu/project/model/ProjectDto.java +++ b/src/main/java/ru/ulstu/project/model/ProjectDto.java @@ -7,6 +7,7 @@ import org.thymeleaf.util.StringUtils; import ru.ulstu.deadline.model.Deadline; import ru.ulstu.file.model.FileDataDto; import ru.ulstu.grant.model.GrantDto; +import ru.ulstu.name.NameContainer; import ru.ulstu.user.model.User; import ru.ulstu.user.model.UserDto; @@ -18,7 +19,7 @@ import java.util.stream.Collectors; import static ru.ulstu.core.util.StreamApiUtils.convert; -public class ProjectDto { +public class ProjectDto extends NameContainer { private Integer id; @NotEmpty diff --git a/src/main/java/ru/ulstu/project/repository/ProjectRepository.java b/src/main/java/ru/ulstu/project/repository/ProjectRepository.java index 6a78075..799327e 100644 --- a/src/main/java/ru/ulstu/project/repository/ProjectRepository.java +++ b/src/main/java/ru/ulstu/project/repository/ProjectRepository.java @@ -1,8 +1,13 @@ package ru.ulstu.project.repository; 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; -public interface ProjectRepository extends JpaRepository { - +public interface ProjectRepository extends JpaRepository, 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); } diff --git a/src/main/java/ru/ulstu/project/service/ProjectService.java b/src/main/java/ru/ulstu/project/service/ProjectService.java index 6491b04..d9ee707 100644 --- a/src/main/java/ru/ulstu/project/service/ProjectService.java +++ b/src/main/java/ru/ulstu/project/service/ProjectService.java @@ -2,12 +2,14 @@ package ru.ulstu.project.service; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.springframework.validation.Errors; import org.thymeleaf.util.StringUtils; import ru.ulstu.deadline.service.DeadlineService; import ru.ulstu.file.model.FileDataDto; import ru.ulstu.file.service.FileService; import ru.ulstu.grant.model.GrantDto; import ru.ulstu.grant.repository.GrantRepository; +import ru.ulstu.name.BaseService; import ru.ulstu.project.model.Project; import ru.ulstu.project.model.ProjectDto; 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; @Service -public class ProjectService { +public class ProjectService extends BaseService { private final static int MAX_DISPLAY_SIZE = 40; private final ProjectRepository projectRepository; @@ -48,6 +50,7 @@ public class ProjectService { this.fileService = fileService; this.eventService = eventService; this.userService = userService; + this.baseRepository = projectRepository; } public List findAll() { @@ -118,6 +121,19 @@ public class ProjectService { 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 { if (isEmpty(projectDto.getId())) { return create(projectDto); diff --git a/src/main/resources/templates/projects/fragments/projectDashboardFragment.html b/src/main/resources/templates/projects/fragments/projectDashboardFragment.html index 027dce7..a2c3cef 100644 --- a/src/main/resources/templates/projects/fragments/projectDashboardFragment.html +++ b/src/main/resources/templates/projects/fragments/projectDashboardFragment.html @@ -10,7 +10,10 @@
- title + + title + +

status

diff --git a/src/main/resources/templates/projects/project.html b/src/main/resources/templates/projects/project.html index 0d132e9..9113665 100644 --- a/src/main/resources/templates/projects/project.html +++ b/src/main/resources/templates/projects/project.html @@ -30,11 +30,12 @@ -

Incorrect title

-

+

Incorrect title

+

+