增加 Todo 的一些业务

在 controller 下面新建 todo.ts, 为了更加的健壮大家可以自行把 catch 里面的逻辑补完。

  1. import * as Koa from 'koa';
  2. import { Todo } from '../model/todo';
  3. export default {
  4. async create(ctx: Koa.Context, next){
  5. try {
  6. const { todo_folder_id, text, completed } = ctx.request.fields;
  7. let todo = await Todo.create({
  8. todo_folder_id,
  9. text,
  10. completed
  11. });
  12. ctx.body = todo;
  13. } catch (e) {
  14. console.error(e);
  15. }
  16. },
  17. async edit(ctx: Koa.Context, next){
  18. try {
  19. const { text = null, completed = null, todo_folder_id = null } = ctx.request.fields;
  20. const id = ctx.params.id;
  21. const todo = await Todo.findOne({
  22. where: {
  23. id
  24. }
  25. });
  26. if(text) todo.text = text;
  27. if(completed) todo.completed = completed;
  28. if(todo_folder_id) todo.todo_folder_id = todo_folder_id;
  29. await todo.save();
  30. ctx.body = todo;
  31. } catch (error) {
  32. console.error(error);
  33. }
  34. },
  35. async show(ctx: Koa.Context, next){
  36. try {
  37. const id = ctx.params.id
  38. const todo = await Todo.findOne({
  39. where: {
  40. id
  41. }
  42. });
  43. ctx.body = todo;
  44. } catch (error) {
  45. console.error(error)
  46. }
  47. },
  48. async delete(ctx: Koa.Context, next){
  49. try {
  50. const id = ctx.params['id'];
  51. await Todo.destroy({
  52. where: {
  53. id
  54. }
  55. });
  56. ctx.body = "删除成功";
  57. } catch (error) {
  58. console.error(error);
  59. }
  60. }
  61. }

增加路由

  1. api.get('/todo/:id', TodoController.show);
  2. api.post('/todo', TodoController.create);
  3. api.put('/todo/:id', TodoController.edit);
  4. api.del('/todo/:id', TodoController.delete);

增加 folder 业务逻辑

在controller 目录下面新建 folder.ts

  1. import { TodoFolder } from '../model/todoFolder'
  2. import * as Koa from 'koa';
  3. import { Todo } from '../model/todo';
  4. function getFields(ctx: Koa.Context, next){
  5. try {
  6. const { user_id, title } = ctx.request.fields;
  7. return [ user_id, title ];
  8. } catch (e) {
  9. console.error(e);
  10. ctx.status = 422;
  11. ctx.body = '[Unprocesable entity] \n验证失败,必须传递 user_id/title 字段';
  12. }
  13. }
  14. export default {
  15. async create(ctx: Koa.Context, next){
  16. const [ user_id, title ] = getFields(ctx, next);
  17. try {
  18. const folder = await TodoFolder.create({
  19. user_id,
  20. title
  21. });
  22. ctx.body = folder;
  23. } catch (e) {
  24. ctx.body = e.errors[0].message;
  25. }
  26. },
  27. async edit(ctx: Koa.Context, next){
  28. try{
  29. const id = ctx.params.id;
  30. const { title } = ctx.request.fields;
  31. let folder = await TodoFolder.findOne({
  32. where:{
  33. id
  34. }
  35. });
  36. if(title) folder.title = title;
  37. await folder.save();
  38. ctx.body = folder;
  39. }catch (e) {
  40. console.error(e);
  41. }
  42. },
  43. async show(ctx: Koa.Context, next){
  44. try {
  45. const id = ctx.params.id
  46. const folder = await TodoFolder.findOne({
  47. where: {
  48. id
  49. }
  50. });
  51. ctx.body = folder;
  52. } catch (error) {
  53. ctx.status = 422;
  54. ctx.body = '[Unprocesable entity] \n 验证失败,必须传递 folder_id 字段';
  55. }
  56. },
  57. async delete(ctx: Koa.Context, next){
  58. try {
  59. const id = ctx.params.id;
  60. await Todo.destroy({
  61. where: {
  62. 'todo_folder_id': id
  63. }
  64. });
  65. await TodoFolder.destroy({
  66. where: {
  67. id
  68. }
  69. });
  70. ctx.body = "删除成功";
  71. } catch (error) {
  72. ctx.status = 422;
  73. ctx.body = '[Unprocesable entity] \n验证失败,必须传递 folder_id 字段';
  74. }
  75. }
  76. }

增加路由

  1. api.get('/folder/:id', FolderController.show);
  2. api.post('/folder', FolderController.create);
  3. api.put('/folder/:id', FolderController.edit);
  4. api.del('/folder/:id', FolderController.delete);