Browse Source

refactor: added songsCount and totalLength aggregation/columns for playlists table

Kristian Vos 3 years ago
parent
commit
98f8e0be16

+ 1 - 1
backend/logic/actions/punishments.js

@@ -283,7 +283,7 @@ export default {
 				(pipeline, next) => {
 					punishmentModel.aggregate(pipeline).exec((err, result) => {
 						// console.dir(err);
-						console.dir(result, { depth: 6 });
+						// console.dir(result, { depth: 6 });
 						if (err) return next(err);
 						if (result[0].count.length === 0) return next(null, 0, []);
 						const { count } = result[0].count[0];

+ 44 - 0
backend/logic/playlists.js

@@ -882,6 +882,50 @@ class _PlaylistsModule extends CoreClass {
 					// Creates pipeline array
 					next => next(null, []),
 
+					// If a filter or property exists for totalLength, add totalLength property to all documents
+					(pipeline, next) => {
+						const { queries, properties } = payload;
+
+						// Check if a filter with the totalLength property exists
+						const totalLengthFilterExists =
+							queries.map(query => query.filter.property).indexOf("totalLength") !== -1;
+						// Check if a property with the totalLength property exists
+						const totalLengthPropertyExists = properties.indexOf("totalLength") !== -1;
+						// If no such filter or property exists, skip this function
+						if (!totalLengthFilterExists && !totalLengthPropertyExists) return next(null, pipeline);
+
+						// Adds totalLength field which is the sum of all durations of all songs in the songs array
+						pipeline.push({
+							$addFields: {
+								totalLength: { $sum: "$songs.duration" }
+							}
+						});
+
+						return next(null, pipeline);
+					},
+
+					// If a filter or property exists for songsCount, add songsCount property to all documents
+					(pipeline, next) => {
+						const { queries, properties } = payload;
+
+						// Check if a filter with the songsCount property exists
+						const songsCountFilterExists =
+							queries.map(query => query.filter.property).indexOf("songsCount") !== -1;
+						// Check if a property with the songsCount property exists
+						const songsCountPropertyExists = properties.indexOf("songsCount") !== -1;
+						// If no such filter or property exists, skip this function
+						if (!songsCountFilterExists && !songsCountPropertyExists) return next(null, pipeline);
+
+						// Adds songsCount field which is the length of the songs array
+						pipeline.push({
+							$addFields: {
+								songsCount: { $size: "$songs" }
+							}
+						});
+
+						return next(null, pipeline);
+					},
+
 					// If a filter exists for createdBy, add createdByUsername property to all documents
 					(pipeline, next) => {
 						const { queries } = payload;

+ 12 - 19
frontend/src/pages/Admin/tabs/Playlists.vue

@@ -47,17 +47,14 @@
 					}}</span>
 				</template>
 				<template #column-songsCount="slotProps">
-					<span :title="slotProps.item.songs.length">{{
-						slotProps.item.songs.length
+					<span :title="slotProps.item.songsCount">{{
+						slotProps.item.songsCount
 					}}</span>
 				</template>
 				<template #column-totalLength="slotProps">
-					<span
-						:title="totalLengthForPlaylist(slotProps.item.songs)"
-						>{{
-							totalLengthForPlaylist(slotProps.item.songs)
-						}}</span
-					>
+					<span :title="formatTimeLong(slotProps.item.totalLength)">{{
+						formatTimeLong(slotProps.item.totalLength)
+					}}</span>
 				</template>
 				<template #column-createdBy="slotProps">
 					<span v-if="slotProps.item.createdBy === 'Musare'"
@@ -162,16 +159,16 @@ export default {
 				{
 					name: "songsCount",
 					displayName: "Songs #",
-					properties: ["songs"],
-					sortable: false,
-					minWidth: 80,
-					defaultWidth: 80
+					properties: ["songsCount"],
+					sortProperty: "songsCount",
+					minWidth: 100,
+					defaultWidth: 100
 				},
 				{
 					name: "totalLength",
 					displayName: "Total Length",
-					properties: ["songs"],
-					sortable: false,
+					properties: ["totalLength"],
+					sortProperty: "totalLength",
 					minWidth: 250,
 					defaultWidth: 250
 				},
@@ -342,11 +339,7 @@ export default {
 			const minute = `${date.getMinutes()}`.padStart(2, 0);
 			return `${year}-${month}-${day} ${hour}:${minute}`;
 		},
-		totalLengthForPlaylist(songs) {
-			let length = 0;
-			songs.forEach(song => {
-				length += song.duration;
-			});
+		formatTimeLong(length) {
 			return this.utils.formatTimeLong(length);
 		},
 		...mapActions("modalVisibility", ["openModal"]),