Commit 914fc6ad authored by shj's avatar shj

[ADD] 인덱스 추가,수정,삭제 시 형제 인덱스들 order 자동 업데이트 구현

parent 73b6c854
......@@ -11,6 +11,7 @@ import java.util.List;
public interface GuideIndexRepository extends CrudRepository<GuideIndex, String> {
List<GuideIndex> findAll();
List<GuideIndex> findAllByOrderByOrder();
List<GuideIndex> findAllByDepthOrderByOrder(Integer depth);
List<GuideIndex> findAllByPathContainingAndDepth(String path, Integer depth);
List<GuideIndex> findAllByPathContainingAndDepthOrderByOrderDesc(String path, Integer depth);
GuideIndex findByPath(String path);
......
......@@ -16,7 +16,6 @@ import java.util.NoSuchElementException;
@Log4j2
public class GuideIndexService {
GuideIndexRepository repository;
GuideTestRepository testRepository;
@Transactional
public GuideIndex getById(String id) throws Exception{
......@@ -100,7 +99,7 @@ public class GuideIndexService {
@Transactional
public GuideIndex create(GuideIndex index) throws Exception{
// todo : 기존 인덱스와 위치가 중복될 경우, 기존 인덱스들 order + 1
autoUpdateOrder(index, 1);
index.setLike(0);
index.setView(0);
......@@ -112,8 +111,8 @@ public class GuideIndexService {
public GuideIndex update(GuideIndex index) throws Exception{
GuideIndex db = repository.findById(index.getId()).orElseThrow(NoSuchElementException::new);
// todo : 기존 인덱스와 위치가 중복될 경우, 기존 인덱스들 order + 1
autoUpdateOrder(db, -1);
autoUpdateOrder(index, 1);
db.setLocale(index.getLocale());
db.setTitle(index.getTitle());
......@@ -127,7 +126,63 @@ public class GuideIndexService {
@Transactional
public String delete(String id) throws Exception{
GuideIndex db = repository.findById(id).orElseThrow(NoSuchElementException::new);
autoUpdateOrder(db, -1);
repository.delete(db);
return db.getId();
}
// targetIndex의 형제들 order 업데이트
@Transactional
public void autoUpdateOrder(GuideIndex targetIndex, Integer type) throws Exception {
String path = targetIndex.getPath();
String parentPath = path.substring(0, path.lastIndexOf("/", path.length() -2) + 1);
// 인덱스 증가해야 할 경우 (type == 1)
if(type == 1){
if(targetIndex.getDepth() == 1){
List<GuideIndex> siblings = repository.findAllByDepthOrderByOrder(1);
siblings.forEach(el -> {
if(el.getOrder() >= targetIndex.getOrder()){
el.setOrder(el.getOrder() + 1);
repository.save(el);
}
});
} else {
List<GuideIndex> siblings = repository.findAllByPathContainingAndDepth(parentPath, targetIndex.getDepth());
siblings.forEach(el -> {
if(el.getOrder() >= targetIndex.getOrder()){
el.setOrder(el.getOrder() + 1);
repository.save(el);
}
});
}
return ;
}
// 인덱스 감소해야 할 경우 (type == -1)
if(type == -1){
if(targetIndex.getDepth() == 1){
List<GuideIndex> siblings = repository.findAllByDepthOrderByOrder(1);
siblings.forEach(el -> {
if(el.getOrder() > targetIndex.getOrder()){
el.setOrder(el.getOrder() - 1);
repository.save(el);
}
});
} else {
List<GuideIndex> siblings = repository.findAllByPathContainingAndDepth(parentPath, targetIndex.getDepth());
siblings.forEach(el -> {
if(el.getOrder() > targetIndex.getOrder()){
el.setOrder(el.getOrder() - 1);
repository.save(el);
}
});
}
return ;
}
log.info("type 파라미터가 부적절합니다.");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment