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