u
This commit is contained in:
14
src/store/category/create.js
Normal file
14
src/store/category/create.js
Normal file
@@ -0,0 +1,14 @@
|
||||
import sql from 'mssql';
|
||||
import { CREATE } from '../../sql/category.js';
|
||||
|
||||
export default function create(pool) {
|
||||
return async ({ parent_id, sort }) => {
|
||||
const result = await pool
|
||||
.request()
|
||||
.input('parentId', sql.Int, parent_id)
|
||||
.input('sort', sql.Int, sort)
|
||||
.query(CREATE);
|
||||
|
||||
return result.recordset[0].id;
|
||||
};
|
||||
}
|
||||
@@ -1,30 +1,6 @@
|
||||
import sql from 'mssql';
|
||||
import { GET_TREE } from '../../sql/category.js';
|
||||
|
||||
function buildTree(rows, rootId) {
|
||||
const byId = new Map();
|
||||
for (const row of rows) {
|
||||
byId.set(row.id, {
|
||||
id: row.id,
|
||||
parent_id: row.parent_id,
|
||||
sort: row.sort,
|
||||
children: [],
|
||||
});
|
||||
}
|
||||
|
||||
let root = null;
|
||||
for (const node of byId.values()) {
|
||||
if (node.id === rootId) {
|
||||
root = node;
|
||||
continue;
|
||||
}
|
||||
const parent = byId.get(node.parent_id);
|
||||
if (parent) parent.children.push(node);
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
export default function getTree(pool) {
|
||||
return async (rootId) => {
|
||||
const result = await pool
|
||||
@@ -32,7 +8,6 @@ export default function getTree(pool) {
|
||||
.input('rootId', sql.Int, rootId)
|
||||
.query(GET_TREE);
|
||||
|
||||
if (result.recordset.length === 0) return null;
|
||||
return buildTree(result.recordset, rootId);
|
||||
return result.recordset;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user