feat(*): Add support for catch records to My Dex page

This commit is contained in:
Josh Creek
2024-07-12 14:19:44 +01:00
parent 0e8af37309
commit e7f1930eff
6 changed files with 460 additions and 31 deletions
+58
View File
@@ -0,0 +1,58 @@
import { json } from '@sveltejs/kit';
import { dbConnect, dbDisconnect } from '$lib/utils/db';
import { type CatchRecord } from '$lib/models/CatchRecord';
import CatchRecordRepository from '$lib/repositories/CatchRecordRepository';
export const GET = async () => {
let catchData = null as CatchRecord[] | null;
try {
await dbConnect();
const repo = new CatchRecordRepository();
catchData = await repo.findAll();
// order by pokedexEntryId property, ascending
catchData = catchData.sort((a, b) => a.pokedexEntryId - b.pokedexEntryId);
} catch (error) {
console.error(error);
} finally {
dbDisconnect();
}
return json(catchData);
};
export const PUT = async ({ request }) => {
try {
const data: Partial<CatchRecord> = await request.json();
await dbConnect();
const repo = new CatchRecordRepository();
const upsertedRecord = await repo.upsert(data);
return json(upsertedRecord);
} catch (err) {
console.error(err);
throw error(500, 'Internal Server Error');
} finally {
await dbDisconnect();
}
};
export const POST = async ({ request }) => {
try {
const records: Partial<CatchRecord>[] = await request.json();
await dbConnect();
const repo = new CatchRecordRepository();
const insertedRecords = [];
for (const record of records) {
const upsertedRecord = await repo.upsert(record);
insertedRecords.push(upsertedRecord);
}
return json(insertedRecords);
} catch (err) {
console.error(err);
throw error(500, 'Internal Server Error');
} finally {
await dbDisconnect();
}
};