This commit is contained in:
Nightblade73 2019-05-12 00:28:03 +04:00
parent b57142e28f
commit d846192046
8 changed files with 36 additions and 43 deletions

View File

@ -76,7 +76,12 @@ public class ConferenceController {
public String save(@Valid ConferenceDto conferenceDto, Errors errors) throws IOException { public String save(@Valid ConferenceDto conferenceDto, Errors errors) throws IOException {
conferenceService.filterEmptyDeadlines(conferenceDto); conferenceService.filterEmptyDeadlines(conferenceDto);
conferenceService.checkEmptyFieldsOfDeadline(conferenceDto, errors); conferenceService.checkEmptyFieldsOfDeadline(conferenceDto, errors);
conferenceService.checkUniqueName(conferenceDto, errors); conferenceDto.setName(conferenceDto.getTitle());
conferenceService.checkUniqueName(conferenceDto,
errors,
conferenceDto.getId(),
"title",
"Конференция с таким именем уже существует");
if (errors.hasErrors()) { if (errors.hasErrors()) {
return CONFERENCE_PAGE; return CONFERENCE_PAGE;
} }

View File

@ -5,13 +5,13 @@ import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
import ru.ulstu.conference.model.Conference; import ru.ulstu.conference.model.Conference;
import ru.ulstu.name.NameRepository; import ru.ulstu.name.BaseRepository;
import ru.ulstu.user.model.User; import ru.ulstu.user.model.User;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
public interface ConferenceRepository extends JpaRepository<Conference, Integer>, NameRepository { public interface ConferenceRepository extends JpaRepository<Conference, Integer>, BaseRepository {
@Query("SELECT c FROM Conference c LEFT JOIN c.users u WHERE (:user IS NULL OR u.user = :user) " + @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") "AND (YEAR(c.beginDate) = :year OR :year IS NULL) ORDER BY begin_date DESC")
List<Conference> findByUserAndYear(@Param("user") User user, @Param("year") Integer year); List<Conference> findByUserAndYear(@Param("user") User user, @Param("year") Integer year);
@ -27,6 +27,6 @@ public interface ConferenceRepository extends JpaRepository<Conference, Integer>
int updatePingConference(@Param("id") Integer id); int updatePingConference(@Param("id") Integer id);
@Override @Override
@Query("SELECT title FROM Conference c WHERE c.title = :name") @Query("SELECT title FROM Conference c WHERE (c.title = :name) AND (:id IS NULL OR c.id != :id) ")
String findByName(@Param("name") String name); String findByNameAndNotId(@Param("name") String name, @Param("id") Integer id);
} }

View File

@ -13,7 +13,7 @@ import ru.ulstu.conference.model.ConferenceUser;
import ru.ulstu.conference.repository.ConferenceRepository; import ru.ulstu.conference.repository.ConferenceRepository;
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.name.NameService; import ru.ulstu.name.BaseService;
import ru.ulstu.paper.model.Paper; import ru.ulstu.paper.model.Paper;
import ru.ulstu.paper.service.PaperService; import ru.ulstu.paper.service.PaperService;
import ru.ulstu.ping.service.PingService; import ru.ulstu.ping.service.PingService;
@ -32,7 +32,7 @@ import static org.springframework.util.ObjectUtils.isEmpty;
import static ru.ulstu.core.util.StreamApiUtils.convert; import static ru.ulstu.core.util.StreamApiUtils.convert;
@Service @Service
public class ConferenceService extends NameService { public class ConferenceService extends BaseService {
private final static int MAX_DISPLAY_SIZE = 40; private final static int MAX_DISPLAY_SIZE = 40;
private final ConferenceRepository conferenceRepository; private final ConferenceRepository conferenceRepository;
@ -52,6 +52,7 @@ public class ConferenceService extends NameService {
PingService pingService, PingService pingService,
ConferenceNotificationService conferenceNotificationService, ConferenceNotificationService conferenceNotificationService,
EventService eventService) { EventService eventService) {
this.baseRepository = conferenceRepository;
this.conferenceRepository = conferenceRepository; this.conferenceRepository = conferenceRepository;
this.conferenceUserService = conferenceUserService; this.conferenceUserService = conferenceUserService;
this.deadlineService = deadlineService; this.deadlineService = deadlineService;
@ -286,12 +287,4 @@ public class ConferenceService extends NameService {
.filter(dto -> dto.getDate() != null || !org.springframework.util.StringUtils.isEmpty(dto.getDescription())) .filter(dto -> dto.getDate() != null || !org.springframework.util.StringUtils.isEmpty(dto.getDescription()))
.collect(Collectors.toList())); .collect(Collectors.toList()));
} }
public void checkUniqueName(ConferenceDto conferenceDto, Errors errors) {
conferenceDto.setName(conferenceDto.getTitle());
conferenceDto.setNameRepository(conferenceRepository);
if (checkUniqueName(conferenceDto)) {
errors.rejectValue("title", "errorCode", "Конференция с таким именем уже существует");
}
}
} }

View File

@ -0,0 +1,7 @@
package ru.ulstu.name;
import org.springframework.data.repository.query.Param;
public interface BaseRepository {
String findByNameAndNotId(@Param("name") String name, @Param("id") Integer id);
}

View File

@ -0,0 +1,16 @@
package ru.ulstu.name;
import org.springframework.stereotype.Service;
import org.springframework.validation.Errors;
@Service
public abstract class BaseService {
public BaseRepository baseRepository;
public void checkUniqueName(NameContainer nameContainer, Errors errors, Integer id, String checkField, String errorMessage) {
if (nameContainer.getName().equals(baseRepository.findByNameAndNotId(nameContainer.getName(), id))) {
errors.rejectValue(checkField, "errorCode", errorMessage);
}
}
}

View File

@ -4,16 +4,6 @@ public abstract class NameContainer {
private String name = ""; private String name = "";
private NameRepository nameRepository;
public NameRepository getNameRepository() {
return nameRepository;
}
public void setNameRepository(NameRepository nameRepository) {
this.nameRepository = nameRepository;
}
public String getName() { public String getName() {
return name; return name;
} }

View File

@ -1,7 +0,0 @@
package ru.ulstu.name;
import org.springframework.data.repository.query.Param;
public interface NameRepository {
String findByName(@Param("name") String name);
}

View File

@ -1,11 +0,0 @@
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()));
}
}