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();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { dbConnect, dbDisconnect } from '$lib/utils/db';
|
||||
import CombinedDataRepository from '$lib/repositories/CombinedDataRepository';
|
||||
import { requireAuth } from '$lib/utils/auth';
|
||||
import type { RequestEvent } from '@sveltejs/kit';
|
||||
@@ -9,13 +8,12 @@ export const GET = async (event: RequestEvent) => {
|
||||
const page = parseInt(url.searchParams.get('page') || '1', 10);
|
||||
const limit = parseInt(url.searchParams.get('limit') || '20', 10);
|
||||
const enableForms = url.searchParams.get('enableForms') === 'true';
|
||||
const region = url.searchParams.get('region');
|
||||
const game = url.searchParams.get('game');
|
||||
const region = url.searchParams.get('region') || '';
|
||||
const game = url.searchParams.get('game') || '';
|
||||
|
||||
try {
|
||||
const userId = await requireAuth(event);
|
||||
await dbConnect();
|
||||
const repo = new CombinedDataRepository();
|
||||
const repo = new CombinedDataRepository(event.locals.supabase, userId);
|
||||
const combinedData = await repo.findCombinedData(userId, page, limit, enableForms, region, game);
|
||||
|
||||
// Return empty array instead of 404 for better UX
|
||||
@@ -33,7 +31,5 @@ export const GET = async (event: RequestEvent) => {
|
||||
throw error;
|
||||
}
|
||||
return json({ error: 'Internal Server Error' }, { status: 500 });
|
||||
} finally {
|
||||
dbDisconnect();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { dbConnect, dbDisconnect } from '$lib/utils/db';
|
||||
import CombinedDataRepository from '$lib/repositories/CombinedDataRepository';
|
||||
import { requireAuth } from '$lib/utils/auth';
|
||||
import type { RequestEvent } from '@sveltejs/kit';
|
||||
@@ -7,13 +6,12 @@ import type { RequestEvent } from '@sveltejs/kit';
|
||||
export const GET = async (event: RequestEvent) => {
|
||||
const { url } = event;
|
||||
const enableForms = url.searchParams.get('enableForms') === 'true';
|
||||
const region = url.searchParams.get('region');
|
||||
const game = url.searchParams.get('game');
|
||||
const region = url.searchParams.get('region') || '';
|
||||
const game = url.searchParams.get('game') || '';
|
||||
|
||||
try {
|
||||
const userId = await requireAuth(event);
|
||||
await dbConnect();
|
||||
const repo = new CombinedDataRepository();
|
||||
const repo = new CombinedDataRepository(event.locals.supabase, userId);
|
||||
const combinedData = await repo.findAllCombinedData(userId, enableForms, region, game);
|
||||
|
||||
// Return empty array instead of 404 for better UX
|
||||
@@ -24,7 +22,5 @@ export const GET = async (event: RequestEvent) => {
|
||||
throw error;
|
||||
}
|
||||
return json({ error: 'Internal Server Error' }, { status: 500 });
|
||||
} finally {
|
||||
dbDisconnect();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,26 +1,14 @@
|
||||
import { json } from '@sveltejs/kit';
|
||||
import { dbConnect, dbDisconnect } from '$lib/utils/db';
|
||||
import { type PokedexEntry } from '$lib/models/PokedexEntry';
|
||||
import PokedexEntryRepository from '$lib/repositories/PokedexEntryRepository';
|
||||
import type { RequestHandler } from './$types';
|
||||
|
||||
export const GET = async () => {
|
||||
return json(await fetchPokeDexEntriesFromDatabase());
|
||||
};
|
||||
|
||||
async function fetchPokeDexEntriesFromDatabase() {
|
||||
let pokemonData = null as PokedexEntry[] | null;
|
||||
|
||||
export const GET: RequestHandler = async (event) => {
|
||||
try {
|
||||
await dbConnect();
|
||||
const repo = new PokedexEntryRepository();
|
||||
pokemonData = await repo.findAll();
|
||||
// order by pokedexNumber property, ascending
|
||||
pokemonData = pokemonData.sort((a, b) => a.pokedexNumber - b.pokedexNumber);
|
||||
const repo = new PokedexEntryRepository(event.locals.supabase);
|
||||
const pokemonData = await repo.findAll();
|
||||
return json(pokemonData);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
} finally {
|
||||
dbDisconnect();
|
||||
console.error('Error fetching pokedex entries:', error);
|
||||
return json({ error: 'Failed to fetch pokedex entries' }, { status: 500 });
|
||||
}
|
||||
|
||||
return pokemonData;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -25,7 +25,7 @@
|
||||
let drawerOpen = false;
|
||||
let viewAsBoxes = false;
|
||||
let currentPlacement = 'boxPlacementForms';
|
||||
let boxNumbers = Array<any>;
|
||||
let boxNumbers: number[] = [];
|
||||
|
||||
const unsubscribe = user.subscribe((value) => {
|
||||
localUser = value;
|
||||
@@ -109,8 +109,20 @@
|
||||
) {
|
||||
let catchRecordsToUpdate = combinedData
|
||||
.filter(({ pokedexEntry }) => pokedexEntry[currentPlacement].box === boxNumber)
|
||||
.map(({ catchRecord }) => {
|
||||
let updatedRecord = { ...catchRecord };
|
||||
.map(({ pokedexEntry, catchRecord }) => {
|
||||
// Create default record if null
|
||||
const baseRecord = catchRecord || {
|
||||
_id: '',
|
||||
userId: localUser?.id || '',
|
||||
pokedexEntryId: pokedexEntry._id,
|
||||
haveToEvolve: false,
|
||||
caught: false,
|
||||
inHome: false,
|
||||
hasGigantamaxed: false,
|
||||
personalNotes: ''
|
||||
};
|
||||
|
||||
let updatedRecord = { ...baseRecord };
|
||||
if (inHome !== null) {
|
||||
updatedRecord = {
|
||||
...updatedRecord,
|
||||
|
||||
Reference in New Issue
Block a user