78 lines
1.6 KiB
TypeScript
78 lines
1.6 KiB
TypeScript
import { ResourceModel } from './models/index.ts';
|
|
import { app } from '../../app.ts';
|
|
|
|
app
|
|
.route({
|
|
path: 'resource',
|
|
key: 'list',
|
|
middleware: ['auth'],
|
|
})
|
|
.define(async (ctx) => {
|
|
const tokenUser = ctx.state.tokenUser;
|
|
const list = await ResourceModel.findAll({
|
|
order: [['updatedAt', 'DESC']],
|
|
where: {
|
|
uid: tokenUser.id,
|
|
},
|
|
});
|
|
ctx.body = list;
|
|
return ctx;
|
|
})
|
|
.addTo(app);
|
|
|
|
app
|
|
.route({
|
|
path: 'resource',
|
|
key: 'get',
|
|
})
|
|
.define(async (ctx) => {
|
|
const id = ctx.query.id;
|
|
if (!id) {
|
|
ctx.throw('id is required');
|
|
}
|
|
const rm = await ResourceModel.findByPk(id);
|
|
if (!rm) {
|
|
ctx.throw('resource not found');
|
|
}
|
|
ctx.body = rm;
|
|
return ctx;
|
|
})
|
|
.addTo(app);
|
|
|
|
app
|
|
.route({ path: 'resource', key: 'update' })
|
|
.define(async (ctx) => {
|
|
const { data, id, ...rest } = ctx.query.data;
|
|
if (id) {
|
|
const resource = await ResourceModel.findByPk(id);
|
|
if (resource) {
|
|
const newResource = await resource.update({ data, ...rest });
|
|
ctx.body = newResource;
|
|
}
|
|
} else if (data) {
|
|
const resource = await ResourceModel.create({ data, ...rest });
|
|
ctx.body = resource;
|
|
}
|
|
})
|
|
.addTo(app);
|
|
|
|
app
|
|
.route({
|
|
path: 'resource',
|
|
key: 'delete',
|
|
middleware: ['auth'],
|
|
})
|
|
.define(async (ctx) => {
|
|
const id = ctx.query.id;
|
|
if (!id) {
|
|
ctx.throw('id is required');
|
|
}
|
|
const resource = await ResourceModel.findByPk(id);
|
|
if (!resource) {
|
|
ctx.throw('resource not found');
|
|
}
|
|
await resource.destroy();
|
|
ctx.body = 'success';
|
|
})
|
|
.addTo(app);
|