diff --git a/DB/playlists.sql b/DB/playlists.sql new file mode 100644 index 0000000..9c39e60 --- /dev/null +++ b/DB/playlists.sql @@ -0,0 +1,17 @@ +create table playlists +( + id text not null primary key, + "spotifyId" text not null, + name text not null, + cover text, + enabled boolean default false not null +); + +CREATE UNIQUE INDEX enabled_playlist_unique_name ON playlists (name) WHERE enabled = true; + +create table categories +( + name text not null, + "playlistId" text not null references playlists, + primary key (name, "playlistId") +); diff --git a/server/api/v1/playlist/[uid].get.ts b/server/api/v1/playlist/[uid].get.ts index b9be938..ace864b 100644 --- a/server/api/v1/playlist/[uid].get.ts +++ b/server/api/v1/playlist/[uid].get.ts @@ -25,12 +25,16 @@ export default defineEventHandler(async (event) => { } const client = serverSupabaseServiceRole(event); - const {data, error} = await client.from('playlists').select('*').eq('id', playlistId).single(); + const {data, error} = await client.from('playlists').select('*, categories (name)').eq('id', playlistId).single(); if (error) { setResponseStatus(event, 500); return {error: error.message}; + } else { + const transformedData = { + ...data, + categories: data.categories.map(category => category.name) + }; + return transformedData; } - - return data; }) \ No newline at end of file diff --git a/server/api/v1/playlist/index.get.ts b/server/api/v1/playlist/index.get.ts index 1a23cb1..c1e3d9b 100644 --- a/server/api/v1/playlist/index.get.ts +++ b/server/api/v1/playlist/index.get.ts @@ -16,12 +16,20 @@ export default defineEventHandler(async (event) => { } const client = serverSupabaseServiceRole(event); - const {data, error} = await client.from('playlists').select(); + const {data, error} = await client.from('playlists') + .select(` + *, + categories (name) + `); if (error) { setResponseStatus(event, 500); return {error: error.message}; + } else { + const transformedData = data.map(playlist => ({ + ...playlist, + categories: playlist.categories.map(category => category.name), + })); + return transformedData; } - - return data; }) \ No newline at end of file diff --git a/server/api/v1/playlist/index.post.ts b/server/api/v1/playlist/index.post.ts index 7fa2412..7131877 100644 --- a/server/api/v1/playlist/index.post.ts +++ b/server/api/v1/playlist/index.post.ts @@ -38,18 +38,33 @@ export default defineEventHandler(async (event) => { cover: coverUrl } - const client = serverSupabaseServiceRole(event) - const {data, error} = await client.from('playlists').insert(playlistInsert as never).select().single(); //todo: fix type error! + const categoriesInsert = result.data.categories.map((category) => ({ + playlistId: result.data.id, + name: category, + })); - if (error) { + const client = serverSupabaseServiceRole(event); + const {data: playlistData, error: playlistError} = await client.from('playlists').insert(playlistInsert as never).select().single(); //todo: fix type error! + + + if (playlistError) { setResponseStatus(event, 400); - if (error.code === UNIQUE_VIOLATION) + if (playlistError.code === UNIQUE_VIOLATION) return {error: 'Playlist with this ID already exists'}; setResponseStatus(event, 500); - return {error: error.message}; + return {error: playlistError.message}; + } + + const { data: categoriesData, error: categoriesError } = await client + .from('categories') + .insert(categoriesInsert as never); + + if (categoriesError) { + setResponseStatus(event, 500); + return { error: `Error inserting categories: ${categoriesError.message}` }; } setResponseStatus(event, 201); - return data; + return { playlist: playlistData, categories: categoriesData }; }) \ No newline at end of file diff --git a/server/utils/spotify.ts b/server/utils/spotify.ts index 3639c9f..1f07a71 100644 --- a/server/utils/spotify.ts +++ b/server/utils/spotify.ts @@ -38,9 +38,10 @@ export async function getSpotifyToken() { export async function getPlaylistCover(token: string | null, playlistId: string): Promise { if (!token) return undefined; - const res = await fetch(`https://api.spotify.com/v1/playlists/${playlistId}?fields=images`, { + const res = await fetch(`https://api.spotify.com/v1/playlists/${playlistId}/images`, { headers: {'Authorization': `Bearer ${token}`} }) const data = await res.json() - return data.images[0].url; + // console.log(data) + return data[0].url; } \ No newline at end of file