From e6614d0805bb6cbd4862c8a3e523e457833dabe5 Mon Sep 17 00:00:00 2001 From: Artem Anufrij Date: Wed, 27 Sep 2023 16:52:28 +0200 Subject: [PATCH 1/4] add get api for random album covers --- config.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config.json b/config.json index 15616a3..2f2b3bf 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": "localhost", "port": 6379 }, "album_cover_files": [ -- 2.45.2 From 97fcdcce5d1a42c5179caf62c566d63c521968c9 Mon Sep 17 00:00:00 2001 From: Artem Anufrij Date: Wed, 27 Sep 2023 16:52:37 +0200 Subject: [PATCH 2/4] add get api for random album covers --- router/album.js | 6 ++++++ services/database/albums.js | 27 ++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/router/album.js b/router/album.js index 4d97485..d46f930 100644 --- a/router/album.js +++ b/router/album.js @@ -51,6 +51,12 @@ router.route("/filter/:term") res.json(result).status(200).end(); }); }) +router.route("/random/:count") + .get(passport.authenticate("jwt", { session: false }), (req, res) => { + database.albums.randomCovers(req.params.count, 64, (result) => { + res.json(result).status(200).end(); + }); + }) router.route("/:id") .get(checkGuest, (req, res) => { diff --git a/services/database/albums.js b/services/database/albums.js index 09c9e43..e8258cb 100644 --- a/services/database/albums.js +++ b/services/database/albums.js @@ -310,4 +310,29 @@ exports.empty = function (callback) { .toArray((err, result) => { callback(result.filter(f => !f.tracks || f.tracks.length == 0)); }); -}; \ No newline at end of file +}; + +exports.randomCovers = function (count, size, callback) { + dbo + .collection("albums") + .find({ "covers.cover64": { $exists: true } }) + .project({ "covers.cover64": true }) + .toArray((err, result) => { + let res = []; + if (result.length > count) { + let i = 0; + while (i < count) { + let rnd = randomNumber(0, result.length); + res.push(result[rnd]); + i++; + } + callback(res); + } else { + callback(result); + } + }) +} + +function randomNumber(min, max) { + return Math.floor(Math.random() * (max - min) + min); +} \ No newline at end of file -- 2.45.2 From cd0b9a7f552d7b7c7a7de41655d05076dbd45829 Mon Sep 17 00:00:00 2001 From: Artem Anufrij Date: Wed, 27 Sep 2023 21:43:52 +0200 Subject: [PATCH 3/4] code design --- config.json | 4 ++-- services/database/albums.js | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/config.json b/config.json index 2f2b3bf..15616a3 100644 --- a/config.json +++ b/config.json @@ -10,12 +10,12 @@ "http://localhost" ], "database": { - "host": "localhost", + "host": "database", "port": 27017, "name": "webplay" }, "redis": { - "host": "localhost", + "host": "redis", "port": 6379 }, "album_cover_files": [ diff --git a/services/database/albums.js b/services/database/albums.js index e8258cb..9f33e44 100644 --- a/services/database/albums.js +++ b/services/database/albums.js @@ -320,11 +320,9 @@ exports.randomCovers = function (count, size, callback) { .toArray((err, result) => { let res = []; if (result.length > count) { - let i = 0; - while (i < count) { + while (count-- > 0) { let rnd = randomNumber(0, result.length); res.push(result[rnd]); - i++; } callback(res); } else { -- 2.45.2 From 94a689344419d2c38698c243d6e977b79fbda808 Mon Sep 17 00:00:00 2001 From: Artem Anufrij Date: Wed, 27 Sep 2023 21:44:58 +0200 Subject: [PATCH 4/4] code style --- services/database/albums.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/database/albums.js b/services/database/albums.js index 9f33e44..9fe2da7 100644 --- a/services/database/albums.js +++ b/services/database/albums.js @@ -318,8 +318,8 @@ exports.randomCovers = function (count, size, callback) { .find({ "covers.cover64": { $exists: true } }) .project({ "covers.cover64": true }) .toArray((err, result) => { - let res = []; if (result.length > count) { + let res = []; while (count-- > 0) { let rnd = randomNumber(0, result.length); res.push(result[rnd]); -- 2.45.2