Todo与Folder
增加 Todo 的一些业务
在 controller 下面新建 todo.ts, 为了更加的健壮大家可以自行把 catch 里面的逻辑补完。
import * as Koa from 'koa';
import { Todo } from '../model/todo';
export default {
async create(ctx: Koa.Context, next){
try {
const { todo_folder_id, text, completed } = ctx.request.fields;
let todo = await Todo.create({
todo_folder_id,
text,
completed
});
ctx.body = todo;
} catch (e) {
console.error(e);
}
},
async edit(ctx: Koa.Context, next){
try {
const { text = null, completed = null, todo_folder_id = null } = ctx.request.fields;
const id = ctx.params.id;
const todo = await Todo.findOne({
where: {
id
}
});
if(text) todo.text = text;
if(completed) todo.completed = completed;
if(todo_folder_id) todo.todo_folder_id = todo_folder_id;
await todo.save();
ctx.body = todo;
} catch (error) {
console.error(error);
}
},
async show(ctx: Koa.Context, next){
try {
const id = ctx.params.id
const todo = await Todo.findOne({
where: {
id
}
});
ctx.body = todo;
} catch (error) {
console.error(error)
}
},
async delete(ctx: Koa.Context, next){
try {
const id = ctx.params['id'];
await Todo.destroy({
where: {
id
}
});
ctx.body = "删除成功";
} catch (error) {
console.error(error);
}
}
}增加路由
增加 folder 业务逻辑
在controller 目录下面新建 folder.ts
增加路由
Last updated