Advertisement
Guest User

Untitled

a guest
May 20th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let actions = {
  2.     list: (req, res) => {
  3.         res.status(200).json(userlist);
  4.     },
  5.     get: (req, res) => {
  6.         let findUserList = userlist.filter(user => user.uuid === parseInt(req.params.uuid));
  7.         if (findUserList.length > 0) return res.status(200).json(findUserList[0]);
  8.         else return res.status(404).json({error: 'User not found'});
  9.     },
  10.     add: async (req, res) => {
  11.         if(!validateUser(req.bodyParser)) return res.status(400).json({error:'Incorrect data'});
  12.         const lastUser = userlist[userlist.length-1];
  13.         const newUser = {
  14.         uuid: lastUser.uuid+1,
  15.         name: req.body.name,
  16.         password: req.body.password,
  17.         profession: req.body.profession
  18.         }
  19.         userlist.push(newUser);
  20.         await saveDb();
  21.         res.status(201).json(newUser);
  22.     },
  23.     update: async (req, res) => {
  24.         if(!validateUser(req.body)) return res.status(400).json({error:'Incorrect data'});
  25.         let userFound = userlist.find(user => user.uuid === parseInt(req.params.uuid));
  26.         if (!userFound) return res.status(404).json({error:'User not found'});
  27.         Object.assign(userFound, req.body);
  28.         await saveDb();
  29.         res.status(200).json(userFound);
  30.     },
  31.     delete: async (req, res) => {
  32.         userlist = userlist.filter(user => user.uuid !== parseInt(req.params.uuid));
  33.         await saveDb();
  34.         res.status(200),json({message: 'Delete successed.'});
  35.     },
  36.     index: (req, res) => {
  37.         res.status(200);
  38.         res.setHeader('Content-Type', 'text/html');
  39.         res.body = fs.createReadStream(path.join(__dirname, 'index.html'));
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement