Advertisement
Guest User

Untitled

a guest
Jun 7th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const koa = require('koa');
  2. const koaRouter = require('koa-router');
  3. const koaBodyParser = require('koa-bodyparser');
  4. const Ajv = require('ajv');
  5. const Promise = require('bluebird');
  6. const fs = require('fs');
  7. const ajv = new Ajv();
  8. const app = new koa();
  9. const router = new koaRouter();
  10.  
  11. Promise.promisifyAll(fs);
  12. app.use(koaBodyParser());
  13. app.use(async(ctx, next) => {
  14.   ctx.set('Content-Type', 'application/json');
  15.   await next();
  16. });
  17. app.use(async(ctx, next) => {
  18.   try { await next(); }
  19.   catch(err){
  20.     ctx.status = err.status || 500;
  21.     ctx.body = {error: err.message};
  22.     ctx.app.emit('error', err, ctx);
  23.   }
  24. })
  25.  
  26. const userSchema = {
  27.   properties: {
  28.     uuid: { type: 'integer'},
  29.     name: {type: 'string'},
  30.     profession: { type: 'string'},
  31.     password: {type: 'string'}
  32.   }
  33. }
  34.  
  35. const validateUser = ajv.compile(userSchema);
  36.  
  37. let userlist = []
  38.  
  39. const saveDb = async function(){
  40.   await fs.writeFileAsync('user.json', JSON.stringify(userlist));
  41. }
  42.  
  43. let actions = {
  44.   list: async (ctx) => {
  45.     ctx.body = JSON.stringify(userlist);
  46.   },
  47.   get: async (ctx) => {
  48.     let findUserList = userlist.filter(user => user.uuid === parseInt(ctx.params.uuid));
  49.     if (findUserList.length > 0) ctx.body = JSON.stringify(findUserList[0]);
  50.     else ctx.throw(404, 'User not found');
  51.   },
  52.   add: async (ctx) => {
  53.     if(!validateUser(ctx.request.body)) ctx.throw(400, 'Incorrect data');
  54.     const newUser = {
  55.       uuid: userlist.length+1,
  56.       name: ctx.request.body.name,
  57.       password: ctx.request.body.password,
  58.       profession: ctx.request.body.profession
  59.     }
  60.     userlist.push(newUser);
  61.     await saveDb();
  62.     ctx.status = 201;
  63.     ctx.body = newUser
  64.   },
  65.   update: async(ctx) => {
  66.     if(!validateUser(ctx.request.body)) ctx.throw(400, 'Incorrect data');
  67.     let userFound = userlist.find(user => user.uuid === parseInt(ctx.params.uuid));
  68.     if (!userFound) ctx.throw(404, 'User not found');
  69.     Object.assign(userFound, ctx.request.body);
  70.     await saveDb();
  71.     ctx.status = 200
  72.     ctx.body = userFound;
  73.   },
  74.   delete: async(ctx) => {
  75.     userlist = userlist.filter(user => user.uuid !== parseInt(ctx.params.uuid));
  76.     await saveDb();
  77.     ctx.status = 200;
  78.     ctx.body = {message: 'Delete successed.'}
  79.   }
  80. }
  81.  
  82. router.get('/', actions.list)
  83.       .get('/:uuid', actions.get)
  84.       .post('/', actions.add)
  85.       .put('/:uuid', actions.update)
  86.       .delete('/:uuid', actions.delete);
  87.      
  88. app.use(router.routes());
  89. app.use(router.allowedMethods());
  90.  
  91. app.listen(3000, () => {
  92.   console.info("Server started.");
  93. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement