From d7879a654a153b894979c7c57749d37f3506cc39 Mon Sep 17 00:00:00 2001 From: Artem Anufrij Date: Tue, 19 Sep 2023 01:58:22 +0200 Subject: [PATCH 1/2] start to work on new functions --- config.json | 4 ++-- router/user.js | 27 ++++++++++++++++++++++----- services/database/index.js | 3 +++ services/database/progress.js | 27 +++++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 7 deletions(-) create mode 100644 services/database/progress.js diff --git a/config.json b/config.json index 15616a3..ff2d43a 100644 --- a/config.json +++ b/config.json @@ -10,12 +10,12 @@ "http://localhost" ], "database": { - "host": "database", + "host": "localhost", "port": 27017, "name": "webplay" }, "redis": { - "host": "redis", + "host": "127.0.0.1", "port": 6379 }, "album_cover_files": [ diff --git a/router/user.js b/router/user.js index 5ddd66c..37ebb7f 100644 --- a/router/user.js +++ b/router/user.js @@ -137,12 +137,22 @@ router }); router - .route("/history") - .get(passport.authenticate("jwt", { session: false }), (req, res) => { - database.historyList(req.user._id, result => { - res.json(result).end(); - }); + .route("/progress") + .post(passport.authenticate("jwt", { session: false }), (req, res) => { + let item = req.body; + item.userId = req.user._id; + database.progress.update(item); + res.status(200).end(); }) + .delete(passport.authenticate("jwt", { session: false }), (req, res) => { + let item = req.body; + item.userId = req.user._id; + database.progress.delete(item); + res.status(200).end(); + }); + +router + .route("/history") .post(passport.authenticate("jwt", { session: false }), (req, res) => { let item = req.body; item.userId = req.user._id; @@ -159,6 +169,13 @@ router }); }); +router.route("/progress/:parent") + .get(passport.authenticate("jwt", { session: false }), (req, res) => { + database.progress.get(req.params.parent, result => { + res.json(result).end(); + }); + }) + router .route("/settings") .put(passport.authenticate("jwt", { session: false }), (req, res) => { diff --git a/services/database/index.js b/services/database/index.js index 72ed6fe..700e9c5 100644 --- a/services/database/index.js +++ b/services/database/index.js @@ -62,6 +62,9 @@ exports.artists = artists; const boxes = require("./boxes"); exports.boxes = boxes; +const progress = require("./progress"); +exports.progress = progress; + const videos = require("./videos"); exports.videos = videos; diff --git a/services/database/progress.js b/services/database/progress.js new file mode 100644 index 0000000..704e7c3 --- /dev/null +++ b/services/database/progress.js @@ -0,0 +1,27 @@ +const { ObjectId } = require('mongodb'); +const connector = require("./CONNECTOR"); +var dbo; +connector.connect().then((ret) => { + dbo = ret; +}); + +exports.get = function (parentId, callback) { + dbo.collection("progress") + .findOne({ parentId: parentId }, (err, result) => { + if (err) throw err; + callback(result); + }); +} +exports.update = function (item) { + dbo.collection("progress").deleteMany({ userId: ObjectId(item.userId), parentId: item.parentId }, () => { + dbo.collection("progress").insertOne(item, (err) => { + if (err) throw err; + }); + }); +}; + +exports.delete = function (item) { + dbo.collection("progress").deleteMany({ userId: ObjectId(item.userId), id: item.id }, (err) => { + if (err) throw err; + }); +} \ No newline at end of file From 85367e844d20b27a239ba150fe400561118f7c05 Mon Sep 17 00:00:00 2001 From: Artem Anufrij Date: Tue, 19 Sep 2023 15:00:25 +0200 Subject: [PATCH 2/2] api for deleting progress --- router/user.js | 35 +++++++++++++++++++---------------- services/database/progress.js | 2 +- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/router/user.js b/router/user.js index 37ebb7f..fb266f7 100644 --- a/router/user.js +++ b/router/user.js @@ -136,21 +136,6 @@ router }); }); -router - .route("/progress") - .post(passport.authenticate("jwt", { session: false }), (req, res) => { - let item = req.body; - item.userId = req.user._id; - database.progress.update(item); - res.status(200).end(); - }) - .delete(passport.authenticate("jwt", { session: false }), (req, res) => { - let item = req.body; - item.userId = req.user._id; - database.progress.delete(item); - res.status(200).end(); - }); - router .route("/history") .post(passport.authenticate("jwt", { session: false }), (req, res) => { @@ -169,12 +154,30 @@ router }); }); -router.route("/progress/:parent") +router + .route("/progress") + .post(passport.authenticate("jwt", { session: false }), (req, res) => { + let item = req.body; + item.userId = req.user._id; + database.progress.update(item); + res.status(200).end(); + }); + +router + .route("/progress/:parent") .get(passport.authenticate("jwt", { session: false }), (req, res) => { database.progress.get(req.params.parent, result => { res.json(result).end(); }); }) + .delete(passport.authenticate("jwt", { session: false }), (req, res) => { + let item = { + parentId: req.params.parent, + userId: req.user._id + } + database.progress.delete(item); + res.status(200).end(); + }); router .route("/settings") diff --git a/services/database/progress.js b/services/database/progress.js index 704e7c3..27ce169 100644 --- a/services/database/progress.js +++ b/services/database/progress.js @@ -21,7 +21,7 @@ exports.update = function (item) { }; exports.delete = function (item) { - dbo.collection("progress").deleteMany({ userId: ObjectId(item.userId), id: item.id }, (err) => { + dbo.collection("progress").deleteMany({ userId: ObjectId(item.userId), parentId: item.parentId }, (err) => { if (err) throw err; }); } \ No newline at end of file