mirror of
https://github.com/jcreek/LivingDexTracker.git
synced 2026-07-13 02:53:45 +00:00
refactor(*):Address PR comments
This commit is contained in:
@@ -13,15 +13,25 @@
|
||||
export let supabase: any;
|
||||
|
||||
async function signInWithEmail() {
|
||||
// TODO use the data and error from the response
|
||||
const { data, error } = await supabase.auth
|
||||
.signInWithPassword({
|
||||
try {
|
||||
const { data, error } = await supabase.auth.signInWithPassword({
|
||||
email: email,
|
||||
password: password
|
||||
})
|
||||
.then(() => {
|
||||
emitSignedInEvent();
|
||||
});
|
||||
|
||||
if (error) {
|
||||
console.error('Sign in error:', error);
|
||||
alert(`Sign in failed: ${error.message}`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (data) {
|
||||
emitSignedInEvent();
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Sign in error:', err);
|
||||
alert('Sign in failed');
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
@@ -12,8 +12,8 @@
|
||||
|
||||
// Create a working copy for editing
|
||||
$: workingCatchRecord = catchRecord || {
|
||||
_id: '',
|
||||
userId: '',
|
||||
_id: `temp-${Date.now()}-${pokedexEntry._id}`, // Generate temporary ID
|
||||
userId: '', // TODO: Get from session context
|
||||
pokedexEntryId: pokedexEntry._id,
|
||||
haveToEvolve: false,
|
||||
caught: false,
|
||||
@@ -136,8 +136,9 @@
|
||||
</div>
|
||||
{/if}
|
||||
<p>
|
||||
<label class="block font-bold mb-1" for={`personalNotesInput-${catchRecord?._id || pokedexEntry._id}`}
|
||||
>Notes:</label
|
||||
<label
|
||||
class="block font-bold mb-1"
|
||||
for={`personalNotesInput-${catchRecord?._id || pokedexEntry._id}`}>Notes:</label
|
||||
>
|
||||
<textarea
|
||||
bind:value={workingCatchRecord.personalNotes}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import mongoose, { Schema, Document, Types } from 'mongoose';
|
||||
|
||||
export interface CatchRecord extends Document {
|
||||
// Supabase-based CatchRecord interface
|
||||
export interface CatchRecord {
|
||||
_id: string; // Maps to Supabase 'id' field for frontend compatibility
|
||||
userId: string;
|
||||
pokedexEntryId: Types.ObjectId;
|
||||
pokedexEntryId: string; // String representation of the foreign key
|
||||
haveToEvolve: boolean;
|
||||
caught: boolean;
|
||||
inHome: boolean;
|
||||
@@ -10,15 +10,16 @@ export interface CatchRecord extends Document {
|
||||
personalNotes: string;
|
||||
}
|
||||
|
||||
const catchRecordSchema = new Schema<CatchRecord>({
|
||||
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>('CatchRecord', catchRecordSchema);
|
||||
export default CatchRecordModel;
|
||||
// Database record type for Supabase
|
||||
export interface CatchRecordDB {
|
||||
id: string;
|
||||
userId: string;
|
||||
pokedexEntryId: number; // Numeric foreign key in database
|
||||
haveToEvolve: boolean;
|
||||
caught: boolean;
|
||||
inHome: boolean;
|
||||
hasGigantamaxed: boolean;
|
||||
personalNotes: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import mongoose, { Schema, Document } from 'mongoose';
|
||||
|
||||
export interface PokedexEntry extends Document {
|
||||
// Supabase-based PokedexEntry interface
|
||||
export interface PokedexEntry {
|
||||
_id: string; // Maps to Supabase 'id' field for frontend compatibility
|
||||
pokedexNumber: number;
|
||||
boxPlacement: { box: number; row: number; column: number };
|
||||
boxPlacementForms: { box: number; row: number; column: number };
|
||||
@@ -11,41 +11,27 @@ export interface PokedexEntry extends Document {
|
||||
gamesToCatchIn: string[];
|
||||
regionToEvolveIn: string;
|
||||
evolutionInformation: string;
|
||||
catchInformation: [
|
||||
{
|
||||
game: string;
|
||||
location: string;
|
||||
notes: string;
|
||||
}
|
||||
];
|
||||
catchInformation: string[];
|
||||
}
|
||||
|
||||
const pokedexEntrySchema = new Schema<PokedexEntry>({
|
||||
pokedexNumber: Number,
|
||||
boxPlacement: {
|
||||
box: Number,
|
||||
row: Number,
|
||||
column: Number
|
||||
},
|
||||
boxPlacementForms: {
|
||||
box: Number,
|
||||
row: Number,
|
||||
column: Number
|
||||
},
|
||||
pokemon: String,
|
||||
form: String,
|
||||
canGigantamax: Boolean,
|
||||
regionToCatchIn: String,
|
||||
gamesToCatchIn: Array<string>,
|
||||
regionToEvolveIn: String,
|
||||
evolutionInformation: String,
|
||||
catchInformation: [
|
||||
{
|
||||
game: String,
|
||||
location: String,
|
||||
notes: String
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
export default mongoose.model<PokedexEntry>('PokedexEntry', pokedexEntrySchema);
|
||||
// Database record type for Supabase
|
||||
export interface PokedexEntryDB {
|
||||
id: number;
|
||||
pokedexNumber: number;
|
||||
pokemon: string;
|
||||
form: string | null;
|
||||
canGigantamax: boolean;
|
||||
regionToCatchIn: string | null;
|
||||
gamesToCatchIn: string[] | null;
|
||||
regionToEvolveIn: string | null;
|
||||
evolutionInformation: string | null;
|
||||
catchInformation: string[] | null;
|
||||
boxPlacementFormsBox: number | null;
|
||||
boxPlacementFormsRow: number | null;
|
||||
boxPlacementFormsColumn: number | null;
|
||||
boxPlacementBox: number | null;
|
||||
boxPlacementRow: number | null;
|
||||
boxPlacementColumn: number | null;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
@@ -1,35 +1,42 @@
|
||||
import type { CatchRecord } from '$lib/models/CatchRecord';
|
||||
import type { CatchRecord, CatchRecordDB } from '$lib/models/CatchRecord';
|
||||
import type { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
class CatchRecordRepository {
|
||||
constructor(private supabase: SupabaseClient, private userId: string) {}
|
||||
constructor(
|
||||
private supabase: SupabaseClient,
|
||||
private userId: string
|
||||
) {}
|
||||
|
||||
// Transform Supabase snake_case to frontend camelCase
|
||||
private transformCatchRecord(record: any): CatchRecord {
|
||||
// Transform Supabase data to frontend format (minimal transformation)
|
||||
private transformCatchRecord(record: CatchRecordDB): CatchRecord {
|
||||
return {
|
||||
_id: record.id,
|
||||
userId: record.user_id,
|
||||
pokedexEntryId: record.pokedex_entry_id.toString(),
|
||||
haveToEvolve: record.have_to_evolve,
|
||||
userId: record.userId,
|
||||
pokedexEntryId: record.pokedexEntryId.toString(),
|
||||
haveToEvolve: record.haveToEvolve,
|
||||
caught: record.caught,
|
||||
inHome: record.in_home,
|
||||
hasGigantamaxed: record.has_gigantamaxed,
|
||||
personalNotes: record.personal_notes
|
||||
inHome: record.inHome,
|
||||
hasGigantamaxed: record.hasGigantamaxed,
|
||||
personalNotes: record.personalNotes
|
||||
};
|
||||
}
|
||||
|
||||
// Transform frontend camelCase to Supabase snake_case
|
||||
private transformToDatabase(data: Partial<CatchRecord>): any {
|
||||
const dbData: any = {};
|
||||
// Transform frontend data to database format (minimal transformation)
|
||||
private transformToDatabase(data: Partial<CatchRecord>): Partial<CatchRecordDB> {
|
||||
const dbData: Partial<CatchRecordDB> = {};
|
||||
if (data.pokedexEntryId !== undefined && data.pokedexEntryId !== null) {
|
||||
dbData.pokedex_entry_id = Number(data.pokedexEntryId);
|
||||
const numericId = Number(data.pokedexEntryId);
|
||||
if (isNaN(numericId)) {
|
||||
throw new Error(`Invalid pokedexEntryId: ${data.pokedexEntryId}`);
|
||||
}
|
||||
dbData.pokedexEntryId = numericId;
|
||||
}
|
||||
if (data.haveToEvolve !== undefined) dbData.have_to_evolve = data.haveToEvolve;
|
||||
if (data.haveToEvolve !== undefined) dbData.haveToEvolve = data.haveToEvolve;
|
||||
if (data.caught !== undefined) dbData.caught = data.caught;
|
||||
if (data.inHome !== undefined) dbData.in_home = data.inHome;
|
||||
if (data.hasGigantamaxed !== undefined) dbData.has_gigantamaxed = data.hasGigantamaxed;
|
||||
if (data.personalNotes !== undefined) dbData.personal_notes = data.personalNotes;
|
||||
|
||||
if (data.inHome !== undefined) dbData.inHome = data.inHome;
|
||||
if (data.hasGigantamaxed !== undefined) dbData.hasGigantamaxed = data.hasGigantamaxed;
|
||||
if (data.personalNotes !== undefined) dbData.personalNotes = data.personalNotes;
|
||||
|
||||
return dbData;
|
||||
}
|
||||
|
||||
@@ -38,7 +45,7 @@ class CatchRecordRepository {
|
||||
.from('catch_records')
|
||||
.select('*')
|
||||
.eq('id', id)
|
||||
.eq('user_id', this.userId)
|
||||
.eq('userId', this.userId)
|
||||
.single();
|
||||
|
||||
if (error || !data) return null;
|
||||
@@ -49,19 +56,25 @@ class CatchRecordRepository {
|
||||
const { data, error } = await this.supabase
|
||||
.from('catch_records')
|
||||
.select('*')
|
||||
.eq('user_id', this.userId);
|
||||
.eq('userId', this.userId);
|
||||
|
||||
if (error || !data) return [];
|
||||
return data.map(record => this.transformCatchRecord(record));
|
||||
return data.map((record) => this.transformCatchRecord(record));
|
||||
}
|
||||
|
||||
async findByUserId(userId: string): Promise<CatchRecord[]> {
|
||||
return this.findAll(); // Already filtered by user in constructor
|
||||
const { data, error } = await this.supabase
|
||||
.from('catch_records')
|
||||
.select('*')
|
||||
.eq('userId', userId);
|
||||
|
||||
if (error || !data) return [];
|
||||
return data.map((record) => this.transformCatchRecord(record));
|
||||
}
|
||||
|
||||
async create(data: Partial<CatchRecord>): Promise<CatchRecord> {
|
||||
const dbData = {
|
||||
user_id: this.userId,
|
||||
userId: this.userId,
|
||||
...this.transformToDatabase(data)
|
||||
};
|
||||
|
||||
@@ -75,11 +88,11 @@ class CatchRecordRepository {
|
||||
console.error('Supabase error creating catch record:', error);
|
||||
throw new Error(`Failed to create catch record: ${error.message}`);
|
||||
}
|
||||
|
||||
|
||||
if (!result) {
|
||||
throw new Error('Failed to create catch record: No result returned');
|
||||
}
|
||||
|
||||
|
||||
return this.transformCatchRecord(result);
|
||||
}
|
||||
|
||||
@@ -90,7 +103,7 @@ class CatchRecordRepository {
|
||||
.from('catch_records')
|
||||
.update(dbData)
|
||||
.eq('id', id)
|
||||
.eq('user_id', this.userId)
|
||||
.eq('userId', this.userId)
|
||||
.select()
|
||||
.single();
|
||||
|
||||
@@ -99,11 +112,16 @@ class CatchRecordRepository {
|
||||
}
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
await this.supabase
|
||||
const { error } = await this.supabase
|
||||
.from('catch_records')
|
||||
.delete()
|
||||
.eq('id', id)
|
||||
.eq('user_id', this.userId);
|
||||
.eq('userId', this.userId);
|
||||
|
||||
if (error) {
|
||||
console.error('Error deleting catch record:', error);
|
||||
throw new Error(`Failed to delete catch record: ${error.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
async upsert(data: Partial<CatchRecord>): Promise<CatchRecord | null> {
|
||||
@@ -125,8 +143,8 @@ class CatchRecordRepository {
|
||||
const { data, error } = await this.supabase
|
||||
.from('catch_records')
|
||||
.select('*')
|
||||
.eq('user_id', userId)
|
||||
.eq('pokedex_entry_id', pokedexEntryId)
|
||||
.eq('userId', userId)
|
||||
.eq('pokedexEntryId', pokedexEntryId)
|
||||
.single();
|
||||
|
||||
if (error || !data) return null;
|
||||
|
||||
@@ -1,47 +1,50 @@
|
||||
import { type PokedexEntry } from '$lib/models/PokedexEntry';
|
||||
import { type CatchRecord } from '$lib/models/CatchRecord';
|
||||
import { type PokedexEntry, type PokedexEntryDB } from '$lib/models/PokedexEntry';
|
||||
import { type CatchRecord, type CatchRecordDB } from '$lib/models/CatchRecord';
|
||||
import { type CombinedData } from '$lib/models/CombinedData';
|
||||
import type { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
class CombinedDataRepository {
|
||||
constructor(private supabase: SupabaseClient, private userId: string) {}
|
||||
constructor(
|
||||
private supabase: SupabaseClient,
|
||||
private userId: string
|
||||
) {}
|
||||
|
||||
// Transform Supabase data to match frontend expectations
|
||||
private transformPokedexEntry(entry: any): PokedexEntry {
|
||||
// Transform Supabase data to match frontend expectations (minimal transformation)
|
||||
private transformPokedexEntry(entry: PokedexEntryDB): PokedexEntry {
|
||||
return {
|
||||
_id: entry.id.toString(),
|
||||
pokedexNumber: entry.pokedex_number,
|
||||
pokedexNumber: entry.pokedexNumber,
|
||||
pokemon: entry.pokemon,
|
||||
form: entry.form,
|
||||
canGigantamax: entry.can_gigantamax,
|
||||
regionToCatchIn: entry.region_to_catch_in,
|
||||
gamesToCatchIn: entry.games_to_catch_in || [],
|
||||
regionToEvolveIn: entry.region_to_evolve_in,
|
||||
evolutionInformation: entry.evolution_information,
|
||||
catchInformation: entry.catch_information || [],
|
||||
form: entry.form || '',
|
||||
canGigantamax: entry.canGigantamax,
|
||||
regionToCatchIn: entry.regionToCatchIn || '',
|
||||
gamesToCatchIn: entry.gamesToCatchIn || [],
|
||||
regionToEvolveIn: entry.regionToEvolveIn || '',
|
||||
evolutionInformation: entry.evolutionInformation || '',
|
||||
catchInformation: entry.catchInformation || [],
|
||||
boxPlacementForms: {
|
||||
box: entry.box_placement_forms_box,
|
||||
row: entry.box_placement_forms_row,
|
||||
column: entry.box_placement_forms_column
|
||||
box: entry.boxPlacementFormsBox || 0,
|
||||
row: entry.boxPlacementFormsRow || 0,
|
||||
column: entry.boxPlacementFormsColumn || 0
|
||||
},
|
||||
boxPlacement: {
|
||||
box: entry.box_placement_box,
|
||||
row: entry.box_placement_row,
|
||||
column: entry.box_placement_column
|
||||
box: entry.boxPlacementBox || 0,
|
||||
row: entry.boxPlacementRow || 0,
|
||||
column: entry.boxPlacementColumn || 0
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private transformCatchRecord(record: any): CatchRecord {
|
||||
private transformCatchRecord(record: CatchRecordDB): CatchRecord {
|
||||
return {
|
||||
_id: record.id,
|
||||
userId: record.user_id,
|
||||
pokedexEntryId: record.pokedex_entry_id.toString(),
|
||||
haveToEvolve: record.have_to_evolve,
|
||||
userId: record.userId,
|
||||
pokedexEntryId: record.pokedexEntryId.toString(),
|
||||
haveToEvolve: record.haveToEvolve,
|
||||
caught: record.caught,
|
||||
inHome: record.in_home,
|
||||
hasGigantamaxed: record.has_gigantamaxed,
|
||||
personalNotes: record.personal_notes
|
||||
inHome: record.inHome,
|
||||
hasGigantamaxed: record.hasGigantamaxed,
|
||||
personalNotes: record.personalNotes
|
||||
};
|
||||
}
|
||||
|
||||
@@ -51,35 +54,35 @@ class CombinedDataRepository {
|
||||
region: string = '',
|
||||
game: string = ''
|
||||
): Promise<CombinedData[]> {
|
||||
let query = this.supabase
|
||||
.from('pokedex_entries')
|
||||
.select(`
|
||||
let query = this.supabase.from('pokedex_entries').select(`
|
||||
*,
|
||||
catch_records(*)
|
||||
`);
|
||||
|
||||
// Apply filters
|
||||
if (!enableForms) {
|
||||
query = query.not('box_placement_box', 'is', null);
|
||||
query = query.not('boxPlacementBox', 'is', null);
|
||||
}
|
||||
|
||||
if (region) {
|
||||
query = query.eq('region_to_catch_in', region);
|
||||
query = query.eq('regionToCatchIn', region);
|
||||
}
|
||||
|
||||
if (game) {
|
||||
query = query.contains('games_to_catch_in', [game]);
|
||||
query = query.contains('gamesToCatchIn', [game]);
|
||||
}
|
||||
|
||||
// Order by box placement
|
||||
if (enableForms) {
|
||||
query = query.order('box_placement_forms_box', { ascending: true })
|
||||
.order('box_placement_forms_row', { ascending: true })
|
||||
.order('box_placement_forms_column', { ascending: true });
|
||||
query = query
|
||||
.order('boxPlacementFormsBox', { ascending: true })
|
||||
.order('boxPlacementFormsRow', { ascending: true })
|
||||
.order('boxPlacementFormsColumn', { ascending: true });
|
||||
} else {
|
||||
query = query.order('box_placement_box', { ascending: true })
|
||||
.order('box_placement_row', { ascending: true })
|
||||
.order('box_placement_column', { ascending: true });
|
||||
query = query
|
||||
.order('boxPlacementBox', { ascending: true })
|
||||
.order('boxPlacementRow', { ascending: true })
|
||||
.order('boxPlacementColumn', { ascending: true });
|
||||
}
|
||||
|
||||
const { data, error } = await query;
|
||||
@@ -90,13 +93,13 @@ class CombinedDataRepository {
|
||||
}
|
||||
|
||||
// Transform the data and filter catch records by user
|
||||
return (data || []).map(entry => {
|
||||
const userCatchRecord = Array.isArray(entry.catch_records)
|
||||
? entry.catch_records.find((record: any) => record.user_id === userId)
|
||||
return (data || []).map((entry) => {
|
||||
const userCatchRecord = Array.isArray(entry.catch_records)
|
||||
? entry.catch_records.find((record: CatchRecordDB) => record.userId === userId)
|
||||
: null;
|
||||
|
||||
const transformedEntry = this.transformPokedexEntry(entry);
|
||||
const transformedCatchRecord = userCatchRecord
|
||||
const transformedCatchRecord = userCatchRecord
|
||||
? this.transformCatchRecord(userCatchRecord)
|
||||
: null;
|
||||
|
||||
@@ -120,34 +123,38 @@ class CombinedDataRepository {
|
||||
|
||||
let query = this.supabase
|
||||
.from('pokedex_entries')
|
||||
.select(`
|
||||
.select(
|
||||
`
|
||||
*,
|
||||
catch_records(*)
|
||||
`)
|
||||
`
|
||||
)
|
||||
.range(from, to);
|
||||
|
||||
// Apply filters
|
||||
if (!enableForms) {
|
||||
query = query.not('box_placement_box', 'is', null);
|
||||
query = query.not('boxPlacementBox', 'is', null);
|
||||
}
|
||||
|
||||
if (region) {
|
||||
query = query.eq('region_to_catch_in', region);
|
||||
query = query.eq('regionToCatchIn', region);
|
||||
}
|
||||
|
||||
if (game) {
|
||||
query = query.contains('games_to_catch_in', [game]);
|
||||
query = query.contains('gamesToCatchIn', [game]);
|
||||
}
|
||||
|
||||
// Order by box placement
|
||||
if (enableForms) {
|
||||
query = query.order('box_placement_forms_box', { ascending: true })
|
||||
.order('box_placement_forms_row', { ascending: true })
|
||||
.order('box_placement_forms_column', { ascending: true });
|
||||
query = query
|
||||
.order('boxPlacementFormsBox', { ascending: true })
|
||||
.order('boxPlacementFormsRow', { ascending: true })
|
||||
.order('boxPlacementFormsColumn', { ascending: true });
|
||||
} else {
|
||||
query = query.order('box_placement_box', { ascending: true })
|
||||
.order('box_placement_row', { ascending: true })
|
||||
.order('box_placement_column', { ascending: true });
|
||||
query = query
|
||||
.order('boxPlacementBox', { ascending: true })
|
||||
.order('boxPlacementRow', { ascending: true })
|
||||
.order('boxPlacementColumn', { ascending: true });
|
||||
}
|
||||
|
||||
const { data, error } = await query;
|
||||
@@ -158,13 +165,13 @@ class CombinedDataRepository {
|
||||
}
|
||||
|
||||
// Transform the data and filter catch records by user
|
||||
return (data || []).map(entry => {
|
||||
const userCatchRecord = Array.isArray(entry.catch_records)
|
||||
? entry.catch_records.find((record: any) => record.user_id === userId)
|
||||
return (data || []).map((entry) => {
|
||||
const userCatchRecord = Array.isArray(entry.catch_records)
|
||||
? entry.catch_records.find((record: CatchRecordDB) => record.userId === userId)
|
||||
: null;
|
||||
|
||||
const transformedEntry = this.transformPokedexEntry(entry);
|
||||
const transformedCatchRecord = userCatchRecord
|
||||
const transformedCatchRecord = userCatchRecord
|
||||
? this.transformCatchRecord(userCatchRecord)
|
||||
: null;
|
||||
|
||||
@@ -181,21 +188,19 @@ class CombinedDataRepository {
|
||||
region: string,
|
||||
game: string
|
||||
): Promise<number> {
|
||||
let query = this.supabase
|
||||
.from('pokedex_entries')
|
||||
.select('id', { count: 'exact', head: true });
|
||||
let query = this.supabase.from('pokedex_entries').select('id', { count: 'exact', head: true });
|
||||
|
||||
// Apply same filters as in findCombinedData
|
||||
if (!enableForms) {
|
||||
query = query.not('box_placement_box', 'is', null);
|
||||
query = query.not('boxPlacementBox', 'is', null);
|
||||
}
|
||||
|
||||
if (region) {
|
||||
query = query.eq('region_to_catch_in', region);
|
||||
query = query.eq('regionToCatchIn', region);
|
||||
}
|
||||
|
||||
if (game) {
|
||||
query = query.contains('games_to_catch_in', [game]);
|
||||
query = query.contains('gamesToCatchIn', [game]);
|
||||
}
|
||||
|
||||
const { count, error } = await query;
|
||||
@@ -209,4 +214,4 @@ class CombinedDataRepository {
|
||||
}
|
||||
}
|
||||
|
||||
export default CombinedDataRepository;
|
||||
export default CombinedDataRepository;
|
||||
|
||||
@@ -1,31 +1,31 @@
|
||||
import { type PokedexEntry } from '$lib/models/PokedexEntry';
|
||||
import { type PokedexEntry, type PokedexEntryDB } from '$lib/models/PokedexEntry';
|
||||
import type { SupabaseClient } from '@supabase/supabase-js';
|
||||
|
||||
class PokedexEntryRepository {
|
||||
constructor(private supabase: SupabaseClient) {}
|
||||
|
||||
// Transform Supabase data to match frontend expectations
|
||||
private transformPokedexEntry(entry: any): PokedexEntry {
|
||||
// Transform Supabase data to match frontend expectations (minimal transformation)
|
||||
private transformPokedexEntry(entry: PokedexEntryDB): PokedexEntry {
|
||||
return {
|
||||
_id: entry.id.toString(),
|
||||
pokedexNumber: entry.pokedex_number,
|
||||
pokedexNumber: entry.pokedexNumber,
|
||||
pokemon: entry.pokemon,
|
||||
form: entry.form,
|
||||
canGigantamax: entry.can_gigantamax,
|
||||
regionToCatchIn: entry.region_to_catch_in,
|
||||
gamesToCatchIn: entry.games_to_catch_in || [],
|
||||
regionToEvolveIn: entry.region_to_evolve_in,
|
||||
evolutionInformation: entry.evolution_information,
|
||||
catchInformation: entry.catch_information || [],
|
||||
form: entry.form || '',
|
||||
canGigantamax: entry.canGigantamax,
|
||||
regionToCatchIn: entry.regionToCatchIn || '',
|
||||
gamesToCatchIn: entry.gamesToCatchIn || [],
|
||||
regionToEvolveIn: entry.regionToEvolveIn || '',
|
||||
evolutionInformation: entry.evolutionInformation || '',
|
||||
catchInformation: entry.catchInformation || [],
|
||||
boxPlacementForms: {
|
||||
box: entry.box_placement_forms_box,
|
||||
row: entry.box_placement_forms_row,
|
||||
column: entry.box_placement_forms_column
|
||||
box: entry.boxPlacementFormsBox || 0,
|
||||
row: entry.boxPlacementFormsRow || 0,
|
||||
column: entry.boxPlacementFormsColumn || 0
|
||||
},
|
||||
boxPlacement: {
|
||||
box: entry.box_placement_box,
|
||||
row: entry.box_placement_row,
|
||||
column: entry.box_placement_column
|
||||
box: entry.boxPlacementBox || 0,
|
||||
row: entry.boxPlacementRow || 0,
|
||||
column: entry.boxPlacementColumn || 0
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -45,30 +45,35 @@ class PokedexEntryRepository {
|
||||
const { data, error } = await this.supabase
|
||||
.from('pokedex_entries')
|
||||
.select('*')
|
||||
.order('pokedex_number', { ascending: true });
|
||||
.order('pokedexNumber', { ascending: true });
|
||||
|
||||
if (error || !data) return [];
|
||||
return data.map(entry => this.transformPokedexEntry(entry));
|
||||
return data.map((entry) => this.transformPokedexEntry(entry));
|
||||
}
|
||||
|
||||
async create(data: Partial<PokedexEntry>): Promise<PokedexEntry> {
|
||||
// Transform camelCase to snake_case for database
|
||||
const dbData: any = {};
|
||||
if (data.pokedexNumber !== undefined) dbData.pokedex_number = data.pokedexNumber;
|
||||
// Transform to database format (minimal transformation needed)
|
||||
const dbData: Partial<PokedexEntryDB> = {};
|
||||
if (data.pokedexNumber !== undefined) dbData.pokedexNumber = data.pokedexNumber;
|
||||
if (data.pokemon !== undefined) dbData.pokemon = data.pokemon;
|
||||
if (data.form !== undefined) dbData.form = data.form;
|
||||
if (data.canGigantamax !== undefined) dbData.can_gigantamax = data.canGigantamax;
|
||||
if (data.regionToCatchIn !== undefined) dbData.region_to_catch_in = data.regionToCatchIn;
|
||||
if (data.gamesToCatchIn !== undefined) dbData.games_to_catch_in = data.gamesToCatchIn;
|
||||
if (data.regionToEvolveIn !== undefined) dbData.region_to_evolve_in = data.regionToEvolveIn;
|
||||
if (data.evolutionInformation !== undefined) dbData.evolution_information = data.evolutionInformation;
|
||||
if (data.catchInformation !== undefined) dbData.catch_information = data.catchInformation;
|
||||
if (data.boxPlacementForms?.box !== undefined) dbData.box_placement_forms_box = data.boxPlacementForms.box;
|
||||
if (data.boxPlacementForms?.row !== undefined) dbData.box_placement_forms_row = data.boxPlacementForms.row;
|
||||
if (data.boxPlacementForms?.column !== undefined) dbData.box_placement_forms_column = data.boxPlacementForms.column;
|
||||
if (data.boxPlacement?.box !== undefined) dbData.box_placement_box = data.boxPlacement.box;
|
||||
if (data.boxPlacement?.row !== undefined) dbData.box_placement_row = data.boxPlacement.row;
|
||||
if (data.boxPlacement?.column !== undefined) dbData.box_placement_column = data.boxPlacement.column;
|
||||
if (data.canGigantamax !== undefined) dbData.canGigantamax = data.canGigantamax;
|
||||
if (data.regionToCatchIn !== undefined) dbData.regionToCatchIn = data.regionToCatchIn;
|
||||
if (data.gamesToCatchIn !== undefined) dbData.gamesToCatchIn = data.gamesToCatchIn;
|
||||
if (data.regionToEvolveIn !== undefined) dbData.regionToEvolveIn = data.regionToEvolveIn;
|
||||
if (data.evolutionInformation !== undefined)
|
||||
dbData.evolutionInformation = data.evolutionInformation;
|
||||
if (data.catchInformation !== undefined) dbData.catchInformation = data.catchInformation;
|
||||
if (data.boxPlacementForms?.box !== undefined)
|
||||
dbData.boxPlacementFormsBox = data.boxPlacementForms.box;
|
||||
if (data.boxPlacementForms?.row !== undefined)
|
||||
dbData.boxPlacementFormsRow = data.boxPlacementForms.row;
|
||||
if (data.boxPlacementForms?.column !== undefined)
|
||||
dbData.boxPlacementFormsColumn = data.boxPlacementForms.column;
|
||||
if (data.boxPlacement?.box !== undefined) dbData.boxPlacementBox = data.boxPlacement.box;
|
||||
if (data.boxPlacement?.row !== undefined) dbData.boxPlacementRow = data.boxPlacement.row;
|
||||
if (data.boxPlacement?.column !== undefined)
|
||||
dbData.boxPlacementColumn = data.boxPlacement.column;
|
||||
|
||||
const { data: result, error } = await this.supabase
|
||||
.from('pokedex_entries')
|
||||
@@ -81,9 +86,9 @@ class PokedexEntryRepository {
|
||||
}
|
||||
|
||||
async update(id: string, data: Partial<PokedexEntry>): Promise<PokedexEntry | null> {
|
||||
// Transform camelCase to snake_case for database
|
||||
const dbData: any = {};
|
||||
if (data.pokedexNumber !== undefined) dbData.pokedex_number = data.pokedexNumber;
|
||||
// Transform to database format (minimal transformation needed)
|
||||
const dbData: Partial<PokedexEntryDB> = {};
|
||||
if (data.pokedexNumber !== undefined) dbData.pokedexNumber = data.pokedexNumber;
|
||||
if (data.pokemon !== undefined) dbData.pokemon = data.pokemon;
|
||||
// ... add other fields as needed
|
||||
|
||||
@@ -99,10 +104,7 @@ class PokedexEntryRepository {
|
||||
}
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
await this.supabase
|
||||
.from('pokedex_entries')
|
||||
.delete()
|
||||
.eq('id', id);
|
||||
await this.supabase.from('pokedex_entries').delete().eq('id', id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user