Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feat/api' into feat/friend-system
Browse files Browse the repository at this point in the history
  • Loading branch information
Likqez committed Dec 4, 2024
2 parents 8e4cff3 + 90aaf2d commit 84453e6
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 14 deletions.
17 changes: 17 additions & 0 deletions DB/playlists.sql
Original file line number Diff line number Diff line change
@@ -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")
);
10 changes: 7 additions & 3 deletions server/api/v1/playlist/[uid].get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})
14 changes: 11 additions & 3 deletions server/api/v1/playlist/index.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})
27 changes: 21 additions & 6 deletions server/api/v1/playlist/index.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
})
5 changes: 3 additions & 2 deletions server/utils/spotify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ export async function getSpotifyToken() {
export async function getPlaylistCover(token: string | null, playlistId: string): Promise<string | undefined> {
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;
}

0 comments on commit 84453e6

Please sign in to comment.