Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. function convertFromUSD(value, currency) {
  2. switch(currency) {
  3. case 'USD': return value * 1;
  4. case 'GBP': return value * 0.6;
  5. case 'BTC': return value * 0.0023707918444761;
  6. default: return NaN;
  7. }
  8. }
  9.  
  10. // Vacation database code
  11. app.get('/vacations', function (req, res) {
  12. Vacation.find({available: true}, function (err, vacations) {
  13. var currency = req.session.currency || 'USD';
  14. var context = {
  15. currency: currency,
  16. vacations: vacations.map(function (vacation) {
  17. return {
  18. sku: vacation.sku,
  19. name: vacation.name,
  20. description: vacation.description,
  21. inSeason: vacation.inSeason,
  22. price: convertFromUSD(vacation.priceInCents/100, currency),
  23. qty: vacation.qty,
  24. }
  25. })
  26. };
  27. switch(currency) {
  28. case 'USD': context.currencyUSD = 'selected'; break;
  29. case 'GBP': context.currencyGBP = 'selected'; break;
  30. case 'BTC': context.currencyBTC = 'selected'; break;
  31. }
  32. res.render('vacations', context);
  33. });
  34. });
  35.  
  36. var VacationInSeasonListener = require('./models/vacationInSeasonListener');
  37.  
  38. app.get('/notify-me-when-in-season', function (req, res) {
  39. res.render('notify-me-when-in-season', {sku: req.query.sku});
  40. });
  41.  
  42. app.post('/notify-me-when-in-season', function (req, res) {
  43. VacationInSeasonListener.update(
  44. {email: req.body.email},
  45. {$push: {skus: req.body.sku}},
  46. {upsert: true},
  47. function (err) {
  48. if(err) {
  49. console.error(err.stack);
  50. req.session.flash = {
  51. type: 'danger',
  52. intro: 'Ooops!',
  53. message: 'There was an error processing your request.',
  54. };
  55. return res.redirect(303, '/vacations');
  56. }
  57. req.session.flash = {
  58. type: 'success',
  59. intro: 'Thank you!',
  60. message: 'You will be notified when this vacation is in season.',
  61. };
  62. res.redirect(303, '/vacations');
  63. }
  64. );
  65. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement