fix check unique name of grant

This commit is contained in:
Anton Romanov 2020-03-28 11:20:29 +04:00
parent 2c55293e0e
commit b20ab96b55
5 changed files with 23 additions and 8 deletions

View File

@ -28,7 +28,7 @@ public interface ConferenceRepository extends JpaRepository<Conference, Integer>
@Override @Override
@Query("SELECT title FROM Conference c WHERE (c.title = :name) AND (:id IS NULL OR c.id != :id) ") @Query("SELECT title FROM Conference c WHERE (c.title = :name) AND (:id IS NULL OR c.id != :id) ")
String findFirstByNameAndNotId(@Param("name") String name, @Param("id") Integer id); List<String> findByNameAndNotId(@Param("name") String name, @Param("id") Integer id);
@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 (u.participation = 'INTRAMURAL') AND (c.beginDate <= CURRENT_DATE) AND (c.endDate >= CURRENT_DATE)") "AND (u.participation = 'INTRAMURAL') AND (c.beginDate <= CURRENT_DATE) AND (c.endDate >= CURRENT_DATE)")

View File

@ -12,13 +12,13 @@ public interface GrantRepository extends JpaRepository<Grant, Integer>, BaseRepo
List<Grant> findByStatus(Grant.GrantStatus status); List<Grant> findByStatus(Grant.GrantStatus status);
Grant findByTitle(String title); Grant findFirstByTitle(String title);
Grant findGrantById(Integer grantId); Grant findGrantById(Integer grantId);
@Override @Override
@Query("SELECT title FROM Grant g WHERE (g.title = :name) AND (:id IS NULL OR g.id != :id) ") @Query("SELECT title FROM Grant g WHERE (g.title = :name) AND (:id IS NULL OR g.id != :id) ")
String findFirstByNameAndNotId(@Param("name") String name, @Param("id") Integer id); List<String> findByNameAndNotId(@Param("name") String name, @Param("id") Integer id);
@Query("SELECT g FROM Grant g WHERE (g.status <> 'SKIPPED') AND (g.status <> 'COMPLETED')") @Query("SELECT g FROM Grant g WHERE (g.status <> 'SKIPPED') AND (g.status <> 'COMPLETED')")
List<Grant> findAllActive(); List<Grant> findAllActive();

View File

@ -197,7 +197,7 @@ public class GrantService extends BaseService {
grantDto.setName(grantDto.getTitle()); grantDto.setName(grantDto.getTitle());
String title = checkUniqueName(grantDto, grantDto.getId()); //проверка уникальности имени String title = checkUniqueName(grantDto, grantDto.getId()); //проверка уникальности имени
if (title != null) { if (title != null) {
Grant grantFromDB = grantRepository.findByTitle(title); //грант с таким же названием из бд Grant grantFromDB = grantRepository.findFirstByTitle(title); //грант с таким же названием из бд
if (checkSameDeadline(grantDto, grantFromDB.getId())) { //если дедайны тоже совпадают if (checkSameDeadline(grantDto, grantFromDB.getId())) { //если дедайны тоже совпадают
return false; return false;
} else { //иначе грант уже был в системе, но в другом году, поэтому надо создать } else { //иначе грант уже был в системе, но в другом году, поэтому надо создать

View File

@ -2,6 +2,8 @@ package ru.ulstu.name;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
import java.util.List;
public interface BaseRepository { public interface BaseRepository {
String findFirstByNameAndNotId(@Param("name") String name, @Param("id") Integer id); List<String> findByNameAndNotId(@Param("name") String name, @Param("id") Integer id);
} }

View File

@ -3,21 +3,34 @@ package ru.ulstu.name;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.validation.Errors; import org.springframework.validation.Errors;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Service @Service
public abstract class BaseService { public abstract class BaseService {
public BaseRepository baseRepository; public BaseRepository baseRepository;
protected void checkUniqueName(NameContainer nameContainer, Errors errors, Integer id, String errorMessage) { protected void checkUniqueName(NameContainer nameContainer, Errors errors, Integer id, String errorMessage) {
if (nameContainer.getName().equals(baseRepository.findFirstByNameAndNotId(nameContainer.getName(), id))) { if (nameContainer.getName().equals(getUnique(baseRepository.findByNameAndNotId(nameContainer.getName(), id)))) {
errors.rejectValue("title", "errorCode", errorMessage); errors.rejectValue("title", "errorCode", errorMessage);
} }
} }
protected String checkUniqueName(NameContainer nameContainer, Integer id) { protected String checkUniqueName(NameContainer nameContainer, Integer id) {
if (nameContainer.getName().equals(baseRepository.findFirstByNameAndNotId(nameContainer.getName(), id))) { String foundName = getUnique(baseRepository.findByNameAndNotId(nameContainer.getName(), id));
return baseRepository.findFirstByNameAndNotId(nameContainer.getName(), id); if (nameContainer.getName().equals(foundName)) {
return foundName;
} }
return null; return null;
} }
private String getUnique(List<String> names) {
return Optional.ofNullable(names)
.orElse(new ArrayList<>())
.stream()
.findAny()
.orElse(null);
}
} }