File tree 6 files changed +135
-26
lines changed
admin/videos/[playlist_id]
6 files changed +135
-26
lines changed Original file line number Diff line number Diff line change @@ -239,13 +239,24 @@ model Video {
239
239
shows ShowVideo []
240
240
}
241
241
242
+ enum PlaylistOrder {
243
+ // Use playlist order as it is on YouTube
244
+ YouTubeAsc
245
+ // Reverse playlist order as it is on YouTube
246
+ YouTubeDesc
247
+ // Order playlist by video publish date accordingly
248
+ VideoPublishedAtAsc
249
+ VideoPublishedAtDesc
250
+ }
251
+
242
252
model Playlist {
243
253
id String @id @default (uuid () )
244
254
title String
245
255
description String ? @db.Text
246
256
created_at DateTime @default (now () )
247
257
slug String @unique
248
258
unlisted Boolean ? @default (false )
259
+ order PlaylistOrder @default (YouTubeAsc )
249
260
videos PlaylistOnVideo []
250
261
}
251
262
Original file line number Diff line number Diff line change
1
+ import { PlaylistOrder } from '@prisma/client' ;
2
+
3
+ export default {
4
+ [ PlaylistOrder . YouTubeAsc ] : {
5
+ order : 'asc'
6
+ } ,
7
+ [ PlaylistOrder . YouTubeDesc ] : {
8
+ order : 'desc'
9
+ } ,
10
+ [ PlaylistOrder . VideoPublishedAtDesc ] : {
11
+ video : {
12
+ published_at : 'desc'
13
+ }
14
+ } ,
15
+ [ PlaylistOrder . VideoPublishedAtAsc ] : {
16
+ video : {
17
+ published_at : 'asc'
18
+ }
19
+ }
20
+ } as const ;
Original file line number Diff line number Diff line change
1
+ import { PlaylistOrder } from '@prisma/client' ;
2
+ import type { Actions } from '@sveltejs/kit' ;
3
+
1
4
export const load = async ( { params, locals } ) => {
2
5
const { playlist_id } = params ;
3
6
@@ -29,9 +32,12 @@ export const load = async ({ params, locals }) => {
29
32
const videos = playlist . videos . map ( ( item ) => item . video ) ;
30
33
console . log ( 'videos' , videos ) ;
31
34
35
+ const playlistOrderTypes = Object . keys ( PlaylistOrder ) ;
36
+
32
37
return {
33
38
playlist,
34
- videos
39
+ videos,
40
+ playlistOrderTypes
35
41
} ;
36
42
} catch ( error ) {
37
43
console . error ( 'Error fetching playlist videos:' , error ) ;
@@ -41,3 +47,27 @@ export const load = async ({ params, locals }) => {
41
47
} ;
42
48
}
43
49
} ;
50
+
51
+ export const actions : Actions = {
52
+ updatePlaylist : async function updatePlaylist ( { locals, params } ) {
53
+ const { playlist_id } = params ;
54
+ const { playlistOrder } = locals . form_data ;
55
+ if ( ! playlist_id || ! playlistOrder ) {
56
+ return {
57
+ error : 'Missing data' ,
58
+ status : 500
59
+ } ;
60
+ }
61
+ await locals . prisma . playlist . update ( {
62
+ where : {
63
+ id : playlist_id
64
+ } ,
65
+ data : {
66
+ order : locals . form_data . playlistOrder as PlaylistOrder
67
+ }
68
+ } ) ;
69
+ return {
70
+ message : 'Updated'
71
+ } ;
72
+ }
73
+ } ;
Original file line number Diff line number Diff line change 1
1
<!-- src/routes/playlists/[playlist_id]/+page.svelte -->
2
2
<script lang =" ts" >
3
3
import AdminSearch from ' $/lib/AdminSearch.svelte' ;
4
+ import { enhance } from ' $app/forms' ;
5
+ import { form_action } from ' $lib/form_action' ;
4
6
import type { PageData } from ' ./$types' ;
5
7
6
8
export let data: PageData ;
7
- const { playlist, videos } = data ;
9
+ const { playlist, videos, playlistOrderTypes } = data ;
8
10
9
11
let search_text = ' ' ;
10
12
13
15
14
16
<h1 >{playlist ?.title }</h1 >
15
17
18
+ {#if playlistOrderTypes }
19
+ <form use:enhance ={form_action ({ message: ' Update Playlist Order' })} action =" ?/updatePlaylist" method =" POST" >
20
+ <label >
21
+ Order Playlist
22
+ <select name ="playlistOrder" value ={playlist ?.order }>
23
+ {#each playlistOrderTypes as orderType }
24
+ <option >{orderType }</option >
25
+ {/each }
26
+ </select >
27
+ </label >
28
+ <button >Update</button >
29
+ </form >
30
+ {/if }
31
+
16
32
<div >
17
33
<AdminSearch bind:text ={search_text } />
18
34
42
58
</table >
43
59
</div >
44
60
</div >
61
+
62
+ <style >
63
+ form {
64
+ margin : 1rem ;
65
+ }
66
+ </style >
Original file line number Diff line number Diff line change
1
+ import playListOrderBy from '$lib/videos/playlistOrderBy' ;
2
+
1
3
export const load = async function ( { locals } ) {
2
- const playlists = await locals . prisma . playlist . findMany ( {
4
+ const playlistMeta = await locals . prisma . playlist . findMany ( {
3
5
orderBy : {
4
6
created_at : 'desc'
5
7
} ,
6
- include : {
7
- videos : {
8
- take : 3 ,
9
- orderBy : {
10
- order : 'asc'
8
+ select : {
9
+ id : true ,
10
+ order : true
11
+ }
12
+ } ) ;
13
+ const playlists = await Promise . all (
14
+ playlistMeta . map ( async ( playlist ) => {
15
+ return locals . prisma . playlist . findFirst ( {
16
+ where : {
17
+ id : playlist . id
11
18
} ,
12
19
include : {
13
- video : true
20
+ videos : {
21
+ take : 3 ,
22
+ orderBy : playListOrderBy [ playlist . order ] ,
23
+ include : {
24
+ video : true
25
+ }
26
+ } ,
27
+ _count : {
28
+ select : {
29
+ videos : true
30
+ }
31
+ }
14
32
}
15
- } ,
16
- _count : {
17
- select : {
18
- videos : true
19
- }
20
- }
21
- }
22
- } ) ;
33
+ } ) ;
34
+ } )
35
+ ) ;
23
36
24
37
const playlists_with_item_count = playlists . map ( ( playlist ) => ( {
25
38
...playlist ,
26
- item_count : playlist . _count . videos
39
+ item_count : playlist ! . _count . videos
27
40
} ) ) ;
28
41
return {
29
42
playlists : playlists_with_item_count
Original file line number Diff line number Diff line change
1
+ import playlistOrderBy from '$/lib/videos/playlistOrderBy.js' ;
2
+
1
3
export const load = async function ( { locals, params } ) {
2
4
const { p_slug } = params ;
3
- const playlist = await locals . prisma . playlist . findUnique ( {
5
+ const playlistMeta = await locals . prisma . playlist . findUnique ( {
4
6
where : { slug : p_slug } ,
5
- include : {
6
- videos : {
7
- include : {
8
- video : true
9
- }
10
- }
7
+ select : {
8
+ id : true ,
9
+ order : true
11
10
}
12
11
} ) ;
13
12
14
- if ( ! playlist ) {
13
+ if ( ! playlistMeta ) {
15
14
// Playlist not found
16
15
return {
17
16
status : 404 ,
18
17
error : 'Playlist not found'
19
18
} ;
20
19
}
21
20
21
+ const playlist = await locals . prisma . playlist . findUnique ( {
22
+ where : {
23
+ id : playlistMeta . id
24
+ } ,
25
+ include : {
26
+ videos : {
27
+ include : {
28
+ video : true
29
+ } ,
30
+ orderBy : playlistOrderBy [ playlistMeta . order ]
31
+ }
32
+ }
33
+ } ) ;
34
+
22
35
return {
23
36
playlist
24
37
} ;
You can’t perform that action at this time.
0 commit comments