From b57142e28fc423b5779372e28107e536f03db963 Mon Sep 17 00:00:00 2001 From: Nightblade73 Date: Wed, 8 May 2019 17:08:37 +0400 Subject: [PATCH] #70 added unique name control --- .../controller/ConferenceController.java | 1 + .../ulstu/conference/model/ConferenceDto.java | 3 ++- .../repository/ConferenceRepository.java | 7 +++++- .../conference/service/ConferenceService.java | 12 ++++++++-- .../java/ru/ulstu/name/NameContainer.java | 24 +++++++++++++++++++ .../java/ru/ulstu/name/NameRepository.java | 7 ++++++ src/main/java/ru/ulstu/name/NameService.java | 11 +++++++++ 7 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 src/main/java/ru/ulstu/name/NameContainer.java create mode 100644 src/main/java/ru/ulstu/name/NameRepository.java create mode 100644 src/main/java/ru/ulstu/name/NameService.java diff --git a/src/main/java/ru/ulstu/conference/controller/ConferenceController.java b/src/main/java/ru/ulstu/conference/controller/ConferenceController.java index 5aecb51..4e11d32 100644 --- a/src/main/java/ru/ulstu/conference/controller/ConferenceController.java +++ b/src/main/java/ru/ulstu/conference/controller/ConferenceController.java @@ -76,6 +76,7 @@ public class ConferenceController { public String save(@Valid ConferenceDto conferenceDto, Errors errors) throws IOException { conferenceService.filterEmptyDeadlines(conferenceDto); conferenceService.checkEmptyFieldsOfDeadline(conferenceDto, errors); + conferenceService.checkUniqueName(conferenceDto, errors); if (errors.hasErrors()) { return CONFERENCE_PAGE; } diff --git a/src/main/java/ru/ulstu/conference/model/ConferenceDto.java b/src/main/java/ru/ulstu/conference/model/ConferenceDto.java index 2e2f13b..9c9b37a 100644 --- a/src/main/java/ru/ulstu/conference/model/ConferenceDto.java +++ b/src/main/java/ru/ulstu/conference/model/ConferenceDto.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import org.hibernate.validator.constraints.NotEmpty; import org.springframework.format.annotation.DateTimeFormat; import ru.ulstu.deadline.model.Deadline; +import ru.ulstu.name.NameContainer; import ru.ulstu.paper.model.Paper; import javax.persistence.Temporal; @@ -16,7 +17,7 @@ import java.util.List; import static ru.ulstu.core.util.StreamApiUtils.convert; -public class ConferenceDto { +public class ConferenceDto extends NameContainer { private final static String BEGIN_DATE = "Начало: "; private final static String END_DATE = "Конец: "; diff --git a/src/main/java/ru/ulstu/conference/repository/ConferenceRepository.java b/src/main/java/ru/ulstu/conference/repository/ConferenceRepository.java index 3a8bbe1..5dfe7dd 100644 --- a/src/main/java/ru/ulstu/conference/repository/ConferenceRepository.java +++ b/src/main/java/ru/ulstu/conference/repository/ConferenceRepository.java @@ -5,12 +5,13 @@ import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import ru.ulstu.conference.model.Conference; +import ru.ulstu.name.NameRepository; import ru.ulstu.user.model.User; import java.util.Date; import java.util.List; -public interface ConferenceRepository extends JpaRepository { +public interface ConferenceRepository extends JpaRepository, NameRepository { @Query("SELECT c FROM Conference c LEFT JOIN c.users u WHERE (:user IS NULL OR u.user = :user) " + "AND (YEAR(c.beginDate) = :year OR :year IS NULL) ORDER BY begin_date DESC") List findByUserAndYear(@Param("user") User user, @Param("year") Integer year); @@ -24,4 +25,8 @@ public interface ConferenceRepository extends JpaRepository @Modifying @Query("UPDATE Conference c SET c.ping = (c.ping + 1) WHERE c.id = :id") int updatePingConference(@Param("id") Integer id); + + @Override + @Query("SELECT title FROM Conference c WHERE c.title = :name") + String findByName(@Param("name") String name); } diff --git a/src/main/java/ru/ulstu/conference/service/ConferenceService.java b/src/main/java/ru/ulstu/conference/service/ConferenceService.java index 38de435..9a10efb 100644 --- a/src/main/java/ru/ulstu/conference/service/ConferenceService.java +++ b/src/main/java/ru/ulstu/conference/service/ConferenceService.java @@ -13,6 +13,7 @@ import ru.ulstu.conference.model.ConferenceUser; import ru.ulstu.conference.repository.ConferenceRepository; import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.service.DeadlineService; +import ru.ulstu.name.NameService; import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.service.PaperService; import ru.ulstu.ping.service.PingService; @@ -31,7 +32,7 @@ import static org.springframework.util.ObjectUtils.isEmpty; import static ru.ulstu.core.util.StreamApiUtils.convert; @Service -public class ConferenceService { +public class ConferenceService extends NameService { private final static int MAX_DISPLAY_SIZE = 40; private final ConferenceRepository conferenceRepository; @@ -280,10 +281,17 @@ public class ConferenceService { } } - public void filterEmptyDeadlines(ConferenceDto conferenceDto) { conferenceDto.setDeadlines(conferenceDto.getDeadlines().stream() .filter(dto -> dto.getDate() != null || !org.springframework.util.StringUtils.isEmpty(dto.getDescription())) .collect(Collectors.toList())); } + + public void checkUniqueName(ConferenceDto conferenceDto, Errors errors) { + conferenceDto.setName(conferenceDto.getTitle()); + conferenceDto.setNameRepository(conferenceRepository); + if (checkUniqueName(conferenceDto)) { + errors.rejectValue("title", "errorCode", "Конференция с таким именем уже существует"); + } + } } diff --git a/src/main/java/ru/ulstu/name/NameContainer.java b/src/main/java/ru/ulstu/name/NameContainer.java new file mode 100644 index 0000000..bcccc80 --- /dev/null +++ b/src/main/java/ru/ulstu/name/NameContainer.java @@ -0,0 +1,24 @@ +package ru.ulstu.name; + +public abstract class NameContainer { + + private String name = ""; + + private NameRepository nameRepository; + + public NameRepository getNameRepository() { + return nameRepository; + } + + public void setNameRepository(NameRepository nameRepository) { + this.nameRepository = nameRepository; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/src/main/java/ru/ulstu/name/NameRepository.java b/src/main/java/ru/ulstu/name/NameRepository.java new file mode 100644 index 0000000..99bd3ac --- /dev/null +++ b/src/main/java/ru/ulstu/name/NameRepository.java @@ -0,0 +1,7 @@ +package ru.ulstu.name; + +import org.springframework.data.repository.query.Param; + +public interface NameRepository { + String findByName(@Param("name") String name); +} diff --git a/src/main/java/ru/ulstu/name/NameService.java b/src/main/java/ru/ulstu/name/NameService.java new file mode 100644 index 0000000..7d7bdc9 --- /dev/null +++ b/src/main/java/ru/ulstu/name/NameService.java @@ -0,0 +1,11 @@ +package ru.ulstu.name; + +import org.springframework.stereotype.Service; + +@Service +public abstract class NameService { + + public boolean checkUniqueName(NameContainer nameContainer) { + return nameContainer.getName().equals(nameContainer.getNameRepository().findByName(nameContainer.getName())); + } +}