Jelajahi Sumber

add: 添加返回父子关系的标签接口

yyy2015 7 tahun lalu
induk
melakukan
14609b6dd8

+ 1 - 0
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/dao/TagDao.java

@@ -16,4 +16,5 @@ public interface TagDao extends JpaRepository<Tag, Long> {
     Tag findById(Long id);
     List<Tag> findByParentId(Long parentId);
     List<Tag> findByNameLike(String name);
+    List<Tag> findByParentIdIsNull();
 }

+ 1 - 1
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/models/Tag.java

@@ -9,7 +9,7 @@ public class Tag {
 
     @Id
     @GeneratedValue
-    private Long id;
+    private long id;
 
     @Column(name = "name")
     private String name;

+ 3 - 1
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/service/TagService.java

@@ -16,6 +16,8 @@ public interface TagService {
      */
     List<Tag> getTagsByParent(Long tagId);
 
+    List<Tag> getDirectSubTagsByParent(long tagId);
+
     /**
      * 根据TagName模糊查找Tag
      *
@@ -23,6 +25,6 @@ public interface TagService {
      */
     List<Tag> getTagsByNameLike(String tagName);
 
-    List<Tag> getAllTags();
+    List<Tag> getAllParentTags();
 
 }

+ 9 - 4
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/service/impl/TagServiceImpl.java

@@ -27,14 +27,19 @@ public class TagServiceImpl implements TagService{
             Tag nowTag = queue.poll();
             List<Tag> tags = tagDao.findByParentId(nowTag.getId());
             tags.stream().forEach(tag -> {
-                result.add(nowTag);
-                queue.add(nowTag);
+                result.add(tag);
+                queue.add(tag);
             });
         }
         return result;
     }
 
     @Override
+    public List<Tag> getDirectSubTagsByParent(long tagId) {
+        return tagDao.findByParentId(tagId);
+    }
+
+    @Override
     public List<Tag> getTagsByNameLike(String tagName) {
         tagName = "%"+tagName.trim()+"%";
         List<Tag> result = new ArrayList<>();
@@ -61,7 +66,7 @@ public class TagServiceImpl implements TagService{
     }
 
     @Override
-    public List<Tag> getAllTags() {
-        return tagDao.findAll();
+    public List<Tag> getAllParentTags() {
+        return tagDao.findByParentIdIsNull();
     }
 }

+ 17 - 0
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/web/ctrl/TagController.java

@@ -1,5 +1,15 @@
 package cn.iselab.mooctest.site.web.ctrl;
 
+import cn.iselab.mooctest.site.common.constant.UrlConstants;
+import cn.iselab.mooctest.site.web.data.TagVO;
+import cn.iselab.mooctest.site.web.data.response.ResponseVO;
+import cn.iselab.mooctest.site.web.data.response.ServerCode;
+import cn.iselab.mooctest.site.web.logic.TagLogic;
+import java.util.List;
+import org.apache.shiro.authz.annotation.RequiresRoles;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
 import org.springframework.web.bind.annotation.RestController;
 
 /**
@@ -8,5 +18,12 @@ import org.springframework.web.bind.annotation.RestController;
 @RestController
 public class TagController {
 
+    @Autowired
+    TagLogic tagLogic;
 
+    @RequiresRoles(value = "manager")
+    @RequestMapping(value = UrlConstants.API_COMMON+"tags", method = RequestMethod.GET)
+    public ResponseVO<List<TagVO>> getAllParentTags(){
+        return new ResponseVO<>(ServerCode.SUCCESS,tagLogic.getFirstLevelTags());
+    }
 }

+ 20 - 0
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/web/data/TagVO.java

@@ -0,0 +1,20 @@
+package cn.iselab.mooctest.site.web.data;
+
+import java.util.List;
+import lombok.AllArgsConstructor;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+/**
+ * Created on 2018/10/25
+ */
+@Data
+@AllArgsConstructor
+@NoArgsConstructor
+public class TagVO {
+
+    private long id;
+    private String name;
+    private List<TagVO> subTags;
+
+}

+ 44 - 0
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/web/data/wrapper/fromDev/TagVOWrapper.java

@@ -0,0 +1,44 @@
+package cn.iselab.mooctest.site.web.data.wrapper.fromDev;
+
+import cn.iselab.mooctest.site.models.Tag;
+import cn.iselab.mooctest.site.service.TagService;
+import cn.iselab.mooctest.site.web.data.TagVO;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Stack;
+import org.springframework.beans.BeanUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.util.CollectionUtils;
+
+/**
+ * Created on 2018/10/25
+ */
+@Component
+public class TagVOWrapper {
+
+    @Autowired
+    TagService tagService;
+
+    public TagVO wrapTag2VORecurse(Tag tag){
+        if(tag == null){
+            return null;
+        }
+
+        TagVO tagVO = new TagVO();
+        BeanUtils.copyProperties(tag,tagVO,"parentId","subTags");
+
+        List<TagVO> subTagVOs = new ArrayList<>();
+        List<Tag> subTags = tagService.getDirectSubTagsByParent(tagVO.getId());
+        if(!CollectionUtils.isEmpty(subTags)){
+            for(Tag subTag: subTags){
+                TagVO subTagVO = wrapTag2VORecurse(subTag);
+                subTagVOs.add(subTagVO);
+            }
+        }
+        tagVO.setSubTags(subTagVOs);
+
+        return tagVO;
+    }
+
+}

+ 2 - 1
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/web/logic/TagLogic.java

@@ -2,11 +2,12 @@ package cn.iselab.mooctest.site.web.logic;
 
 import cn.iselab.mooctest.site.models.Tag;
 
+import cn.iselab.mooctest.site.web.data.TagVO;
 import java.util.List;
 
 public interface TagLogic {
 
     List<Tag> getTagByNameLike(String name);
 
-    List<Tag> getAllTags();
+    List<TagVO> getFirstLevelTags();
 }

+ 9 - 2
mooctest-site-server/src/main/java/cn/iselab/mooctest/site/web/logic/impl/TagLogicImpl.java

@@ -2,7 +2,10 @@ package cn.iselab.mooctest.site.web.logic.impl;
 
 import cn.iselab.mooctest.site.models.Tag;
 import cn.iselab.mooctest.site.service.TagService;
+import cn.iselab.mooctest.site.web.data.TagVO;
+import cn.iselab.mooctest.site.web.data.wrapper.fromDev.TagVOWrapper;
 import cn.iselab.mooctest.site.web.logic.TagLogic;
+import java.util.stream.Collectors;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
@@ -13,6 +16,8 @@ public class TagLogicImpl implements TagLogic{
 
     @Autowired
     private TagService tagService;
+    @Autowired
+    private TagVOWrapper tagVOWrapper;
 
     @Override
     public List<Tag> getTagByNameLike(String name) {
@@ -20,8 +25,10 @@ public class TagLogicImpl implements TagLogic{
     }
 
     @Override
-    public List<Tag> getAllTags() {
-        return tagService.getAllTags();
+    public List<TagVO> getFirstLevelTags() {
+        List<Tag> tags =  tagService.getAllParentTags();
+        return tags.stream().map(tag -> tagVOWrapper.wrapTag2VORecurse(tag)).collect(
+                Collectors.toList());
     }
 
 }