Commit 48aae8f1 authored by shj's avatar shj

[REFACTOR] GuideIndex 모델 개편(코드,파일 정리), 클래스 이름 전반적으로 개선

parent 7f1a4c26
package com.vazil.vridge.docs.controller;
import com.vazil.vridge.docs.model.Guide;
import com.vazil.vridge.docs.model.GuideContent;
import com.vazil.vridge.docs.service.GuideService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@AllArgsConstructor
@Log4j2
@RequestMapping("/api")
@CrossOrigin
@Api("가이드 API V1")
public class GuideController {
GuideService guideService;
@ApiOperation(value="모든 가이드 목차 조회")
@GetMapping("/index")
public ResponseEntity<?> findIndex()throws Exception{
return new ResponseEntity<>(guideService.findGuideIndex(), HttpStatus.OK);
}
@ApiOperation(value="모든 가이드 목차 검색")
@GetMapping("/search")
public ResponseEntity<?> search(@RequestParam(value = "keyword", required = false) String keyword) throws Exception{
return new ResponseEntity<>(guideService.search(keyword), HttpStatus.OK);
}
@ApiOperation(value="가이드 타이틀 추가")
@PostMapping("/index")
public ResponseEntity<?> createGuideIndex(@RequestBody Guide guide) throws Exception{
return new ResponseEntity<>(guideService.createGuide(guide), HttpStatus.CREATED);
}
@PutMapping("/index")
public ResponseEntity<?> updateGuideIndex(@RequestBody Guide guide) throws Exception{
return new ResponseEntity<>(guideService.updateGuide(guide), HttpStatus.OK);
}
@PutMapping("/keyword")
public ResponseEntity<?> updateGuideKeyword(@RequestBody Guide guide) throws Exception{
return new ResponseEntity<>(guideService.updateGuideKeyword(guide), HttpStatus.OK);
}
@GetMapping
public ResponseEntity<?> getGuide(@RequestParam(value = "guideId") String guideId) throws Exception{
return new ResponseEntity<>(guideService.findGuideById(guideId), HttpStatus.OK);
}
@DeleteMapping
public ResponseEntity<?> deleteGuideIndex(@RequestParam(value = "guideId") String guideId, @RequestParam(value = "cascade", required = false) boolean cascade) throws Exception{
return new ResponseEntity<>(guideService.deleteGuide(guideId, cascade), HttpStatus.OK);
}
@PutMapping
public ResponseEntity<?> updateGuideContent(@RequestBody GuideContent guideContent) throws Exception{
return new ResponseEntity<>(guideService.updateGuideContent(guideContent), HttpStatus.OK);
}
}
package com.vazil.vridge.docs.controller;
import com.vazil.vridge.docs.model.GuideIndex;
import com.vazil.vridge.docs.service.GuideIndexService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@AllArgsConstructor
@Log4j2
@RequestMapping("/guide-index")
@CrossOrigin
@Api("가이드 API V1")
public class GuideIndexController {
GuideIndexService service;
@ApiOperation(value="가이드 인덱스 조회")
@GetMapping
public ResponseEntity<?> getById(@RequestParam(value = "id") String id) throws Exception{
return new ResponseEntity<>(service.getById(id), HttpStatus.OK);
}
@ApiOperation(value="가이드 인덱스 전체 조회")
@GetMapping("/all")
public ResponseEntity<?> getAll()throws Exception{
return new ResponseEntity<>(service.getAll(), HttpStatus.OK);
}
@ApiOperation(value="가이드 인덱스 추가")
@PostMapping
public ResponseEntity<?> create(@RequestBody GuideIndex index) throws Exception{
return new ResponseEntity<>(service.create(index), HttpStatus.CREATED);
}
@PutMapping
public ResponseEntity<?> update(@RequestBody GuideIndex index) throws Exception{
return new ResponseEntity<>(service.update(index), HttpStatus.OK);
}
@DeleteMapping
public ResponseEntity<?> delete(@RequestParam(value = "id") String id) throws Exception{
return new ResponseEntity<>(service.delete(id), HttpStatus.OK);
}
}
package com.vazil.vridge.docs.controller;
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.service.GuideService;
import com.vazil.vridge.docs.service.GuideTestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@AllArgsConstructor
@Log4j2
@RequestMapping("/test")
@CrossOrigin
@Api("가이드 API V1")
public class GuideTestController {
GuideService guideService;
GuideTestService guideTestService;
@GetMapping
public ResponseEntity<?> getGuide(@RequestParam(value = "guideId") String guideId) throws Exception{
return new ResponseEntity<>(guideTestService.findGuideById(guideId), HttpStatus.OK);
}
@ApiOperation(value="모든 가이드 목차 조회")
@GetMapping("/index")
public ResponseEntity<?> findIndex()throws Exception{
return new ResponseEntity<>(guideTestService.findGuideIndex(), HttpStatus.OK);
}
@ApiOperation(value="모든 가이드 목차 검색")
@GetMapping("/search")
public ResponseEntity<?> search(@RequestParam(value = "keyword", required = false) String keyword) throws Exception{
return new ResponseEntity<>(guideService.search(keyword), HttpStatus.OK);
}
@ApiOperation(value="가이드 타이틀 추가")
@PostMapping
public ResponseEntity<?> createGuideIndex(@RequestBody GuideTest guide) throws Exception{
return new ResponseEntity<>(guideTestService.createGuide(guide), HttpStatus.CREATED);
}
@PutMapping("/index")
public ResponseEntity<?> updateGuideIndex(@RequestBody GuideTest guide) throws Exception{
return new ResponseEntity<>(guideTestService.updateGuide(guide), HttpStatus.OK);
}
@PutMapping("/keyword")
public ResponseEntity<?> updateGuideKeyword(@RequestBody Guide guide) throws Exception{
return new ResponseEntity<>(guideService.updateGuideKeyword(guide), HttpStatus.OK);
}
@DeleteMapping
public ResponseEntity<?> deleteGuideIndex(@RequestParam(value = "guideId") String guideId) throws Exception{
return new ResponseEntity<>(guideTestService.deleteGuide(guideId), HttpStatus.OK);
}
@PutMapping
public ResponseEntity<?> updateGuideContent(@RequestBody GuideContent guideContent) throws Exception{
return new ResponseEntity<>(guideService.updateGuideContent(guideContent), HttpStatus.OK);
}
}
package com.vazil.vridge.docs.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import nonapi.io.github.classgraph.json.Id;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.Document;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Document(value= "guide")
@Data
public class Guide implements Serializable {
private static final long serialVersionUID = 1928367242L;
@Id
private String id;
private String parentId;
private String title;
private Integer order;
private Integer like;
private Integer view;
private String contentKey;
private Set<String> keywordSet;
@CreatedDate
private LocalDateTime createDate;
@LastModifiedDate
private LocalDateTime updateDate;
private LocalDateTime deleteDate;
@Transient
List<Guide> children;
@Transient
private Guide prevContent;
@Transient
private Guide nextContent;
}
package com.vazil.vridge.docs.model;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mongodb.core.mapping.Document;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Document(value= "guideContent")
public class GuideContent {
@Id
private String id;
private String guideId;
private String title;
private String content;
private Integer like;
private Integer view;
@Transient
private Guide prevContent;
@Transient
private Guide nextContent;
@CreatedDate
private LocalDateTime createDate;
@LastModifiedDate
private LocalDateTime updateDate;
private LocalDateTime deleteDate;
}
......@@ -15,9 +15,9 @@ import java.time.LocalDateTime;
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Document(value= "guide-test")
@Document(value= "guide-index")
@Data
public class GuideTest implements Serializable {
public class GuideIndex implements Serializable {
private static final long serialVersionUID = 1928367242L;
@Id
private String id;
......@@ -30,9 +30,9 @@ public class GuideTest implements Serializable {
private Integer like;
@Transient
private GuideTest prevGuide;
private GuideIndex prevIndex;
@Transient
private GuideTest nextGuide;
private GuideIndex nextIndex;
private LocalDateTime createDate;
@LastModifiedDate
......
package com.vazil.vridge.docs.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.LastModifiedDate;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GuideSub {
@Id
private String id;
private String title;
private String contents;
private String router;
@CreatedDate
private LocalDateTime createDate;
@LastModifiedDate
private LocalDateTime updateDate;
private LocalDateTime deleteDate;
}
package com.vazil.vridge.docs.repository;
import com.vazil.vridge.docs.model.GuideContent;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.Optional;
@Repository
public interface GuideContentRepository extends CrudRepository<GuideContent, String> {
Optional<GuideContent> findByGuideId(String guideId);
void deleteByGuideId(String guideId);
}
package com.vazil.vridge.docs.repository;
import com.vazil.vridge.docs.model.GuideIndex;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface GuideIndexRepository extends CrudRepository<GuideIndex, String> {
List<GuideIndex> findAll();
List<GuideIndex> findAllByOrderByOrder();
List<GuideIndex> findAllByPathContainingAndDepth(String path, Integer depth);
List<GuideIndex> findAllByPathContainingAndDepthOrderByOrderDesc(String path, Integer depth);
GuideIndex findByPath(String path);
GuideIndex findByPathContainingAndDepthAndOrder(String path, Integer depth, Integer order);
}
package com.vazil.vridge.docs.repository;
import com.vazil.vridge.docs.model.Guide;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface GuideRepository extends CrudRepository<Guide, String> {
List<Guide> findAllByParentIdOrderByOrderAsc(String parentId);
List<Guide> findAllByKeywordSetContaining(String keyword);
int countAllBy();
int countAllByParentId(String parentId);
}
package com.vazil.vridge.docs.repository;
import com.vazil.vridge.docs.model.Guide;
import com.vazil.vridge.docs.model.GuideTest;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface GuideTestRepository extends CrudRepository<GuideTest, String> {
List<GuideTest> findAll();
List<GuideTest> findAllByOrderByOrder();
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.GuideTest;
import com.vazil.vridge.docs.repository.GuideTestRepository;
import com.vazil.vridge.docs.model.GuideIndex;
import com.vazil.vridge.docs.repository.GuideIndexRepository;
import com.vazil.vridge.docs.utils.TimeManager;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.util.List;
import java.util.NoSuchElementException;
@Service
@AllArgsConstructor
@Log4j2
public class GuideTestService {
GuideTestRepository repository;
public List<GuideTest> findGuideIndex() throws Exception{
List<GuideTest> guideList = repository.findAllByOrderByOrder();
return guideList;
}
public class GuideIndexService {
GuideIndexRepository repository;
GuideTestRepository testRepository;
@Transactional
public GuideTest findGuideById(String guideId) throws Exception{
GuideTest targetGuide = repository.findById(guideId).orElseThrow(NoSuchElementException::new);
public GuideIndex getById(String id) throws Exception{
GuideIndex db = repository.findById(id).orElseThrow(NoSuchElementException::new);
int viewCount = targetGuide.getView() != null ? targetGuide.getView() : 0;
targetGuide.setView(viewCount + 1);
repository.save(targetGuide);
int viewCount = db.getView() != null ? db.getView() : 0;
db.setView(viewCount + 1);
repository.save(db);
targetGuide.setNextGuide(getNextGuide(targetGuide, false));
targetGuide.setPrevGuide(getPrevGuide(targetGuide));
db.setNextIndex(getNextIndex(db, false));
db.setPrevIndex(getPrevIndex(db));
return targetGuide;
return db;
}
public GuideTest getPrevGuide(GuideTest targetGuide) throws Exception{
String path = targetGuide.getPath();
public GuideIndex getPrevIndex(GuideIndex targetIndex) throws Exception{
String path = targetIndex.getPath();
String parentPath = path.substring(0, path.lastIndexOf("/", path.length() -2) + 1);
// depth == 1, order == 0 인 경우 이전 guide 없음
if(targetGuide.getDepth() == 1 && targetGuide.getOrder() == 0)
// depth == 1, order == 0 인 경우 이전 인덱스 없음
if(targetIndex.getDepth() == 1 && targetIndex.getOrder() == 0)
return null;
// order == 0 인 경우 부모 guide 반환
if(targetGuide.getOrder() == 0){
// order == 0 인 경우 부모 인덱스 반환
if(targetIndex.getOrder() == 0){
return repository.findByPath(parentPath);
}
// 이전 형제의 자식이 없을 경우 해당 형제 반환
GuideTest prevSibling = repository.findByPathContainingAndDepthAndOrder(parentPath, targetGuide.getDepth(), targetGuide.getOrder() - 1);
GuideIndex prevSibling = repository.findByPathContainingAndDepthAndOrder(parentPath, targetIndex.getDepth(), targetIndex.getOrder() - 1);
if(repository.findAllByPathContainingAndDepth(prevSibling.getPath(), prevSibling.getDepth() + 1).size() == 0){
return prevSibling;
} else { // 이전 형제의 자식이 있을 경우 가장 마지막 자식 반환
return getLastChildGuide(prevSibling);
return getLastChildIndex(prevSibling);
}
}
public GuideTest getLastChildGuide(GuideTest targetGuide) throws Exception{
GuideTest lastChild = repository.findAllByPathContainingAndDepthOrderByOrderDesc(targetGuide.getPath(), targetGuide.getDepth() + 1).get(0);
public GuideIndex getLastChildIndex(GuideIndex targetIndex) throws Exception{
GuideIndex lastChild = repository.findAllByPathContainingAndDepthOrderByOrderDesc(targetIndex.getPath(), targetIndex.getDepth() + 1).get(0);
if(repository.findAllByPathContainingAndDepth(lastChild.getPath(), lastChild.getDepth() + 1).size() == 0){
return lastChild;
} else {
return getLastChildGuide(lastChild);
return getLastChildIndex(lastChild);
}
}
/**
* @param targetGuide
* @param targetIndex
* @param onlySibling true일 경우, 자식 guide는 무시하고 다음 형제 guide를 찾음
*/
public GuideTest getNextGuide(GuideTest targetGuide, Boolean onlySibling) throws Exception{
String path = targetGuide.getPath();
String parentPath = targetGuide.getDepth() == 1 ?
public GuideIndex getNextIndex(GuideIndex targetIndex, Boolean onlySibling) throws Exception{
String path = targetIndex.getPath();
String parentPath = targetIndex.getDepth() == 1 ?
"/" : path.substring(0, path.lastIndexOf("/", path.length() -2) + 1);
GuideTest nextGuide = null;
GuideIndex nextIndex = null;
// child가 있으면 order == 0 인 자식 반환 (onlySibling == true 일 경우 패스하여 형제 반환으로 넘어감)
if(!onlySibling){
nextGuide = repository.findByPathContainingAndDepthAndOrder(path, targetGuide.getDepth() + 1, 0);
nextIndex = repository.findByPathContainingAndDepthAndOrder(path, targetIndex.getDepth() + 1, 0);
}
// child가 없으면 다음 형제 반환
if(nextGuide == null){
nextGuide = repository.findByPathContainingAndDepthAndOrder(parentPath, targetGuide.getDepth(), targetGuide.getOrder() + 1);
if(nextIndex == null){
nextIndex = repository.findByPathContainingAndDepthAndOrder(parentPath, targetIndex.getDepth(), targetIndex.getOrder() + 1);
}
// child가 없고, 다음 형제 없는 경우, 부모 guide의 nextGuide 탐색
if(nextGuide == null && targetGuide.getDepth() != 1) {
GuideTest parentGuide = repository.findByPath(parentPath);
nextGuide = getNextGuide(parentGuide, true);
// child가 없고, 다음 형제 없는 경우, 부모 guide의 nextIndex 탐색
if(nextIndex == null && targetIndex.getDepth() != 1) {
GuideIndex parentIndex = repository.findByPath(parentPath);
nextIndex = getNextIndex(parentIndex, true);
}
return nextGuide;
return nextIndex;
}
@Transactional
public GuideTest createGuide(GuideTest guide) throws Exception{
// guide.setOrder(guide.getOrder() == null ? getLastOrder(guide.getParentId()) : 0);
public List<GuideIndex> getAll() throws Exception{
return repository.findAllByOrderByOrder();
}
@Transactional
public GuideIndex create(GuideIndex index) throws Exception{
// todo : 기존 인덱스와 위치가 중복될 경우, 기존 인덱스들 order + 1
guide.setLike(0);
guide.setView(0);
guide.setCreateDate(TimeManager.now());
return repository.save(guide);
index.setLike(0);
index.setView(0);
index.setCreateDate(TimeManager.now());
return repository.save(index);
}
@Transactional
public GuideTest updateGuide(GuideTest guide) throws Exception{
GuideTest db = repository.findById(guide.getId()).orElseThrow(NoSuchElementException::new);
public GuideIndex update(GuideIndex index) throws Exception{
GuideIndex db = repository.findById(index.getId()).orElseThrow(NoSuchElementException::new);
// todo : 기존 인덱스와 위치가 중복될 경우, 기존 인덱스들 order + 1
db.setLocale(guide.getLocale());
db.setTitle(guide.getTitle());
db.setOrder(guide.getOrder());
db.setPath(guide.getPath());
db.setDepth(guide.getDepth());
db.setLocale(index.getLocale());
db.setTitle(index.getTitle());
db.setOrder(index.getOrder());
db.setPath(index.getPath());
db.setDepth(index.getDepth());
return repository.save(db);
}
@Transactional
public String deleteGuide(String guideId) throws Exception{
GuideTest db = repository.findById(guideId).orElseThrow(NoSuchElementException::new);
public String delete(String id) throws Exception{
GuideIndex db = repository.findById(id).orElseThrow(NoSuchElementException::new);
repository.delete(db);
return db.getId();
}
......
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.repository.GuideContentRepository;
import com.vazil.vridge.docs.repository.GuideRepository;
import com.vazil.vridge.docs.utils.TimeManager;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
@Service
@AllArgsConstructor
@Log4j2
public class GuideService {
GuideRepository guideRepository;
GuideContentRepository guideContentRepository;
public List<Guide> findGuideIndex() throws Exception{
List<Guide> guides = new ArrayList<>();
List<Guide> sortedGuideList = guideRepository.findAllByParentIdOrderByOrderAsc("");
for(Guide guide : sortedGuideList) {
guide.setChildren(getGuideTreeByParentId(guide.getId()));
guides.add(guide);
}
return guides;
}
@Transactional
public Guide findGuideById(String guideId) throws Exception{
Guide targetGuide = guideRepository.findById(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) {
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{
return guideRepository.findAllByKeywordSetContaining(keyword);
}
@Transactional
public Guide createGuide(Guide guide) throws Exception{
guide.setCreateDate(TimeManager.now());
guide.setUpdateDate(TimeManager.now());
guide.setOrder(guide.getOrder() == null ? getLastOrder(guide.getParentId()) : 0);
guide.setParentId(StringUtils.isEmpty(guide.getParentId()) ? "" : guide.getParentId());
guide.setLike(0);
guide.setView(0);
return guideRepository.save(guide);
}
@Transactional
public Guide updateGuide(Guide guide) throws Exception{
Guide savedGuide = guideRepository.findById(guide.getId()).orElseThrow(NoSuchElementException::new);
savedGuide.setParentId(guide.getParentId());
savedGuide.setOrder(guide.getOrder());
savedGuide.setUpdateDate(TimeManager.now());
savedGuide.setContentKey(guide.getContentKey());
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);
}
@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);
}
}
guideRepository.delete(guide);
//guideContentRepository.deleteByGuideId(guide.getId());
return guide;
}
///////////////////////////////////////////////////////
// Guide Content CRUD
@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));
}
}
}
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);
}
/////////////////////////////////////////////////
// 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;
}
//트리 구조의 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()));
}
return children;
}
//현재 뎁스의 마지막 순번을 가져옴
private Integer getLastOrder(String parentId) {
return guideRepository.countAllByParentId(parentId);
}
}
......@@ -320,7 +320,7 @@ export default{
async getGuideIndex(){
this.loadingPageList = true
await this.$axios.get('/test/index')
await this.$axios.get('/guide-index/all')
.then(res => {
this.rawIndexList = res.data;
this.getIndexTree(JSON.parse(JSON.stringify(res.data)));
......@@ -422,7 +422,7 @@ export default{
return
}
this.$axios.post('/test', this.formData)
this.$axios.post('/guide-index', this.formData)
.then(res=>{
this.getGuideIndex()
})
......@@ -438,7 +438,7 @@ export default{
return
}
this.$axios.put('/test/index', this.formData)
this.$axios.put('/guide-index', this.formData)
.then(res=>{
this.getGuideIndex()
})
......@@ -447,9 +447,9 @@ export default{
})
},
removeIndex(){
this.$axios.delete("/test", {
this.$axios.delete("/guide-index", {
params:{
guideId: this.formData.id,
id: this.formData.id,
}
})
.then(res => {
......
......@@ -143,9 +143,9 @@ export default {
methods:{
async getGuide(id){
this.loading = true
await this.$axios.get('/test',{
await this.$axios.get('/guide-index',{
params:{
guideId: id
id
}
})
.then(res=>{
......
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