#120 add methods for checking unique title and date of grant

This commit is contained in:
T-Midnight 2019-05-22 15:10:51 +04:00
parent bb9f412480
commit bab621677e
4 changed files with 21 additions and 0 deletions

View File

@ -2,8 +2,15 @@ package ru.ulstu.deadline.repository;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.model.Deadline;
import java.util.Date;
public interface DeadlineRepository extends JpaRepository<Deadline, Integer> { public interface DeadlineRepository extends JpaRepository<Deadline, Integer> {
@Query(
value = "SELECT date FROM Deadline d WHERE (d.grant_id = ?1) AND (d.date = ?2)",
nativeQuery = true)
Date findByGrantIdAndDate(Integer grant_id, Date date);
} }

View File

@ -5,6 +5,7 @@ import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.deadline.model.Deadline; import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.deadline.repository.DeadlineRepository; import ru.ulstu.deadline.repository.DeadlineRepository;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -46,4 +47,8 @@ public class DeadlineService {
public void remove(Integer deadlineId) { public void remove(Integer deadlineId) {
deadlineRepository.delete(deadlineId); deadlineRepository.delete(deadlineId);
} }
public Date findByGrantIdAndDate(Integer id, Date date) {
return deadlineRepository.findByGrantIdAndDate(id, date);
}
} }

View File

@ -12,6 +12,8 @@ public interface GrantRepository extends JpaRepository<Grant, Integer>, BaseRepo
List<Grant> findByStatus(Grant.GrantStatus status); List<Grant> findByStatus(Grant.GrantStatus status);
Grant findByTitle(String title);
@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 findByNameAndNotId(@Param("name") String name, @Param("id") Integer id); String findByNameAndNotId(@Param("name") String name, @Param("id") Integer id);

View File

@ -13,4 +13,11 @@ public abstract class BaseService {
errors.rejectValue(checkField, "errorCode", errorMessage); errors.rejectValue(checkField, "errorCode", errorMessage);
} }
} }
public String checkUniqueName(NameContainer nameContainer, Integer id) {
if (nameContainer.getName().equals(baseRepository.findByNameAndNotId(nameContainer.getName(), id))) {
return baseRepository.findByNameAndNotId(nameContainer.getName(), id);
}
return null;
}
} }