mirror of
https://gitee.com/bookshelfplus/bookshelfplus
synced 2025-09-22 01:30:40 +08:00
查询所有分类后端完成
This commit is contained in:
@@ -15,7 +15,6 @@ router.get('/search', function (req, res) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.get('/category', function (req, res) {
|
router.get('/category', function (req, res) {
|
||||||
console.log(req)
|
|
||||||
if (req.query.id) {
|
if (req.query.id) {
|
||||||
// 分类详情页
|
// 分类详情页
|
||||||
res.render('category-details', {
|
res.render('category-details', {
|
||||||
|
@@ -13,50 +13,39 @@
|
|||||||
</main>
|
</main>
|
||||||
<%- include("./component/footer.html"); %>
|
<%- include("./component/footer.html"); %>
|
||||||
|
|
||||||
<!-- 获取参数 -->
|
|
||||||
<script src="/assets/javascripts/getParams.js"></script>
|
|
||||||
<script>
|
<script>
|
||||||
// var requestParams = getParams();
|
getRequest("/category/list")
|
||||||
// var searchbox = document.getElementById("searchInput");
|
.then(function (response) {
|
||||||
// var categoryId = requestParams["id"] ?? "";
|
var axiosData = response.data;
|
||||||
// console.log("categoryId", categoryId);
|
var status = axiosData.status;
|
||||||
// if (categoryId === "") {
|
var data = axiosData.data;
|
||||||
// location.href = "/search";
|
if (status === "success") {
|
||||||
// }
|
console.log(data)
|
||||||
</script>
|
// if (data.description == "")
|
||||||
<script>
|
// data.description = "暂无描述";
|
||||||
// getRequest("/category/get", { id: categoryId })
|
|
||||||
// .then(function (response) {
|
|
||||||
// var axiosData = response.data;
|
|
||||||
// var status = axiosData.status;
|
|
||||||
// var data = axiosData.data;
|
|
||||||
// if (status === "success") {
|
|
||||||
// console.log(data)
|
|
||||||
// if (data.description == "")
|
|
||||||
// data.description = "暂无描述";
|
|
||||||
|
|
||||||
// var topCategory = data.parentId !== 0 ? `<a href="/category?id=${data.parentId}">上级分类</a>` : "";
|
// var topCategory = data.parentId !== 0 ? `<a href="/category?id=${data.parentId}">上级分类</a>` : "";
|
||||||
// document.getElementById("container").innerHTML = `
|
// document.getElementById("container").innerHTML = `
|
||||||
// <div class="grid">
|
// <div class="grid">
|
||||||
// <div class="grid-item">
|
// <div class="grid-item">
|
||||||
// <h1>${data.name}</h1>
|
// <h1>${data.name}</h1>
|
||||||
// <p>分类ID: ${data.id}</p>
|
// <p>分类ID: ${data.id}</p>
|
||||||
// <p>${topCategory}</p>
|
// <p>${topCategory}</p>
|
||||||
// </div>
|
// </div>
|
||||||
// <div class="grid-item">
|
// <div class="grid-item">
|
||||||
// <h2>简介</h2>
|
// <h2>简介</h2>
|
||||||
// <p>${data.description}</p>
|
// <p>${data.description}</p>
|
||||||
// </div>
|
// </div>
|
||||||
// </div>`;
|
// </div>`;
|
||||||
|
|
||||||
// // 渲染后重新获取一次字体
|
// 渲染后重新获取一次字体
|
||||||
// fontmin(getPageText());
|
fontmin(getPageText());
|
||||||
// } else {
|
} else {
|
||||||
// alert(`出错啦!${data.errMsg} (错误码: ${data.errCode}) `);
|
alert(`出错啦!${data.errMsg} (错误码: ${data.errCode}) `);
|
||||||
// }
|
}
|
||||||
// }).catch(function (error) {
|
}).catch(function (error) {
|
||||||
// console.log(error);
|
console.log(error);
|
||||||
// });
|
});
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
@@ -17,6 +17,9 @@ import plus.bookshelf.Service.Model.CategoryModel;
|
|||||||
import plus.bookshelf.Service.Service.BookService;
|
import plus.bookshelf.Service.Service.BookService;
|
||||||
import plus.bookshelf.Service.Service.CategoryService;
|
import plus.bookshelf.Service.Service.CategoryService;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Api(tags = "书籍分类信息")
|
@Api(tags = "书籍分类信息")
|
||||||
@Controller("category")
|
@Controller("category")
|
||||||
@RequestMapping("/category")
|
@RequestMapping("/category")
|
||||||
@@ -25,7 +28,7 @@ public class CategoryController extends BaseController {
|
|||||||
@Autowired
|
@Autowired
|
||||||
CategoryService categoryService;
|
CategoryService categoryService;
|
||||||
|
|
||||||
@ApiOperation(value = "获取书籍分类", notes = "获取书籍分类")
|
@ApiOperation(value = "获取指定分类", notes = "获取指定的书籍分类")
|
||||||
@RequestMapping(value = "get", method = {RequestMethod.GET})
|
@RequestMapping(value = "get", method = {RequestMethod.GET})
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public CommonReturnType get(@RequestParam(value = "id") Integer id) {
|
public CommonReturnType get(@RequestParam(value = "id") Integer id) {
|
||||||
@@ -38,6 +41,19 @@ public class CategoryController extends BaseController {
|
|||||||
return CommonReturnType.create(categoryVO);
|
return CommonReturnType.create(categoryVO);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ApiOperation(value = "获取所有分类", notes = "获取所有的书籍分类")
|
||||||
|
@RequestMapping(value = "list", method = {RequestMethod.GET})
|
||||||
|
@ResponseBody
|
||||||
|
public CommonReturnType getAll() {
|
||||||
|
List<CategoryModel> categoryModels = categoryService.getAllCategorys();
|
||||||
|
List<CategoryVO> categoryVOS = new ArrayList<>();
|
||||||
|
for (CategoryModel categoryModel : categoryModels) {
|
||||||
|
CategoryVO categoryVO = convertFromModel(categoryModel);
|
||||||
|
categoryVOS.add(categoryVO);
|
||||||
|
}
|
||||||
|
return CommonReturnType.create(categoryVOS);
|
||||||
|
}
|
||||||
|
|
||||||
private CategoryVO convertFromModel(CategoryModel categoryModel) {
|
private CategoryVO convertFromModel(CategoryModel categoryModel) {
|
||||||
if (categoryModel == null) {
|
if (categoryModel == null) {
|
||||||
return null;
|
return null;
|
||||||
|
@@ -62,4 +62,6 @@ public interface CategoryDOMapper {
|
|||||||
int updateByPrimaryKey(CategoryDO record);
|
int updateByPrimaryKey(CategoryDO record);
|
||||||
|
|
||||||
CategoryDO[] selectChildrenByCategoryId(Integer id);
|
CategoryDO[] selectChildrenByCategoryId(Integer id);
|
||||||
|
|
||||||
|
CategoryDO[] selectAll();
|
||||||
}
|
}
|
@@ -7,12 +7,21 @@ import plus.bookshelf.Dao.Mapper.CategoryDOMapper;
|
|||||||
import plus.bookshelf.Service.Model.CategoryModel;
|
import plus.bookshelf.Service.Model.CategoryModel;
|
||||||
import plus.bookshelf.Service.Service.CategoryService;
|
import plus.bookshelf.Service.Service.CategoryService;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class CategoryServiceImpl implements CategoryService {
|
public class CategoryServiceImpl implements CategoryService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private CategoryDOMapper categoryDOMapper;
|
private CategoryDOMapper categoryDOMapper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取指定分类详情
|
||||||
|
*
|
||||||
|
* @param id
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
@Override
|
@Override
|
||||||
public CategoryModel getCategoryById(Integer id) {
|
public CategoryModel getCategoryById(Integer id) {
|
||||||
CategoryDO categoryDO = categoryDOMapper.selectByPrimaryKey(id);
|
CategoryDO categoryDO = categoryDOMapper.selectByPrimaryKey(id);
|
||||||
@@ -31,6 +40,23 @@ public class CategoryServiceImpl implements CategoryService {
|
|||||||
return categoryModel;
|
return categoryModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 取得所有分类
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<CategoryModel> getAllCategorys() {
|
||||||
|
CategoryDO[] categoryDOS = categoryDOMapper.selectAll();
|
||||||
|
List<CategoryModel> categoryModels = new ArrayList<>();
|
||||||
|
|
||||||
|
for (CategoryDO categoryDO : categoryDOS) {
|
||||||
|
CategoryModel categoryModel = convertFromDataObject(categoryDO);
|
||||||
|
categoryModels.add(categoryModel);
|
||||||
|
}
|
||||||
|
return categoryModels;
|
||||||
|
}
|
||||||
|
|
||||||
// 转换时不转换父亲与儿子,否则会陷入死循环
|
// 转换时不转换父亲与儿子,否则会陷入死循环
|
||||||
private CategoryModel convertFromDataObject(CategoryDO categoryDO) {
|
private CategoryModel convertFromDataObject(CategoryDO categoryDO) {
|
||||||
if (categoryDO == null) {
|
if (categoryDO == null) {
|
||||||
|
@@ -2,6 +2,8 @@ package plus.bookshelf.Service.Service;
|
|||||||
|
|
||||||
import plus.bookshelf.Service.Model.CategoryModel;
|
import plus.bookshelf.Service.Model.CategoryModel;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public interface CategoryService {
|
public interface CategoryService {
|
||||||
/**
|
/**
|
||||||
* 通过分类Id获取分类
|
* 通过分类Id获取分类
|
||||||
@@ -10,4 +12,11 @@ public interface CategoryService {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
CategoryModel getCategoryById(Integer id);
|
CategoryModel getCategoryById(Integer id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取所有的分类列表
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
List<CategoryModel> getAllCategorys();
|
||||||
}
|
}
|
||||||
|
@@ -182,4 +182,9 @@
|
|||||||
from category_info
|
from category_info
|
||||||
where parent_id = #{parentId,jdbcType=INTEGER}
|
where parent_id = #{parentId,jdbcType=INTEGER}
|
||||||
</select>
|
</select>
|
||||||
|
<select id="selectAll" resultMap="BaseResultMap">
|
||||||
|
select
|
||||||
|
<include refid="Base_Column_List" />
|
||||||
|
from category_info
|
||||||
|
</select>
|
||||||
</mapper>
|
</mapper>
|
Reference in New Issue
Block a user