Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. router.put(
  2. "/stock",
  3. auth,
  4. [
  5. check("ticker", "symbol is require or incorrect.")
  6. .not()
  7. .isEmpty(),
  8. check("name", "Stock name is required")
  9. .not()
  10. .isEmpty(),
  11. check(
  12. "qty",
  13. "Quantity of the stock purchased is required and should be numeric"
  14. )
  15. .isNumeric()
  16. .not()
  17. .isEmpty(),
  18. check(
  19. "dateInvested",
  20. "Date of investment is required and should be valid date format."
  21. )
  22. .not()
  23. .isEmpty(),
  24. check(
  25. "priceInvested",
  26. "Price of the stock purchased is required and should be numeric"
  27. )
  28. .isNumeric()
  29. .not()
  30. .isEmpty()
  31. ],
  32. async (req, res) => {
  33. const errors = validationResult(req);
  34. if (!errors.isEmpty) {
  35. return res.status(400).json({ errors: errors.array() });
  36. }
  37.  
  38. const { ticker, name, qty, dateInvested, priceInvested } = req.body;
  39.  
  40. //Build Portfolio object
  41. const newStock = {
  42. ticker,
  43. name,
  44. qty,
  45. dateInvested,
  46. priceInvested
  47. };
  48.  
  49. try {
  50. let portfolio = await Portfolio.updateOne({ user: req.user.id });
  51.  
  52. //add stock to the portfolio
  53. portfolio.stocks.unshift(newStock);
  54.  
  55. await portfolio.save();
  56.  
  57. res.json(portfolio);
  58.  
  59. } catch (err) {
  60. console.error(err.message);
  61. res.status(500).send("Sever Error");
  62. }
  63. }
  64. );
  65.  
  66. {
  67. "_id": "5d0757fc0747c72cd0e4da04",
  68. "user": "5d01abfc0f97b52dac7044bf",
  69. "portname": "MyPortfolio",
  70. "goal": "Make a profit of 50%",
  71. "stocks": [
  72. {
  73. "_id": "5d0758050747c72cd0e4da06",
  74. "ticker": "AAPL",
  75. "name": "Apple",
  76. "qty": 22,
  77. "dateInvested": "2018-06-30T07:00:00.000Z",
  78. "priceInvested": 99
  79. },
  80. {
  81. "_id": "5d0758000747c72cd0e4da05",
  82. "ticker": "AAPL",
  83. "name": "Apple",
  84. "qty": 22,
  85. "dateInvested": "2018-06-30T07:00:00.000Z",
  86. "priceInvested": 99
  87. }
  88. ],
  89. "date": "2019-06-17T09:06:04.412Z",
  90. "__v": 2
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement