Commit 84e1ca6e authored by shj's avatar shj

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

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