SHARE
TWEET

Untitled

a guest Mar 14th, 2019 134 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import jwt from 'jsonwebtoken';
  2. import bcrypt from 'bcrypt';
  3. import User from '../models/user';
  4. import Beer from '../models/beer';
  5. import { SECRET, SALTINESS } from '../config';
  6. import onPromiseError from './utils';
  7.  
  8. const onGetList = (req, res) => {
  9.   const onUsersFound = (users) => {
  10.     res.json({
  11.       message: 'se ha realizado con exito',
  12.       data: users,
  13.     });
  14.   };
  15.   User.find({})
  16.     .populate('beersTaken')
  17.     .then(onUsersFound)
  18.     .catch(onPromiseError(res));
  19. };
  20.  
  21. const onGetEntity = (req, res) => {
  22.   const { id } = req.params;
  23.   const onUserFound = (user) => {
  24.     res.json({
  25.       message: 'se ha realizado con exito',
  26.       data: user,
  27.     });
  28.   };
  29.  
  30.   User.findOne({ _id: id })
  31.     .populate('beersTaken')
  32.     .then(onUserFound)
  33.     .catch(onPromiseError(res));
  34. };
  35.  
  36. const onCreateEntity = (req, res) => {
  37.   const { name, email, password } = req.body;
  38.   const onSaltHash = (hash) => {
  39.     const newUser = new User({
  40.       name,
  41.       email,
  42.       password: hash,
  43.       beersTaken: [],
  44.     });
  45.     const onSaveUser = (userSaved) => {
  46.       res.json({
  47.         message: 'Usuario creado con exito',
  48.         data: userSaved,
  49.       });
  50.     };
  51.     newUser
  52.       .save()
  53.       .then(onSaveUser)
  54.       .catch(onPromiseError(res));
  55.   };
  56.   bcrypt
  57.     .hash(password, SALTINESS)
  58.     .then(onSaltHash)
  59.     .catch(onPromiseError(res));
  60. };
  61.  
  62. const onUpdateEntity = (req, res) => {
  63.   const { id } = req.params;
  64.   const { name, email, password } = req.body;
  65.   const onUserFound = (user) => {
  66.     user.name = name;
  67.     user.email = email;
  68.     user.password = password;
  69.     const onUserSaved = (userSaved) => {
  70.       res.json({
  71.         message: 'Usuario Actualizado',
  72.         data: userSaved,
  73.       });
  74.     };
  75.     user
  76.       .save()
  77.       .then(onUserSaved)
  78.       .catch(onPromiseError(res));
  79.   };
  80.   User.findOne({ _id: id })
  81.     .then(onUserFound)
  82.     .catch(onPromiseError(res));
  83. };
  84.  
  85. const onDeleteEntity = (req, res) => {
  86.   const { id } = req.params;
  87.   const onUserDeleted = () => {
  88.     res.json({ message: 'Usuario borrado correctamente' });
  89.   };
  90.   User.deleteOne({ _id: id })
  91.     .then(onUserDeleted)
  92.     .catch(onPromiseError(res));
  93. };
  94.  
  95. const onDrankBeer = (req, res) => {
  96.   if (!req.user) {
  97.     res.status(400).json({
  98.       message: 'Permiso denegado',
  99.     });
  100.     return;
  101.   }
  102.   const { beerID } = req.params;
  103.   const onBeerFound = (beerFound) => {
  104.     if (!beerFound) {
  105.       return res.status(404).json({ message: 'cheve not found' });
  106.     }
  107.  
  108.     req.user.beersTaken.push(beerFound._id);
  109.     const onUserSaved = () => {
  110.       res.json({
  111.         message: 'User drank a beer succesfully',
  112.       });
  113.     };
  114.     req.user
  115.       .save()
  116.       .then(onUserSaved)
  117.       .catch(onPromiseError(res));
  118.   };
  119.   Beer.findOne({ _id: beerID })
  120.     .then(onBeerFound)
  121.     .catch(onPromiseError(res));
  122. };
  123.  
  124. const onLogin = (req, res) => {
  125.   const { email, password } = req.body;
  126.   const onUserFound = (user) => {
  127.     if (!user) {
  128.       return res.status(404).json({
  129.         message: 'Usuario no encontrado',
  130.       });
  131.    }
  132.     const onCompare = (isEqual) => {
  133.       if (!isEqual) {
  134.         res.status(300).json({ message: 'wrong password' });
  135.       }
  136.       const token = jwt.sign({ id: user._id }, SECRET, { expiresIn: 240 });
  137.       res.json({ auth: true, token });
  138.     };
  139.     //  const onCompareError = ({ message }) => {
  140.     //    res.status(500).send({ message });
  141.     //  };
  142.     bcrypt
  143.       .compare(password, user.password)
  144.       .then(onCompare)
  145.       //      .catch(onCompareError);
  146.       .catch(onPromiseError(res));
  147.   };
  148.   const onUserFoundError = ({ message }) => {
  149.     res.status(500).json({
  150.       message,
  151.     });
  152.   };
  153.   User.findOne({ email })
  154.     .then(onUserFound)
  155.     .catch(onUserFoundError);
  156. };
  157.  
  158. export default (app) => {
  159.   app.get('/user', onGetList);
  160.   app.get('/user/:id', onGetEntity);
  161.   app.post('/user', onCreateEntity);
  162.   app.post('/user/login', onLogin);
  163.   app.put('/user/:id', onUpdateEntity);
  164.   app.put('/user/drank/:beerID', onDrankBeer);
  165.   app.delete('/user/:id', onDeleteEntity);
  166. };
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top