fix check unique name of grant
This commit is contained in:
parent
2c55293e0e
commit
b20ab96b55
@ -28,7 +28,7 @@ public interface ConferenceRepository extends JpaRepository<Conference, Integer>
|
||||
|
||||
@Override
|
||||
@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) " +
|
||||
"AND (u.participation = 'INTRAMURAL') AND (c.beginDate <= CURRENT_DATE) AND (c.endDate >= CURRENT_DATE)")
|
||||
|
@ -12,13 +12,13 @@ public interface GrantRepository extends JpaRepository<Grant, Integer>, BaseRepo
|
||||
|
||||
List<Grant> findByStatus(Grant.GrantStatus status);
|
||||
|
||||
Grant findByTitle(String title);
|
||||
Grant findFirstByTitle(String title);
|
||||
|
||||
Grant findGrantById(Integer grantId);
|
||||
|
||||
@Override
|
||||
@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')")
|
||||
List<Grant> findAllActive();
|
||||
|
@ -197,7 +197,7 @@ public class GrantService extends BaseService {
|
||||
grantDto.setName(grantDto.getTitle());
|
||||
String title = checkUniqueName(grantDto, grantDto.getId()); //проверка уникальности имени
|
||||
if (title != null) {
|
||||
Grant grantFromDB = grantRepository.findByTitle(title); //грант с таким же названием из бд
|
||||
Grant grantFromDB = grantRepository.findFirstByTitle(title); //грант с таким же названием из бд
|
||||
if (checkSameDeadline(grantDto, grantFromDB.getId())) { //если дедайны тоже совпадают
|
||||
return false;
|
||||
} else { //иначе грант уже был в системе, но в другом году, поэтому надо создать
|
||||
|
@ -2,6 +2,8 @@ package ru.ulstu.name;
|
||||
|
||||
import org.springframework.data.repository.query.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface BaseRepository {
|
||||
String findFirstByNameAndNotId(@Param("name") String name, @Param("id") Integer id);
|
||||
List<String> findByNameAndNotId(@Param("name") String name, @Param("id") Integer id);
|
||||
}
|
||||
|
@ -3,21 +3,34 @@ package ru.ulstu.name;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.Errors;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public abstract class BaseService {
|
||||
|
||||
public BaseRepository baseRepository;
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
protected String checkUniqueName(NameContainer nameContainer, Integer id) {
|
||||
if (nameContainer.getName().equals(baseRepository.findFirstByNameAndNotId(nameContainer.getName(), id))) {
|
||||
return baseRepository.findFirstByNameAndNotId(nameContainer.getName(), id);
|
||||
String foundName = getUnique(baseRepository.findByNameAndNotId(nameContainer.getName(), id));
|
||||
if (nameContainer.getName().equals(foundName)) {
|
||||
return foundName;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getUnique(List<String> names) {
|
||||
return Optional.ofNullable(names)
|
||||
.orElse(new ArrayList<>())
|
||||
.stream()
|
||||
.findAny()
|
||||
.orElse(null);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user