feat: add demo
This commit is contained in:
parent
b4c1ddd57d
commit
2ae49eb4c8
119
src/app-demo/index.ts
Normal file
119
src/app-demo/index.ts
Normal file
@ -0,0 +1,119 @@
|
||||
import { Op } from 'sequelize';
|
||||
import { AppDemoModel } from './models/index.ts';
|
||||
import { app } from '@/app.ts';
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'app-demo',
|
||||
key: 'list',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { page = 1, pageSize = 20, search, sort = 'DESC' } = ctx.query;
|
||||
const searchWhere = search
|
||||
? {
|
||||
[Op.or]: [{ title: { [Op.like]: `%${search}%` } }, { summary: { [Op.like]: `%${search}%` } }],
|
||||
}
|
||||
: {};
|
||||
|
||||
const { rows: appDemo, count } = await AppDemoModel.findAndCountAll({
|
||||
where: {
|
||||
uid: tokenUser.uid,
|
||||
...searchWhere,
|
||||
},
|
||||
offset: (page - 1) * pageSize,
|
||||
limit: pageSize,
|
||||
order: [['updatedAt', sort]],
|
||||
});
|
||||
|
||||
ctx.body = {
|
||||
list: appDemo,
|
||||
pagination: {
|
||||
page,
|
||||
current: page,
|
||||
pageSize,
|
||||
total: count,
|
||||
},
|
||||
};
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'app-demo',
|
||||
key: 'update',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { id, data, updatedAt: _clear, createdAt: _clear2, ...rest } = ctx.query.data;
|
||||
let appDemo: AppDemoModel;
|
||||
let isNew = false;
|
||||
if (id) {
|
||||
const appDemo = await AppDemoModel.findByPk(id);
|
||||
if (appDemo.uid !== tokenUser.uid) {
|
||||
ctx.throw(403, 'No permission');
|
||||
}
|
||||
} else {
|
||||
appDemo = await AppDemoModel.create({
|
||||
data: data,
|
||||
...rest,
|
||||
uid: tokenUser.uid,
|
||||
});
|
||||
isNew = true;
|
||||
}
|
||||
if (!appDemo) {
|
||||
ctx.throw(404, 'AppDemo not found');
|
||||
}
|
||||
if (!isNew) {
|
||||
appDemo = await appDemo.update({
|
||||
data: { ...appDemo.data, ...data },
|
||||
...rest,
|
||||
});
|
||||
}
|
||||
|
||||
ctx.body = appDemo;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'app-demo',
|
||||
key: 'delete',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { id, force = false } = ctx.query.data || {};
|
||||
if (!id) {
|
||||
ctx.throw(400, 'id is required');
|
||||
}
|
||||
const appDemo = await AppDemoModel.findByPk(id);
|
||||
if (appDemo.uid !== tokenUser.uid) {
|
||||
ctx.throw(403, 'No permission');
|
||||
}
|
||||
await appDemo.destroy({ force });
|
||||
ctx.body = appDemo;
|
||||
})
|
||||
.addTo(app);
|
||||
|
||||
app
|
||||
.route({
|
||||
path: 'app-demo',
|
||||
key: 'get',
|
||||
middleware: ['auth'],
|
||||
})
|
||||
.define(async (ctx) => {
|
||||
const tokenUser = ctx.state.tokenUser;
|
||||
const { id } = ctx.query.data || {};
|
||||
if (!id) {
|
||||
ctx.throw(400, 'id is required');
|
||||
}
|
||||
const appDemo = await AppDemoModel.findByPk(id);
|
||||
if (appDemo.uid !== tokenUser.uid) {
|
||||
ctx.throw(403, 'No permission');
|
||||
}
|
||||
ctx.body = appDemo;
|
||||
})
|
||||
.addTo(app);
|
71
src/app-demo/models/index.ts
Normal file
71
src/app-demo/models/index.ts
Normal file
@ -0,0 +1,71 @@
|
||||
import { sequelize } from '@/modules/sequelize.ts';
|
||||
import { DataTypes, Model } from 'sequelize';
|
||||
|
||||
export interface AppDemoData {
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
export type AppDemo = Partial<InstanceType<typeof AppDemoModel>>;
|
||||
|
||||
export class AppDemoModel extends Model {
|
||||
declare id: string;
|
||||
declare title: string;
|
||||
declare description: string;
|
||||
declare summary: string;
|
||||
|
||||
declare data: AppDemoData;
|
||||
declare tags: string[];
|
||||
declare version: string;
|
||||
|
||||
declare uid: string;
|
||||
|
||||
declare createdAt: Date;
|
||||
declare updatedAt: Date;
|
||||
}
|
||||
|
||||
AppDemoModel.init(
|
||||
{
|
||||
id: {
|
||||
type: DataTypes.UUID,
|
||||
primaryKey: true,
|
||||
defaultValue: DataTypes.UUIDV4,
|
||||
},
|
||||
title: {
|
||||
type: DataTypes.TEXT,
|
||||
defaultValue: '',
|
||||
},
|
||||
description: {
|
||||
type: DataTypes.TEXT,
|
||||
defaultValue: '',
|
||||
},
|
||||
summary: {
|
||||
type: DataTypes.TEXT,
|
||||
defaultValue: '',
|
||||
},
|
||||
tags: {
|
||||
type: DataTypes.JSONB,
|
||||
defaultValue: [],
|
||||
},
|
||||
version: {
|
||||
type: DataTypes.INTEGER,
|
||||
defaultValue: 0,
|
||||
},
|
||||
data: {
|
||||
type: DataTypes.JSONB,
|
||||
defaultValue: {},
|
||||
},
|
||||
uid: {
|
||||
type: DataTypes.UUID,
|
||||
allowNull: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
sequelize,
|
||||
tableName: 'kv_app_demo',
|
||||
paranoid: true,
|
||||
},
|
||||
);
|
||||
|
||||
AppDemoModel.sync({ alter: true, logging: false }).catch((e) => {
|
||||
console.error('AppDemoModel sync', e);
|
||||
});
|
@ -1 +1 @@
|
||||
Subproject commit 0db28aecde061c597338839408d848ecfe59efdf
|
||||
Subproject commit 1cd6e15ebdeec1dc381584074cf72179972ebacb
|
Loading…
x
Reference in New Issue
Block a user