import { CustomError } from '@kevisual/router'; import { app } from '../../app.ts'; import { ContainerModel, ContainerData, Container } from './models/index.ts'; app .route({ path: 'container', key: 'list', middleware: ['auth'], }) .define(async (ctx) => { const tokenUser = ctx.state.tokenUser; const list = await ContainerModel.findAll({ order: [['updatedAt', 'DESC']], where: { uid: tokenUser.id, }, attributes: { exclude: ['code'] }, }); ctx.body = list; return ctx; }) .addTo(app); app .route({ path: 'container', key: 'get', middleware: ['auth'], }) .define(async (ctx) => { const tokenUser = ctx.state.tokenUser; const id = ctx.query.id; if (!id) { throw new CustomError('id is required'); } const container = await ContainerModel.findByPk(id); if (!container) { throw new CustomError('container not found'); } if (container.uid !== tokenUser.id) { throw new CustomError('container not found'); } ctx.body = container; return ctx; }) .addTo(app); app .route({ path: 'container', key: 'update', middleware: ['auth'], }) .define(async (ctx) => { const tokenUser = ctx.state.tokenUser; const data = ctx.query.data; const { id, ...container } = data; let containerModel: ContainerModel | null = null; if (id) { containerModel = await ContainerModel.findByPk(id); if (containerModel) { containerModel.update({ ...container, publish: { ...containerModel.publish, ...container.publish, }, }); await containerModel.save(); } } else { try { containerModel = await ContainerModel.create({ ...container, uid: tokenUser.id, }); } catch (e) { console.log('error', e); } console.log('containerModel', container); } ctx.body = containerModel; return ctx; }) .addTo(app); app .route({ path: 'container', key: 'delete', middleware: ['auth'], }) .define(async (ctx) => { const tokenUser = ctx.state.tokenUser; const id = ctx.query.id; const container = await ContainerModel.findByPk(id); if (!container) { throw new CustomError('container not found'); } if (container.uid !== tokenUser.id) { throw new CustomError('container not found'); } await container.destroy(); ctx.body = container; return ctx; }) .addTo(app);