refactor(*):Address PR comments

This commit is contained in:
Josh Creek
2025-07-26 19:42:04 +01:00
parent bc9ce84615
commit fa38fa69ca
22 changed files with 749 additions and 412 deletions
+39 -9
View File
@@ -7,15 +7,24 @@ import type { RequestEvent } from '@sveltejs/kit';
export const GET = async (event: RequestEvent) => {
try {
const userId = await requireAuth(event);
// Get the user's session and set it on the Supabase client
const { session } = await event.locals.safeGetSession();
if (session) {
await event.locals.supabase.auth.setSession(session);
}
const repo = new CatchRecordRepository(event.locals.supabase, userId);
const catchData = await repo.findByUserId(userId);
// order by pokedexEntryId property, ascending
const sortedData = catchData.sort((a, b) => Number(a.pokedexEntryId) - Number(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) {
throw error;
} catch (err) {
console.error(err);
if (err && typeof err === 'object' && 'status' in err) {
throw err;
}
return json({ error: 'Internal Server Error' }, { status: 500 });
}
@@ -23,18 +32,32 @@ export const GET = async (event: RequestEvent) => {
export const PUT = async (event: RequestEvent) => {
try {
console.log('PUT request received for catch-records');
// Check if we can get a session first
const { session, user } = await event.locals.safeGetSession();
console.log('Session check:', { hasSession: !!session, hasUser: !!user, userId: user?.id });
const userId = await requireAuth(event);
console.log('Auth successful, userId:', userId);
const data: Partial<CatchRecord> = await event.request.json();
// Get the user's session and set it on the Supabase client
if (session) {
await event.locals.supabase.auth.setSession(session);
console.log('Session set on Supabase client');
}
// Ensure the userId is set to the authenticated user
data.userId = userId;
const repo = new CatchRecordRepository(event.locals.supabase, userId);
const upsertedRecord = await repo.upsert(data);
return json(upsertedRecord);
} catch (err) {
console.error(err);
if (err.status) {
if (err && typeof err === 'object' && 'status' in err) {
throw err;
}
return json({ error: 'Internal Server Error' }, { status: 500 });
@@ -45,6 +68,13 @@ export const POST = async (event: RequestEvent) => {
try {
const userId = await requireAuth(event);
const records: Partial<CatchRecord>[] = await event.request.json();
// Get the user's session and set it on the Supabase client
const { session } = await event.locals.safeGetSession();
if (session) {
await event.locals.supabase.auth.setSession(session);
}
const repo = new CatchRecordRepository(event.locals.supabase, userId);
const insertedRecords = [];
@@ -58,7 +88,7 @@ export const POST = async (event: RequestEvent) => {
return json(insertedRecords);
} catch (err) {
console.error(err);
if (err.status) {
if (err && typeof err === 'object' && 'status' in err) {
throw err;
}
return json({ error: 'Internal Server Error' }, { status: 500 });
+27 -10
View File
@@ -1,6 +1,6 @@
import { json } from '@sveltejs/kit';
import CombinedDataRepository from '$lib/repositories/CombinedDataRepository';
import { requireAuth } from '$lib/utils/auth';
import { getOptionalUserId } from '$lib/utils/auth';
import type { RequestEvent } from '@sveltejs/kit';
export const GET = async (event: RequestEvent) => {
@@ -12,23 +12,40 @@ export const GET = async (event: RequestEvent) => {
const game = url.searchParams.get('game') || '';
try {
const userId = await requireAuth(event);
const repo = new CombinedDataRepository(event.locals.supabase, userId);
const combinedData = await repo.findCombinedData(userId, page, limit, enableForms, region, game);
// Get userId if authenticated, null if not
const userId = await getOptionalUserId(event);
// If user is authenticated, set their session on the Supabase client
if (userId) {
const { session } = await event.locals.safeGetSession();
if (session) {
await event.locals.supabase.auth.setSession(session);
}
}
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
if (combinedData.length === 0) {
return json({ combinedData: [], totalPages: 0 });
}
const totalCount = await repo.countCombinedData(userId, enableForms, region, game);
const totalCount = await repo.countCombinedData(userId || '', enableForms, region, game);
const totalPages = Math.ceil(totalCount / limit);
return json({ combinedData, totalPages });
} catch (error) {
console.error(error);
if (error.status) {
throw error;
} catch (err) {
console.error(err);
if (err && typeof err === 'object' && 'status' in err) {
throw err;
}
return json({ error: 'Internal Server Error' }, { status: 500 });
}
+18 -8
View File
@@ -1,6 +1,6 @@
import { json } from '@sveltejs/kit';
import CombinedDataRepository from '$lib/repositories/CombinedDataRepository';
import { requireAuth } from '$lib/utils/auth';
import { getOptionalUserId } from '$lib/utils/auth';
import type { RequestEvent } from '@sveltejs/kit';
export const GET = async (event: RequestEvent) => {
@@ -10,16 +10,26 @@ export const GET = async (event: RequestEvent) => {
const game = url.searchParams.get('game') || '';
try {
const userId = await requireAuth(event);
const repo = new CombinedDataRepository(event.locals.supabase, userId);
const combinedData = await repo.findAllCombinedData(userId, enableForms, region, game);
// Get userId if authenticated, null if not
const userId = await getOptionalUserId(event);
// If user is authenticated, set their session on the Supabase client
if (userId) {
const { session } = await event.locals.safeGetSession();
if (session) {
await event.locals.supabase.auth.setSession(session);
}
}
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
return json(combinedData);
} catch (error) {
console.error(error);
if (error.status) {
throw error;
} catch (err) {
console.error(err);
if (err && typeof err === 'object' && 'status' in err) {
throw err;
}
return json({ error: 'Internal Server Error' }, { status: 500 });
}
+14 -3
View File
@@ -8,6 +8,11 @@
import PokedexSidebar from '$lib/components/pokedex/PokedexSidebar.svelte';
import PokedexViewList from '$lib/components/pokedex/PokedexViewList.svelte';
import PokedexViewBoxes from '$lib/components/pokedex/PokedexViewBoxes.svelte';
import type { PageData } from './$types';
export let data: PageData;
$: ({ supabase, session } = data);
let combinedData = null as CombinedData[] | null;
let currentPage = 1 as number;
@@ -86,13 +91,19 @@
const requestOptions = {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(catchRecord)
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(catchRecord),
credentials: 'include' as RequestCredentials
};
try {
// Use enhanced fetch that includes authentication context
const response = await fetch('/api/catch-records', requestOptions);
if (!response.ok) {
const errorText = await response.text();
console.error('Server response:', response.status, errorText);
alert('Failed to update catch record');
throw new Error('Failed to update catch record');
}
@@ -121,7 +132,7 @@
hasGigantamaxed: false,
personalNotes: ''
};
let updatedRecord = { ...baseRecord };
if (inHome !== null) {
updatedRecord = {
+7
View File
@@ -0,0 +1,7 @@
export const load = async ({ parent }) => {
const { supabase, session } = await parent();
return {
supabase,
session
};
};