|
| 1 | +import { handlePostgrestQuery, supabase } from '$lib/server/supabase'; |
| 2 | +import { json } from '@sveltejs/kit'; |
| 3 | +import { TABLES } from '../../../../../shared/config'; |
| 4 | + |
| 5 | +export async function GET({ params }) { |
| 6 | + return handlePostgrestQuery( |
| 7 | + async () => supabase.from(TABLES.collectionsRich).select('*').eq('id', params.slug).single(), |
| 8 | + { errorMessage: 'Failed to fetch collection' } |
| 9 | + ); |
| 10 | +} |
| 11 | + |
| 12 | +export async function PATCH({ request, params }) { |
| 13 | + const collectionId = params.slug; |
| 14 | + const { releaseId, addOrRemove } = await request.json(); |
| 15 | + |
| 16 | + if (!releaseId) { |
| 17 | + return json({ error: 'Missing release ID' }, { status: 400 }); |
| 18 | + } |
| 19 | + |
| 20 | + if (addOrRemove === 'remove') { |
| 21 | + const { error } = await supabase |
| 22 | + .from(TABLES.collectionReleases) |
| 23 | + .delete() |
| 24 | + .eq('collection_id', collectionId) |
| 25 | + .eq('release_id', releaseId); |
| 26 | + |
| 27 | + if (error) { |
| 28 | + console.error('Error removing release from collection:', error); |
| 29 | + return json({ error: 'Failed to remove release from collection' }, { status: 500 }); |
| 30 | + } |
| 31 | + |
| 32 | + return json({ success: true }); |
| 33 | + } |
| 34 | + |
| 35 | + const { error } = await supabase |
| 36 | + .from(TABLES.collectionReleases) |
| 37 | + .insert({ collection_id: collectionId, release_id: releaseId }); |
| 38 | + |
| 39 | + if (error) { |
| 40 | + console.error('Error adding release to collection:', error); |
| 41 | + return json({ error: 'Failed to add release to collection' }, { status: 500 }); |
| 42 | + } |
| 43 | + |
| 44 | + return json({ success: true }); |
| 45 | +} |
| 46 | + |
| 47 | +export async function DELETE({ request, params }) { |
| 48 | + const collectionId = params.slug; |
| 49 | + const { userId } = await request.json(); |
| 50 | + |
| 51 | + const { error } = await supabase |
| 52 | + .from(TABLES.collections) |
| 53 | + .delete() |
| 54 | + .eq('id', collectionId) |
| 55 | + .eq('user_id', userId); |
| 56 | + |
| 57 | + if (error) { |
| 58 | + console.error('Error deleting collection:', error); |
| 59 | + return json({ error: 'Failed to delete collection' }, { status: 500 }); |
| 60 | + } |
| 61 | + |
| 62 | + return json({ success: true }); |
| 63 | +} |
0 commit comments