Advertisement
Guest User

Untitled

a guest
May 20th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  import Boom from 'boom';
  2. import { models } from './../../db';
  3.  
  4. export default class UserController {
  5.  
  6.   static async fetchAll(ctx) {
  7.     ctx.body = await models.user.findAll({ include: [models.task] });
  8.   }
  9.  
  10.   static async fetchOne(ctx) {
  11.     const user = await models.user.findOne({
  12.       where: { id: ctx.params.id },
  13.       include: [models.task]
  14.     });
  15.     if (!user) throw Boom.notFound();
  16.     ctx.body = user;
  17.   }
  18.  
  19.   static async create(ctx) {
  20.     const { first_name, last_name } = ctx.request.body;
  21.     const user = await models.user.create({ first_name, last_name });
  22.     ctx.body = user;
  23.   }
  24.  
  25.   static async update(ctx) {
  26.     const { first_name, last_name } = ctx.request.body;
  27.     const { id } = ctx.params;
  28.  
  29.     const user = await models.user.findById(id);
  30.     if (!user) throw Boom.notFound();
  31.  
  32.     ctx.body = await user.update({ first_name, last_name });
  33.   }
  34.  
  35.   static async remove(ctx) {
  36.     const user = await models.user.findById(ctx.params.id);
  37.     if (!user) throw Boom.notFound();
  38.     await user.destroy();
  39.     ctx.status = 204;
  40.   }
  41.  
  42. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement