feat: add packages for edit

This commit is contained in:
xion 2024-10-29 01:09:36 +08:00
parent e032c092d9
commit 6021aca4ba
4 changed files with 196 additions and 0 deletions

View File

@ -19,3 +19,5 @@ import './github/index.ts';
import './app-manager/index.ts'; import './app-manager/index.ts';
import './file/index.ts'; import './file/index.ts';
import './packages/index.ts';

View File

@ -0,0 +1 @@
import './list.ts'

120
src/routes/packages/list.ts Normal file
View File

@ -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);

View File

@ -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<InstanceType<typeof PackagesModel>>;
/**
*
*/
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);
});