diff --git a/controllers/watchlistController.js b/controllers/watchlistController.js new file mode 100644 index 0000000..d1017e3 --- /dev/null +++ b/controllers/watchlistController.js @@ -0,0 +1,39 @@ +const db = require("../models"); + +module.exports = { + + findByUser : function(req, res){ + db.Watchlist + .find({user_email : req.params.email} ).sort("symbol") + .then(dbModel => res.json(dbModel)) + .catch(err => res.status(422).json(err)); + }, + + create: function(req,res){ + db.Watchlist + .find({user_email:req.body.email, symbol:req.body.symbol}) + .then( data =>{ + if (data.length >0){ + res.status(401).json(data); + } + else{ + db.Watchlist + .create({ + user_email: req.body.email, + symbol: req.body.symbol, + companyName: req.body.companyName + }) + .then(dbModel => res.json(dbModel)) + .catch(err => res.status(422).json(err)); + } + }) + .catch(err => res.status(422).json(err)); + }, + + delete: function(req,res){ + db.Watchlist + .deleteOne({user_email: req.params.email,symbol:req.params.symbol}) + .then(dbModel => res.json(dbModel)) + .catch(err => res.status(422).json(err)); + } +} \ No newline at end of file diff --git a/models/watchlist.js b/models/watchlist.js index 748ef0b..eab4d17 100644 --- a/models/watchlist.js +++ b/models/watchlist.js @@ -3,7 +3,8 @@ const Schema = mongoose.Schema; const watchlistSchema = new Schema({ user_email: {type: String, required:true}, - symbol: {type: String, required:true} + symbol: {type: String, required:true}, + companyName: {type: String, required: false} //date: { type: Date, default: Date.now } }); diff --git a/routes/api/index.js b/routes/api/index.js index db1d1df..5a819aa 100644 --- a/routes/api/index.js +++ b/routes/api/index.js @@ -1,5 +1,4 @@ const router = require("express").Router(); -const bookRoutes = require("./books"); // AlpahVantage API - Stock markek Data const alphaVantage = require("./alphaVantage"); @@ -18,9 +17,11 @@ const userPageRoutes = require("./userPage"); //symbols from db const symbolNameRoutes = require("./symbolName"); +// watchlist from db +const watchlistRoutes = require("./watchlists"); + // routes router.use("/alphaVantage", alphaVantage); -router.use("/books", bookRoutes); router.use("/stocks",stockRoutes); // router.use("/user/",userRoutes); //router.use("/user/",userRoutes); @@ -28,5 +29,6 @@ router.use("/userPage",userPageRoutes); router.use("/user",userRoutes); router.use("/finnhub", finnhub); router.use("/symbols", symbolNameRoutes); +router.use("/watchlist",watchlistRoutes); module.exports = router; diff --git a/routes/api/watchlists.js b/routes/api/watchlists.js new file mode 100644 index 0000000..f73ce18 --- /dev/null +++ b/routes/api/watchlists.js @@ -0,0 +1,18 @@ +const router = require("express").Router(); +const watchlistController = require("../../controllers/watchlistController"); + +// Matches with "/api/stocks" + + +/// all stocks person have +router.route("/all/:email") +.get(watchlistController.findByUser); + +/// add new one +router.route("/new") +.post(watchlistController.create); + +router.route("/delete/:email/:symbol") +.delete(watchlistController.delete); + +module.exports = router; \ No newline at end of file diff --git a/scripts/agg_exx.js b/scripts/agg_exx.js index 3d7ac36..7c52238 100644 --- a/scripts/agg_exx.js +++ b/scripts/agg_exx.js @@ -26,8 +26,8 @@ db.Stock process.exit(0); }) */ -db.Stock.find({user_email: "polina@test.com"}). -then(data =>{ +db.Stock.find({user_email: "polina@test.com"}).sort("symbol") +.then(data =>{ console.log(data); process.exit(0); })