mirror of
https://github.com/jcreek/LivingDexTracker.git
synced 2026-07-13 02:53:45 +00:00
feat(*): Migrate from MongoDB to unified Supabase PostgreSQL architecture
This commit is contained in:
@@ -1,32 +1,24 @@
|
||||
import { json, error } from '@sveltejs/kit';
|
||||
import { dbConnect, dbDisconnect } from '$lib/utils/db';
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { type CatchRecord } from '$lib/models/CatchRecord';
|
||||
import CatchRecordRepository from '$lib/repositories/CatchRecordRepository';
|
||||
import { requireAuth } from '$lib/utils/auth';
|
||||
import type { RequestEvent } from '@sveltejs/kit';
|
||||
|
||||
export const GET = async (event: RequestEvent) => {
|
||||
let catchData = null as CatchRecord[] | null;
|
||||
|
||||
try {
|
||||
const userId = await requireAuth(event);
|
||||
await dbConnect();
|
||||
const repo = new CatchRecordRepository();
|
||||
catchData = await repo.findByUserId(userId);
|
||||
const repo = new CatchRecordRepository(event.locals.supabase, userId);
|
||||
const catchData = await repo.findByUserId(userId);
|
||||
// order by pokedexEntryId property, ascending
|
||||
catchData = catchData.sort((a, b) => a.pokedexEntryId - b.pokedexEntryId);
|
||||
const sortedData = catchData.sort((a, b) => Number(a.pokedexEntryId) - Number(b.pokedexEntryId));
|
||||
return json(sortedData);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
if (error.status) {
|
||||
// Re-throw SvelteKit errors (like 401)
|
||||
throw error;
|
||||
}
|
||||
return json({ error: 'Internal Server Error' }, { status: 500 });
|
||||
} finally {
|
||||
dbDisconnect();
|
||||
}
|
||||
|
||||
return json(catchData);
|
||||
};
|
||||
|
||||
export const PUT = async (event: RequestEvent) => {
|
||||
@@ -37,8 +29,7 @@ export const PUT = async (event: RequestEvent) => {
|
||||
// Ensure the userId is set to the authenticated user
|
||||
data.userId = userId;
|
||||
|
||||
await dbConnect();
|
||||
const repo = new CatchRecordRepository();
|
||||
const repo = new CatchRecordRepository(event.locals.supabase, userId);
|
||||
const upsertedRecord = await repo.upsert(data);
|
||||
return json(upsertedRecord);
|
||||
} catch (err) {
|
||||
@@ -47,8 +38,6 @@ export const PUT = async (event: RequestEvent) => {
|
||||
throw err;
|
||||
}
|
||||
return json({ error: 'Internal Server Error' }, { status: 500 });
|
||||
} finally {
|
||||
await dbDisconnect();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -56,8 +45,7 @@ export const POST = async (event: RequestEvent) => {
|
||||
try {
|
||||
const userId = await requireAuth(event);
|
||||
const records: Partial<CatchRecord>[] = await event.request.json();
|
||||
await dbConnect();
|
||||
const repo = new CatchRecordRepository();
|
||||
const repo = new CatchRecordRepository(event.locals.supabase, userId);
|
||||
|
||||
const insertedRecords = [];
|
||||
for (const record of records) {
|
||||
@@ -74,7 +62,5 @@ export const POST = async (event: RequestEvent) => {
|
||||
throw err;
|
||||
}
|
||||
return json({ error: 'Internal Server Error' }, { status: 500 });
|
||||
} finally {
|
||||
await dbDisconnect();
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user