Browse Source

Added logic for pagination for playlist and official song searching in manage station modal

Kristian Vos 3 years ago
parent
commit
ebd272a5e4

+ 8 - 4
backend/logic/actions/playlists.js

@@ -224,9 +224,10 @@ export default {
 	 *
 	 * @param {object} session - the session object automatically added by the websocket
 	 * @param {string} query - the query
+	 * @param {string} query - the page
 	 * @param {Function} cb - gets called with the result
 	 */
-	searchCommunity: isLoginRequired(async function searchCommunity(session, query, cb) {
+	searchCommunity: isLoginRequired(async function searchCommunity(session, query, page, cb) {
 		async.waterfall(
 			[
 				next => {
@@ -241,7 +242,8 @@ export default {
 						includeGenre: true,
 						includeOwn: true,
 						includeSongs: true,
-						userId: session.userId
+						userId: session.userId,
+						page
 					})
 						.then(response => {
 							next(null, response.playlists);
@@ -268,9 +270,10 @@ export default {
 	 *
 	 * @param {object} session - the session object automatically added by the websocket
 	 * @param {string} query - the query
+	 * @param {string} query - the page
 	 * @param {Function} cb - gets called with the result
 	 */
-	searchOfficial: isAdminRequired(async function searchOfficial(session, query, cb) {
+	searchOfficial: isAdminRequired(async function searchOfficial(session, query, page, cb) {
 		async.waterfall(
 			[
 				next => {
@@ -283,7 +286,8 @@ export default {
 						query,
 						includeGenre: true,
 						includePrivate: true,
-						includeSongs: true
+						includeSongs: true,
+						page
 					})
 						.then(response => {
 							next(null, response.playlists);

+ 4 - 2
backend/logic/actions/songs.js

@@ -572,9 +572,10 @@ export default {
 	 *
 	 * @param {object} session - the session object automatically added by the websocket
 	 * @param {string} query - the query
+	 * @param {string} page - the page
 	 * @param {Function} cb - gets called with the result
 	 */
-	searchOfficial: isLoginRequired(async function searchOfficial(session, query, cb) {
+	searchOfficial: isLoginRequired(async function searchOfficial(session, query, page, cb) {
 		async.waterfall(
 			[
 				next => {
@@ -586,7 +587,8 @@ export default {
 					SongsModule.runJob("SEARCH", {
 						query,
 						includeVerified: true,
-						trimmed: true
+						trimmed: true,
+						page
 					})
 						.then(response => {
 							next(null, response.songs);

+ 7 - 1
backend/logic/playlists.js

@@ -991,6 +991,7 @@ class _PlaylistsModule extends CoreClass {
 	 * @param {string} payload.includeOwn - include own user playlists
 	 * @param {string} payload.userId - the user id of the person requesting
 	 * @param {string} payload.includeSongs - include songs
+	 * @param {string} payload.page - page (default 1)
 	 * @returns {Promise} - returns promise (reject, resolve)
 	 */
 	SEARCH(payload) {
@@ -1027,7 +1028,12 @@ class _PlaylistsModule extends CoreClass {
 					},
 
 					(filterArray, includeObject, next) => {
-						PlaylistsModule.playlistModel.find({ $or: filterArray }, includeObject, next);
+						const page = payload.page ? payload.page : 1;
+						PlaylistsModule.playlistModel
+							.find({ $or: filterArray }, includeObject)
+							.skip(15 * (page - 1))
+							.limit(15)
+							.exec(next);
 					},
 
 					(playlists, next) => {

+ 6 - 1
backend/logic/songs.js

@@ -416,6 +416,7 @@ class _SongsModule extends CoreClass {
 	 * @param {string} payload.includeUnverified - include unverified songs
 	 * @param {string} payload.includeVerified - include verified songs
 	 * @param {string} payload.trimmed - include trimmed songs
+	 * @param {string} payload.page - page (default 1)
 	 * @returns {Promise} - returns promise (reject, resolve)
 	 */
 	SEARCH(payload) {
@@ -444,7 +445,11 @@ class _SongsModule extends CoreClass {
 					},
 
 					(filterArray, next) => {
-						SongsModule.SongModel.find({ $or: filterArray }, next);
+						const page = payload.page ? payload.page : 1;
+						SongsModule.SongModel.find({ $or: filterArray })
+							.skip(15 * (page - 1))
+							.limit(15)
+							.exec(next);
 					},
 
 					(songs, next) => {

+ 4 - 4
frontend/src/components/modals/ManageStation/Tabs/Playlists.vue

@@ -93,11 +93,11 @@
 							type="text"
 							placeholder="Enter your playlist query here..."
 							v-model="search.query"
-							@keyup.enter="searchForPlaylists()"
+							@keyup.enter="searchForPlaylists(1)"
 						/>
 					</p>
 					<p class="control">
-						<a class="button is-info" @click="searchForPlaylists()"
+						<a class="button is-info" @click="searchForPlaylists(1)"
 							><i class="material-icons icon-with-button"
 								>search</i
 							>Search</a
@@ -503,14 +503,14 @@ export default {
 			});
 			return selected;
 		},
-		searchForPlaylists() {
+		searchForPlaylists(page) {
 			const { query } = this.search;
 			const action =
 				this.station.type === "official"
 					? "playlists.searchOfficial"
 					: "playlists.searchCommunity";
 
-			this.socket.dispatch(action, query, res => {
+			this.socket.dispatch(action, query, page, res => {
 				if (res.status === "success") {
 					this.search.results = res.data.playlists;
 				} else if (res.status === "error") {

+ 4 - 4
frontend/src/components/modals/ManageStation/Tabs/Search.vue

@@ -9,11 +9,11 @@
 						type="text"
 						placeholder="Enter your song query here..."
 						v-model="musareSearch.query"
-						@keyup.enter="searchForMusareSongs()"
+						@keyup.enter="searchForMusareSongs(1)"
 					/>
 				</p>
 				<p class="control">
-					<a class="button is-info" @click="searchForMusareSongs()"
+					<a class="button is-info" @click="searchForMusareSongs(1)"
 						><i class="material-icons icon-with-button">search</i
 						>Search</a
 					>
@@ -175,10 +175,10 @@ export default {
 				});
 			}
 		},
-		searchForMusareSongs() {
+		searchForMusareSongs(page) {
 			const { query } = this.musareSearch;
 
-			this.socket.dispatch("songs.searchOfficial", query, res => {
+			this.socket.dispatch("songs.searchOfficial", query, page, res => {
 				if (res.status === "success") {
 					this.musareSearch.results = res.data.songs;
 				} else if (res.status === "error") {