diff --git a/src/lib/components/pokedex/PokedexEntryCatchRecord.svelte b/src/lib/components/pokedex/PokedexEntryCatchRecord.svelte new file mode 100644 index 0000000..6d021c9 --- /dev/null +++ b/src/lib/components/pokedex/PokedexEntryCatchRecord.svelte @@ -0,0 +1,165 @@ + + +
+
+
+
+ +
+
+

{pokedexEntry.pokemon}

+ #{pokedexEntry.pokedexNumber.toString().padStart(3, '0')} +
+
+ +
+

Form: {pokedexEntry.form ? pokedexEntry.form : '-'}

+
+ +
+

Box: {pokedexEntry.boxPlacement.box}

+

Row: {pokedexEntry.boxPlacement.row}

+

Column: {pokedexEntry.boxPlacement.column}

+

Can Gigantamax: {pokedexEntry.canGigantamax ? 'Yes' : 'No'}

+

Notes: {pokedexEntry.notes}

+
+
+ +
+
+ + +
+
+ + +
+
+ + +
+ {#if pokedexEntry.canGigantamax} +
+ + +
+ {/if} +

+ + +

+
+ +
+
+

Region to Catch In: {pokedexEntry.regionToCatchIn}

+

Games to catch in:

+
    + {#each pokedexEntry.gamesToCatchIn as game} +
  • {game}
  • + {/each} +
+ {#if pokedexEntry.regionToEvolveIn} +

Region to Evolve In: {pokedexEntry.regionToEvolveIn}

+ {/if} +
+
+ +
+
+
+ + diff --git a/src/lib/models/CatchRecord.ts b/src/lib/models/CatchRecord.ts new file mode 100644 index 0000000..8919e43 --- /dev/null +++ b/src/lib/models/CatchRecord.ts @@ -0,0 +1,24 @@ +import mongoose, { Schema, Document, Types } from 'mongoose'; + +export interface CatchRecord extends Document { + userId: string; + pokedexEntryId: Types.ObjectId; + haveToEvolve: boolean; + caught: boolean; + inHome: boolean; + hasGigantamaxed: boolean; + personalNotes: string; +} + +const catchRecordSchema = new Schema({ + userId: String, + pokedexEntryId: { type: Schema.Types.ObjectId, ref: 'PokedexEntry', required: true }, + haveToEvolve: { type: Boolean, default: false }, + caught: { type: Boolean, default: false }, + inHome: { type: Boolean, default: false }, + hasGigantamaxed: { type: Boolean, default: false }, + personalNotes: String +}); + +const CatchRecordModel = mongoose.model('CatchRecord', catchRecordSchema); +export default CatchRecordModel; diff --git a/src/lib/models/CombinedData.ts b/src/lib/models/CombinedData.ts new file mode 100644 index 0000000..7138032 --- /dev/null +++ b/src/lib/models/CombinedData.ts @@ -0,0 +1,7 @@ +import { type PokedexEntry } from './PokedexEntry'; +import { type CatchRecord } from './CatchRecord'; + +export interface CombinedData { + pokedexEntry: PokedexEntry; + catchRecord: CatchRecord; +} diff --git a/src/lib/repositories/CatchRecordRepository.ts b/src/lib/repositories/CatchRecordRepository.ts new file mode 100644 index 0000000..21e3e6c --- /dev/null +++ b/src/lib/repositories/CatchRecordRepository.ts @@ -0,0 +1,35 @@ +import CatchRecordModel, { type CatchRecord } from '$lib/models/CatchRecord'; + +class CatchRecordRepository { + async findById(id: string): Promise { + return CatchRecordModel.findById(id).exec(); + } + + async findAll(): Promise { + return CatchRecordModel.find().exec(); + } + + async create(data: Partial): Promise { + return CatchRecordModel.create(data); + } + + async update(id: string, data: Partial): Promise { + return CatchRecordModel.findByIdAndUpdate(id, data, { new: true }).exec(); + } + + async delete(id: string): Promise { + await CatchRecordModel.findByIdAndDelete(id).exec(); + } + + async upsert(data: Partial): Promise { + if (data._id) { + // Update existing record + return CatchRecordModel.findByIdAndUpdate(data._id, data, { new: true }).exec(); + } else { + // Create new record + return CatchRecordModel.create(data); + } + } +} + +export default CatchRecordRepository; diff --git a/src/routes/api/catch-records/+server.ts b/src/routes/api/catch-records/+server.ts new file mode 100644 index 0000000..8865b88 --- /dev/null +++ b/src/routes/api/catch-records/+server.ts @@ -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 = 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[] = 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(); + } +}; diff --git a/src/routes/mydex/+page.svelte b/src/routes/mydex/+page.svelte index c46930d..564cd17 100644 --- a/src/routes/mydex/+page.svelte +++ b/src/routes/mydex/+page.svelte @@ -1,49 +1,189 @@ -{#if pokemonData} - {#each pokemonData as pokemon} +
+ {#if combinedData}
- -

- {pokemon.pokedexNumber.toString().padStart(3, '0')} - {pokemon.pokemon} - {pokemon.form} -

+ +
- {/each} -{:else} - -{/if} + + + {#each combinedData as { pokedexEntry, catchRecord }} +
+ updateACatch(catchRecord)} + /> +
+ {/each} + {:else} + +

Loading Pokédex

+

Please be patient, this can take some time on the first load.

+ {#if totalRecordsCreated > 0} +

Processing {totalRecordsCreated}/{pokedexEntries?.length}

+ {:else if creatingRecords} +

Processing...

+ {/if} + {/if} +