#70 added unique name control

This commit is contained in:
Nightblade73 2019-05-08 17:08:37 +04:00
parent 2dff183662
commit b57142e28f
7 changed files with 61 additions and 4 deletions

View File

@ -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;
}

View File

@ -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 = "Конец: ";

View File

@ -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<Conference, Integer> {
public interface ConferenceRepository extends JpaRepository<Conference, Integer>, 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<Conference> findByUserAndYear(@Param("user") User user, @Param("year") Integer year);
@ -24,4 +25,8 @@ public interface ConferenceRepository extends JpaRepository<Conference, Integer>
@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);
}

View File

@ -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", "Конференция с таким именем уже существует");
}
}
}

View File

@ -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;
}
}

View File

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

View File

@ -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()));
}
}