diff --git a/src/routes/index.ts b/src/routes/index.ts index 7087c5b..abec125 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -19,3 +19,5 @@ import './github/index.ts'; import './app-manager/index.ts'; import './file/index.ts'; + +import './packages/index.ts'; diff --git a/src/routes/packages/index.ts b/src/routes/packages/index.ts new file mode 100644 index 0000000..9166f9d --- /dev/null +++ b/src/routes/packages/index.ts @@ -0,0 +1 @@ +import './list.ts' \ No newline at end of file diff --git a/src/routes/packages/list.ts b/src/routes/packages/list.ts new file mode 100644 index 0000000..9fae6f4 --- /dev/null +++ b/src/routes/packages/list.ts @@ -0,0 +1,120 @@ +import { app } from '@/app.ts'; +import { PackagesModel } from './models/index.ts'; +import { Op } from 'sequelize'; +import { CustomError } from '@kevisual/router'; + +app + .route({ + path: 'packages', + key: 'list', + middleware: ['auth'], + }) + .define(async (ctx) => { + const tokenUser = ctx.state.tokenUser; + const { uid } = tokenUser; + const { page = 1, pageSize = 999, search } = ctx.query; + const searchWhere = search ? { title: { [Op.like]: `%${search}%` } } : {}; + const { rows: packages, count } = await PackagesModel.findAndCountAll({ + where: { + uid, + ...searchWhere, + }, + limit: pageSize, + offset: page * pageSize, + }); + ctx.body = { + pagination: { + current: page, + pageSize, + total: count, + }, + list: packages, + }; + }) + .addTo(app); + +app + .route({ + path: 'packages', + key: 'get', + middleware: ['auth'], + }) + .define(async (ctx) => { + const tokenUser = ctx.state.tokenUser; + const { uid } = tokenUser; + const { id } = ctx.query; + if (!id) { + throw new CustomError('id is required'); + } + const packages = await PackagesModel.findOne({ + where: { + uid, + id, + }, + }); + if (!packages) { + throw new CustomError('not found data'); + } + ctx.body = packages; + }) + .addTo(app); + +app + .route({ + path: 'packages', + key: 'update', + }) + .define(async (ctx) => { + const tokenUser = ctx.state.tokenUser; + const { uid } = tokenUser; + const { id, ...rest } = ctx.request.body; + let packages: PackagesModel; + if (!id) { + packages = await PackagesModel.create({ + ...rest, + uid, + }); + } else { + packages = await PackagesModel.findOne({ + where: { + uid, + id, + }, + }); + if (!packages) { + throw new CustomError('not found data'); + } + await packages.update({ + ...rest, + }); + } + ctx.body = packages; + }) + .addTo(app); + +app + .route({ + path: 'packages', + key: 'delete', + middleware: ['auth'], + }) + .define(async (ctx) => { + const tokenUser = ctx.state.tokenUser; + const { uid } = tokenUser; + const { id } = ctx.request.body; + if (!id) { + throw new CustomError('id is required'); + } + const packages = await PackagesModel.findOne({ + where: { + uid, + id, + }, + }); + if (!packages) { + throw new CustomError('not found data'); + } + await packages.destroy(); + ctx.body = packages; + }) + .addTo(app); diff --git a/src/routes/packages/models/index.ts b/src/routes/packages/models/index.ts new file mode 100644 index 0000000..be383f6 --- /dev/null +++ b/src/routes/packages/models/index.ts @@ -0,0 +1,73 @@ +import { sequelize } from '../../../modules/sequelize.ts'; +import { DataTypes, Model } from 'sequelize'; + +export interface PackagesData {} +export type PackagesPublish = { + key: string; + title?: string; + description?: string; + version?: string; + filesName?: any[]; +}; +export type Packages = Partial>; + +/** + * 用户代码容器 + */ +export class PackagesModel extends Model { + declare id: string; + declare title: string; + declare description: string; + declare tags: string[]; + declare data: PackagesData; // files + declare publish: PackagesPublish; + declare uid: string; + declare expand: any; +} +PackagesModel.init( + { + id: { + type: DataTypes.UUID, + primaryKey: true, + defaultValue: DataTypes.UUIDV4, + comment: 'id', + }, + title: { + type: DataTypes.TEXT, + defaultValue: '', + }, + description: { + type: DataTypes.TEXT, + defaultValue: '', + }, + tags: { + type: DataTypes.JSONB, + defaultValue: [], + }, + data: { + type: DataTypes.JSONB, + defaultValue: {}, + }, + publish: { + type: DataTypes.JSONB, + defaultValue: {}, + }, + expand: { + type: DataTypes.JSONB, + defaultValue: {}, + }, + uid: { + type: DataTypes.UUID, + allowNull: true, + }, + }, + { + sequelize, + tableName: 'kv_packages', + paranoid: true, + }, +); + +PackagesModel.sync({ alter: true, logging: false }).catch((e) => { + console.error('PackagesModel sync', e); +});