feat:保存;列表;菜单;todo:card点击回显等

This commit is contained in:
严争鸣 2024-12-10 17:18:57 +08:00
parent a73e44d49b
commit b113d1cdf0
10 changed files with 178 additions and 15 deletions

View File

@ -7,6 +7,13 @@ const knex = require('knex')({
database: 'sandbox', database: 'sandbox',
port: 54321, port: 54321,
}, },
pool: {
min: 2,
max: 10,
idleTimeoutMillis: 30000,
createTimeoutMillis: 3000,
acquireTimeoutMillis: 30000,
},
// connection:`postgres://postgres:123456@yhm.ink:54321/sandbox` // connection:`postgres://postgres:123456@yhm.ink:54321/sandbox`
}) })
@ -14,13 +21,17 @@ async function createTableIfNotExist({ tableName, columns }) {
const exists = await knex.schema.hasTable(tableName) const exists = await knex.schema.hasTable(tableName)
if (!exists) { if (!exists) {
await knex.schema.createTable(tableName, (table) => { await knex.schema.createTable(tableName, (table) => {
table.increments('id').primary() tableName === 'codes'
? table.increments('id').primary()
: table.string('id').primary()
table.timestamp('created_at').defaultTo(knex.fn.now()) table.timestamp('created_at').defaultTo(knex.fn.now())
table.timestamp('updated_at').defaultTo(knex.fn.now()) table.timestamp('updated_at').defaultTo(knex.fn.now())
columns.forEach((column) => { columns.forEach((column) => {
const { name, type, references } = column const { name, type, references, onDelete } = column
const tableExist = table[type](name) const tableExist = table[type](name)
references && tableExist.references(references).onDelete('CASCADE') references && onDelete
? tableExist.references(references).onDelete('CASCADE')
: tableExist.references(references)
}) })
}) })
console.log(`table ${tableName} created`) console.log(`table ${tableName} created`)
@ -40,16 +51,32 @@ const tables = [
tableName: 'types', tableName: 'types',
columns: [ columns: [
{ name: 'name', type: 'string' }, { name: 'name', type: 'string' },
{ name: 'parent_id', type: 'integer', references: 'types.id' }, {
name: 'parent_id',
type: 'string',
references: 'types.id',
onDelete: false,
},
], ],
}, },
{
tableName: 'templates',
columns: [{ name: 'name', type: 'string' }],
},
{ {
tableName: 'codes', tableName: 'codes',
columns: [ columns: [
{ {
name: 'type_id', name: 'type_id',
type: 'integer', type: 'string',
references: 'types.id', references: 'types.id',
onDelete: false,
},
{
name: 'template_id',
type: 'string',
references: 'templates.id',
onDelete: false,
}, },
{ {
name: 'title', name: 'title',
@ -70,7 +97,6 @@ const tables = [
], ],
}, },
] ]
createTables(tables) createTables(tables)
module.exports = { module.exports = {
knex, knex,

View File

@ -6,4 +6,16 @@ class Types extends Model {
} }
} }
// async function preTypes() {
// const types = await Types.query()
// if (types.length === 0) {
// await Types.query().insert([
// { name: 'Cesium' },
// { name: 'Vue' },
// { name: 'HTML' },
// ])
// }
// }
// preTypes()
module.exports = Types module.exports = Types

2
app.js
View File

@ -7,6 +7,7 @@ var logger = require('morgan')
var indexRouter = require('./routes/index') var indexRouter = require('./routes/index')
var usersRouter = require('./routes/users') var usersRouter = require('./routes/users')
var codesRouter = require('./routes/Codes') var codesRouter = require('./routes/Codes')
var typesRouter = require('./routes/Types')
var app = express() var app = express()
@ -23,6 +24,7 @@ app.use(express.static(path.join(__dirname, 'public')))
app.use('/', indexRouter) app.use('/', indexRouter)
app.use('/users', usersRouter) app.use('/users', usersRouter)
app.use('/codes', codesRouter) app.use('/codes', codesRouter)
app.use('/types', typesRouter)
// catch 404 and forward to error handler // catch 404 and forward to error handler
app.use(function (req, res, next) { app.use(function (req, res, next) {

View File

@ -0,0 +1,50 @@
const {
getCodeListByTypeId,
getCodeById,
addCode: addCodeService,
updateCode: updateCodeService,
} = require('../service')
const { sendResponse } = require('../../../utils')
const getCodeList = async (req, res) => {
const { typeId } = req.params
const gCodeList = await getCodeListByTypeId(typeId)
sendResponse({ res, status: 200, message: '', data: gCodeList })
}
const getCode = async (req, res) => {
const { id } = req.params
const gCode = await getCodeById(id)
sendResponse({ res, status: 200, message: '', data: gCode })
}
const addCode = async (req, res) => {
const { typeId, code, name, description } = req.body
const aCode = await addCodeService(req.body)
sendResponse({ res, status: 200, message: '', data: aCode })
}
const updateCode = async (req, res) => {
const { id } = req.params
if (!id) {
sendResponse({ res, status: 400, message: 'id is required', data: null })
return
}
const getCode = await getCodeById(id)
if (getCode) {
const uCode = await updateCodeService(id, req.body)
sendResponse({ res, status: 200, message: '', data: uCode })
} else {
sendResponse({ res, status: 400, message: 'id not found', data: null })
}
}
module.exports = {
getCodeList,
getCode,
addCode,
updateCode,
}

View File

@ -1,15 +1,11 @@
const { getCodeListByTypeId } = require('./service') const { getCodeList, getCode, addCode } = require('./controller')
const { sendResponse } = require('../../utils')
const express = require('express') const express = require('express')
const router = express.Router() const router = express.Router()
router.get('/list/:typeId', async (req, res) => { router.get('/list/:typeId', getCodeList)
const { typeId } = req.params router.get('/:id', getCode)
router.post('/', addCode)
const gCodeList = await getCodeListByTypeId(typeId) router.put('/', addCode)
sendResponse({ res, status: 200, message: '', data: gCodeList })
})
module.exports = router module.exports = router

View File

@ -6,6 +6,22 @@ async function getCodeListByTypeId(type_id) {
: await Codes.query() : await Codes.query()
} }
async function getCodeById(id) {
return await Codes.query().findById(id)
}
async function addCode(code) {
return await Codes.query().insert(code)
}
async function updateCode(code) {
const { id } = code
return await Codes.query().patchAndFetchById(id, code)
}
module.exports = { module.exports = {
getCodeListByTypeId, getCodeListByTypeId,
getCodeById,
addCode,
updateCode,
} }

View File

@ -0,0 +1,17 @@
const { sendResponse, arrayToTree } = require('../../../utils/index')
const { getTypes } = require('../service')
async function getTypeTree(req, res) {
const types = await getTypes()
const treeTypes = arrayToTree(types)
sendResponse({
res,
status: 200,
message: '',
data: treeTypes,
})
}
module.exports = {
getTypeTree,
}

8
routes/Types/index.js Normal file
View File

@ -0,0 +1,8 @@
const { getTypeTree } = require('./controller')
const express = require('express')
const router = express.Router()
router.get('/tree', getTypeTree)
module.exports = router

View File

@ -0,0 +1,9 @@
const Types = require('../../../PG/model/types')
async function getTypes() {
return await Types.query()
}
module.exports = {
getTypes,
}

View File

@ -8,6 +8,33 @@ function sendResponse({ res, status, message, data }) {
res.status(status).send(response) res.status(status).send(response)
} }
function arrayToTree(items) {
const map = new Map()
// 初始化 Map
items.forEach((item) => {
map.set(item.id, { ...item, children: null })
})
const result = []
items.forEach((item) => {
if (item.parent_id === null) {
result.push(map.get(item.id))
} else {
const parent = map.get(item.parent_id)
if (parent) {
parent.children = parent.children
? [...parent.children, map.get(item.id)]
: [map.get(item.id)]
// parent.children.push(map.get(item.id))
}
}
})
return result
}
module.exports = { module.exports = {
sendResponse, sendResponse,
arrayToTree,
} }