Commit 84e1ca6e authored by shj's avatar shj

[ADD] 다음 가이드, 이전 가이드 할당 함수 개편

- 개편된 Guide Model 적용
- depth와 order를 활용하여 find
- 미사용 코드 삭제
parent 1ae93788
...@@ -6,6 +6,7 @@ import lombok.Data; ...@@ -6,6 +6,7 @@ import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import nonapi.io.github.classgraph.json.Id; import nonapi.io.github.classgraph.json.Id;
import org.springframework.data.annotation.LastModifiedDate; import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Document;
import java.io.Serializable; import java.io.Serializable;
...@@ -24,13 +25,14 @@ public class GuideTest implements Serializable { ...@@ -24,13 +25,14 @@ public class GuideTest implements Serializable {
private String title; private String title;
private Integer order; private Integer order;
private String path; private String path;
private Integer depth;
private Integer view; private Integer view;
private Integer like; private Integer like;
// @Transient @Transient
// private Guide prevContent; private GuideTest prevGuide;
// @Transient @Transient
// private Guide nextContent; private GuideTest nextGuide;
private LocalDateTime createDate; private LocalDateTime createDate;
@LastModifiedDate @LastModifiedDate
......
...@@ -10,10 +10,10 @@ import java.util.List; ...@@ -10,10 +10,10 @@ import java.util.List;
@Repository @Repository
public interface GuideTestRepository extends CrudRepository<GuideTest, String> { public interface GuideTestRepository extends CrudRepository<GuideTest, String> {
List<GuideTest> findAll();
List<GuideTest> findAllByOrderByOrder(); List<GuideTest> findAllByOrderByOrder();
// List<GuideTest> findAllByParentIdOrderByOrderAsc(String parentId); List<GuideTest> findAllByPathContainingAndDepth(String path, Integer depth);
// List<GuideTest> findAllByKeywordSetContaining(String keyword); List<GuideTest> findAllByPathContainingAndDepthOrderByOrderDesc(String path, Integer depth);
int countAllBy(); GuideTest findByPath(String path);
// int countAllByParentId(String parentId); GuideTest findByPathContainingAndDepthAndOrder(String path, Integer depth, Integer order);
} }
package com.vazil.vridge.docs.service; package com.vazil.vridge.docs.service;
import com.vazil.vridge.docs.model.Guide;
import com.vazil.vridge.docs.model.GuideContent;
import com.vazil.vridge.docs.model.GuideTest; import com.vazil.vridge.docs.model.GuideTest;
import com.vazil.vridge.docs.repository.GuideContentRepository;
import com.vazil.vridge.docs.repository.GuideRepository;
import com.vazil.vridge.docs.repository.GuideTestRepository; import com.vazil.vridge.docs.repository.GuideTestRepository;
import com.vazil.vridge.docs.utils.TimeManager; import com.vazil.vridge.docs.utils.TimeManager;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
...@@ -18,167 +14,105 @@ import java.util.*; ...@@ -18,167 +14,105 @@ import java.util.*;
@AllArgsConstructor @AllArgsConstructor
@Log4j2 @Log4j2
public class GuideTestService { public class GuideTestService {
GuideRepository guideRepository; GuideTestRepository repository;
GuideTestRepository guideTestRepository;
GuideContentRepository guideContentRepository;
public List<GuideTest> findGuideIndex() throws Exception{ public List<GuideTest> findGuideIndex() throws Exception{
// List<GuideTest> guides = new ArrayList<>(); List<GuideTest> guideList = repository.findAllByOrderByOrder();
List<GuideTest> guideList = guideTestRepository.findAllByOrderByOrder();
// List<GuideTest> sortedGuideList = guideTestRepository.findAllByParentIdOrderByOrderAsc("");
// for(GuideTest guide : sortedGuideList) {
// guide.setChildren(getGuideTreeByParentId(guide.getId()));
// guides.add(guide);
// }
return guideList; return guideList;
} }
@Transactional @Transactional
public GuideTest findGuideById(String guideId) throws Exception{ public GuideTest findGuideById(String guideId) throws Exception{
GuideTest targetGuide = guideTestRepository.findById(guideId).orElseThrow(NoSuchElementException::new); GuideTest targetGuide = repository.findById(guideId).orElseThrow(NoSuchElementException::new);
int viewCount = targetGuide.getView() != null ? targetGuide.getView() : 0; int viewCount = targetGuide.getView() != null ? targetGuide.getView() : 0;
targetGuide.setView(viewCount + 1); targetGuide.setView(viewCount + 1);
guideTestRepository.save(targetGuide); repository.save(targetGuide);
// List<Guide> sortedGuideList = new ArrayList<>();
// for(Guide guide : guideRepository.findAllByParentIdOrderByOrderAsc("")) {
// sortedGuideList.add(guide);
// sortedGuideList.addAll(getGuideListByParentId(guide.getId()));
// }
//
// for(int i = 0; i < sortedGuideList.size(); i++) {
// if(sortedGuideList.get(i).getId().equals(guideId)) {
// if(i > 0) {
// targetGuide.setPrevContent(sortedGuideList.get(i-1));
// }
// if(i < sortedGuideList.size() - 1) {
// targetGuide.setNextContent(sortedGuideList.get(i+1));
// }
// break;
// }
// }
return targetGuide;
}
public List<Guide> search(String keyword) throws Exception{ targetGuide.setNextGuide(getNextGuide(targetGuide, false));
return guideRepository.findAllByKeywordSetContaining(keyword); targetGuide.setPrevGuide(getPrevGuide(targetGuide));
}
@Transactional return targetGuide;
public GuideTest createGuide(GuideTest guide) throws Exception{
// guide.setOrder(guide.getOrder() == null ? getLastOrder(guide.getParentId()) : 0);
guide.setLike(0);
guide.setView(0);
guide.setCreateDate(TimeManager.now());
return guideTestRepository.save(guide);
} }
@Transactional public GuideTest getPrevGuide(GuideTest targetGuide) throws Exception{
public Guide updateGuide(Guide guide) throws Exception{ String path = targetGuide.getPath();
Guide savedGuide = guideRepository.findById(guide.getId()).orElseThrow(NoSuchElementException::new); String parentPath = path.substring(0, path.lastIndexOf("/", path.length() -2) + 1);
savedGuide.setParentId(guide.getParentId()); // depth == 1, order == 0 인 경우 이전 guide 없음
savedGuide.setOrder(guide.getOrder()); if(targetGuide.getDepth() == 1 && targetGuide.getOrder() == 0)
savedGuide.setUpdateDate(TimeManager.now()); return null;
savedGuide.setContentKey(guide.getContentKey());
return guideRepository.save(savedGuide); // order == 0 인 경우 부모 guide 반환
} if(targetGuide.getOrder() == 0){
return repository.findByPath(parentPath);
@Transactional }
public Guide updateGuideKeyword(Guide guide) throws Exception{
Guide savedGuide = guideRepository.findById(guide.getId()).orElseThrow(NoSuchElementException::new);
savedGuide.setKeywordSet(guide.getKeywordSet());
return guideRepository.save(savedGuide);
}
@Transactional // 이전 형제의 자식이 없을 경우 해당 형제 반환
public Guide deleteGuide(String guideId, boolean cascade) throws Exception{ GuideTest prevSibling = repository.findByPathContainingAndDepthAndOrder(parentPath, targetGuide.getDepth(), targetGuide.getOrder() - 1);
Guide guide = guideRepository.findById(guideId).orElseThrow(NoSuchElementException::new); if(repository.findAllByPathContainingAndDepth(prevSibling.getPath(), prevSibling.getDepth() + 1).size() == 0){
String targetId = guide.getId(); return prevSibling;
String parentId = guide.getParentId(); } else { // 이전 형제의 자식이 있을 경우 가장 마지막 자식 반환
List<Guide> children = getGuideListByParentId(targetId); return getLastChildGuide(prevSibling);
if(cascade) {
for(Guide g : children) {
guideRepository.delete(g);
//guideContentRepository.deleteByGuideId(g.getId());
}
} else {
for(Guide g : guideRepository.findAllByParentIdOrderByOrderAsc(targetId)) {
g.setParentId(parentId);
g.setOrder(getLastOrder(parentId));
guideRepository.save(g);
}
} }
guideRepository.delete(guide);
//guideContentRepository.deleteByGuideId(guide.getId());
return guide;
} }
/////////////////////////////////////////////////////// public GuideTest getLastChildGuide(GuideTest targetGuide) throws Exception{
// Guide Content CRUD GuideTest lastChild = repository.findAllByPathContainingAndDepthOrderByOrderDesc(targetGuide.getPath(), targetGuide.getDepth() + 1).get(0);
@Transactional if(repository.findAllByPathContainingAndDepth(lastChild.getPath(), lastChild.getDepth() + 1).size() == 0){
public GuideContent findContentById(String guideId) throws Exception{ return lastChild;
GuideContent targetContent = guideContentRepository.findByGuideId(guideId).orElseThrow(NoSuchElementException::new); } else {
List<Guide> sortedGuideList = new ArrayList<>(); return getLastChildGuide(lastChild);
for(Guide guide : guideRepository.findAllByParentIdOrderByOrderAsc("")) {
sortedGuideList.add(guide);
sortedGuideList.addAll(getGuideListByParentId(guide.getId()));
}
for(int i = 0; i < sortedGuideList.size(); i++) {
if(sortedGuideList.get(i).getId().equals(guideId)) {
if(i > 0) {
targetContent.setPrevContent(sortedGuideList.get(i-1));
}
if(i < sortedGuideList.size() - 1) {
targetContent.setNextContent(sortedGuideList.get(i+1));
}
}
} }
return targetContent;
} }
@Transactional
public GuideContent updateGuideContent(GuideContent guideContent) throws Exception{
GuideContent savedGuideContent = guideContentRepository.findById(guideContent.getId()).orElseThrow(NoSuchElementException::new);
savedGuideContent.setContent(guideContent.getContent());
savedGuideContent.setUpdateDate(TimeManager.now());
return guideContentRepository.save(savedGuideContent);
}
/**
* @param targetGuide
* @param onlySibling true일 경우, 자식 guide는 무시하고 다음 형제 guide를 찾음
*/
public GuideTest getNextGuide(GuideTest targetGuide, Boolean onlySibling) throws Exception{
String path = targetGuide.getPath();
String parentPath = targetGuide.getDepth() == 1 ?
"/" : path.substring(0, path.lastIndexOf("/", path.length() -2) + 1);
GuideTest nextGuide = null;
///////////////////////////////////////////////// // child가 있으면 order == 0 인 자식 반환 (onlySibling == true 일 경우 패스하여 형제 반환으로 넘어감)
// Common if(!onlySibling){
nextGuide = repository.findByPathContainingAndDepthAndOrder(path, targetGuide.getDepth() + 1, 0);
}
// 트리 구조로 자식 guide를 가져옴 // child가 없으면 다음 형제 반환
private List<Guide> getGuideTreeByParentId(String parentId) { if(nextGuide == null){
List<Guide> children = new ArrayList<>(); nextGuide = repository.findByPathContainingAndDepthAndOrder(parentPath, targetGuide.getDepth(), targetGuide.getOrder() + 1);
for(Guide childGuide : guideRepository.findAllByParentIdOrderByOrderAsc(parentId)) {
childGuide.setChildren(guideRepository.findAllByParentIdOrderByOrderAsc(childGuide.getId()));
children.add(childGuide);
} }
return children;
}
//트리 구조의 guide를 1차원 리스트로 가져옴 // child가 없고, 다음 형제 없는 경우, 부모 guide의 nextGuide 탐색
private List<Guide> getGuideListByParentId(String parentId) { if(nextGuide == null && targetGuide.getDepth() != 1) {
List<Guide> children = new ArrayList<>(); GuideTest parentGuide = repository.findByPath(parentPath);
for(Guide childGuide : guideRepository.findAllByParentIdOrderByOrderAsc(parentId)) { nextGuide = getNextGuide(parentGuide, true);
children.add(childGuide);
children.addAll(guideRepository.findAllByParentIdOrderByOrderAsc(childGuide.getId()));
} }
return children;
return nextGuide;
} }
//현재 뎁스의 마지막 순번을 가져옴 @Transactional
private Integer getLastOrder(String parentId) { public GuideTest createGuide(GuideTest guide) throws Exception{
return guideRepository.countAllByParentId(parentId); // guide.setOrder(guide.getOrder() == null ? getLastOrder(guide.getParentId()) : 0);
guide.setLike(0);
guide.setView(0);
guide.setCreateDate(TimeManager.now());
return repository.save(guide);
} }
@Transactional
public GuideTest updateGuide(GuideTest guide) throws Exception{
GuideTest savedGuide = repository.findById(guide.getId()).orElseThrow(NoSuchElementException::new);
savedGuide.setOrder(guide.getOrder());
savedGuide.setUpdateDate(TimeManager.now());
return repository.save(savedGuide);
}
} }
...@@ -84,7 +84,7 @@ ...@@ -84,7 +84,7 @@
<v-divider class="my-8"/> <v-divider class="my-8"/>
<v-row no-gutters align="center"> <v-row no-gutters align="center">
<v-btn class="primary--text" :ripple="false" height="64" text outlined @click="openGuide(guide.prevContent)" v-if="guide !== null && guide.prevContent"> <v-btn class="primary--text" :ripple="false" height="64" text outlined @click="openGuide(guide.prevGuide)" v-if="guide !== null && guide.prevGuide">
<v-row no-gutters align="center"> <v-row no-gutters align="center">
<v-col class="mr-8"> <v-col class="mr-8">
<v-icon left>mdi-chevron-left</v-icon> <v-icon left>mdi-chevron-left</v-icon>
...@@ -92,7 +92,7 @@ ...@@ -92,7 +92,7 @@
<v-col align="end" justify="end"> <v-col align="end" justify="end">
<span class="grey--text">이전 페이지</span> <br/> <span class="grey--text">이전 페이지</span> <br/>
<span style="font-size:1.1rem;font-weight:bold;"> <span style="font-size:1.1rem;font-weight:bold;">
{{parsingContentTitle(guide.prevContent)}} {{guide.prevGuide.title}}
</span> </span>
</v-col> </v-col>
...@@ -107,13 +107,13 @@ ...@@ -107,13 +107,13 @@
<v-spacer/> <v-spacer/>
<v-btn class="primary--text" :ripple="false" height="64" text outlined @click="openGuide(guide.nextContent)" v-if="guide !== null && guide.nextContent"> <v-btn class="primary--text" :ripple="false" height="64" text outlined @click="openGuide(guide.nextGuide)" v-if="guide !== null && guide.nextGuide">
<v-row no-gutters align="center"> <v-row no-gutters align="center">
<v-col align="start" justify="start"> <v-col align="start" justify="start">
<span class="grey--text">다음 페이지</span> <br/> <span class="grey--text">다음 페이지</span> <br/>
<span style="font-size:1.1rem;font-weight:bold;"> <span style="font-size:1.1rem;font-weight:bold;">
{{ parsingContentTitle(guide.nextContent)}} {{ guide.nextGuide.title}}
</span> </span>
</v-col> </v-col>
<v-col class="ml-8"> <v-col class="ml-8">
...@@ -157,7 +157,7 @@ export default { ...@@ -157,7 +157,7 @@ export default {
await this.getGuideContents() await this.getGuideContents()
this.loading = false this.loading = false
// this.generateNavitator() this.generateNavigator()
}, },
async getGuideContents() { async getGuideContents() {
...@@ -272,9 +272,7 @@ export default { ...@@ -272,9 +272,7 @@ export default {
}, },
openGuide(guide) { openGuide(guide) {
this.$router.push('/' + this.parsingContentTitle(guide)) this.$router.push('/' + guide.id)
this.$store.commit('setGuide', {guideId:guide.id, flag:true})
this.$store.state.guideId = guide.id
}, },
openGithub(contentKey) { openGithub(contentKey) {
...@@ -286,7 +284,7 @@ export default { ...@@ -286,7 +284,7 @@ export default {
return false return false
}, },
async generateNavitator() { async generateNavigator() {
this.loading = true this.loading = true
let newNavigator = [] let newNavigator = []
......
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