From 2ae49eb4c86176ee41ae19eeb392de5fa3c54086 Mon Sep 17 00:00:00 2001 From: xion Date: Mon, 7 Apr 2025 17:04:22 +0800 Subject: [PATCH] feat: add demo --- src/app-demo/index.ts | 119 +++++++++++++++++++++++++++++++++++ src/app-demo/models/index.ts | 71 +++++++++++++++++++++ submodules/pay-center-code | 2 +- 3 files changed, 191 insertions(+), 1 deletion(-) create mode 100644 src/app-demo/index.ts create mode 100644 src/app-demo/models/index.ts diff --git a/src/app-demo/index.ts b/src/app-demo/index.ts new file mode 100644 index 0000000..366a77c --- /dev/null +++ b/src/app-demo/index.ts @@ -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); diff --git a/src/app-demo/models/index.ts b/src/app-demo/models/index.ts new file mode 100644 index 0000000..098bfc3 --- /dev/null +++ b/src/app-demo/models/index.ts @@ -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>; + +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); +}); diff --git a/submodules/pay-center-code b/submodules/pay-center-code index 0db28ae..1cd6e15 160000 --- a/submodules/pay-center-code +++ b/submodules/pay-center-code @@ -1 +1 @@ -Subproject commit 0db28aecde061c597338839408d848ecfe59efdf +Subproject commit 1cd6e15ebdeec1dc381584074cf72179972ebacb