From 8a9777b19468b9b555b8ffaad6ee543ee0b4eabd Mon Sep 17 00:00:00 2001 From: w3cj Date: Fri, 29 Mar 2024 09:50:37 -0600 Subject: [PATCH] feat: playlist order --- prisma/schema.prisma | 11 +++++ src/lib/videos/playlistOrderBy.ts | 20 +++++++++ .../videos/[playlist_id]/+page.server.ts | 32 ++++++++++++- .../admin/videos/[playlist_id]/+page.svelte | 24 +++++++++- src/routes/(site)/videos/+page.server.ts | 45 ++++++++++++------- .../(site)/videos/[p_slug]/+page.server.ts | 29 ++++++++---- 6 files changed, 135 insertions(+), 26 deletions(-) create mode 100644 src/lib/videos/playlistOrderBy.ts diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 2bc2e0552..fcd2d4e65 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -239,6 +239,16 @@ model Video { shows ShowVideo[] } +enum PlaylistOrder { + // Use playlist order as it is on YouTube + YouTubeAsc + // Reverse playlist order as it is on YouTube + YouTubeDesc + // Order playlist by video publish date accordingly + VideoPublishedAtAsc + VideoPublishedAtDesc +} + model Playlist { id String @id @default(uuid()) title String @@ -246,6 +256,7 @@ model Playlist { created_at DateTime @default(now()) slug String @unique unlisted Boolean? @default(false) + order PlaylistOrder @default(YouTubeAsc) videos PlaylistOnVideo[] } diff --git a/src/lib/videos/playlistOrderBy.ts b/src/lib/videos/playlistOrderBy.ts new file mode 100644 index 000000000..a9b970877 --- /dev/null +++ b/src/lib/videos/playlistOrderBy.ts @@ -0,0 +1,20 @@ +import { PlaylistOrder } from '@prisma/client'; + +export default { + [PlaylistOrder.YouTubeAsc]: { + order: 'asc' + }, + [PlaylistOrder.YouTubeDesc]: { + order: 'desc' + }, + [PlaylistOrder.VideoPublishedAtDesc]: { + video: { + published_at: 'desc' + } + }, + [PlaylistOrder.VideoPublishedAtAsc]: { + video: { + published_at: 'asc' + } + } +} as const; diff --git a/src/routes/(site)/admin/videos/[playlist_id]/+page.server.ts b/src/routes/(site)/admin/videos/[playlist_id]/+page.server.ts index c5395bd68..e35c27dd4 100644 --- a/src/routes/(site)/admin/videos/[playlist_id]/+page.server.ts +++ b/src/routes/(site)/admin/videos/[playlist_id]/+page.server.ts @@ -1,3 +1,6 @@ +import { PlaylistOrder } from '@prisma/client'; +import type { Actions } from '@sveltejs/kit'; + export const load = async ({ params, locals }) => { const { playlist_id } = params; @@ -29,9 +32,12 @@ export const load = async ({ params, locals }) => { const videos = playlist.videos.map((item) => item.video); console.log('videos', videos); + const playlistOrderTypes = Object.keys(PlaylistOrder); + return { playlist, - videos + videos, + playlistOrderTypes }; } catch (error) { console.error('Error fetching playlist videos:', error); @@ -41,3 +47,27 @@ export const load = async ({ params, locals }) => { }; } }; + +export const actions: Actions = { + updatePlaylist: async function updatePlaylist({ locals, params }) { + const { playlist_id } = params; + const { playlistOrder } = locals.form_data; + if (!playlist_id || !playlistOrder) { + return { + error: 'Missing data', + status: 500 + }; + } + await locals.prisma.playlist.update({ + where: { + id: playlist_id + }, + data: { + order: locals.form_data.playlistOrder as PlaylistOrder + } + }); + return { + message: 'Updated' + }; + } +}; diff --git a/src/routes/(site)/admin/videos/[playlist_id]/+page.svelte b/src/routes/(site)/admin/videos/[playlist_id]/+page.svelte index a1154d9c6..2d23d6dcc 100644 --- a/src/routes/(site)/admin/videos/[playlist_id]/+page.svelte +++ b/src/routes/(site)/admin/videos/[playlist_id]/+page.svelte @@ -1,10 +1,12 @@