From 3d511f30c5e151d889922d6302b107f8320ba8f5 Mon Sep 17 00:00:00 2001 From: synan798 Date: Sat, 30 Nov 2024 12:37:31 +0100 Subject: [PATCH] added categories to post request (getPlaylistCover doesn't work) --- server/api/v1/playlist/index.post.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/server/api/v1/playlist/index.post.ts b/server/api/v1/playlist/index.post.ts index 7fa2412..d53e039 100644 --- a/server/api/v1/playlist/index.post.ts +++ b/server/api/v1/playlist/index.post.ts @@ -21,6 +21,7 @@ const schema = z.object({ * @returns {Object} - Created playlist */ export default defineEventHandler(async (event) => { + // fix coverUrl and delete test.vue const result = await readValidatedBody(event, body => schema.safeParse(body)) if (!result.success) { @@ -36,11 +37,18 @@ export default defineEventHandler(async (event) => { name: result.data.name, spotifyId: result.data.spotifyId, cover: coverUrl + //cover: "https://i.scdn.co/image/ab67706f000000024d183558628c25f8cb314eea" } - const client = serverSupabaseServiceRole(event) + const categoriesInsert = result.data.categories.map((category) => ({ + playlistId: result.data.id, + name: category, + })); + + const client = serverSupabaseServiceRole(event); const {data, error} = await client.from('playlists').insert(playlistInsert as never).select().single(); //todo: fix type error! + if (error) { setResponseStatus(event, 400); if (error.code === UNIQUE_VIOLATION) @@ -49,6 +57,15 @@ export default defineEventHandler(async (event) => { return {error: error.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;