diff --git a/server/routes/dogs.js b/server/routes/dogs.js index b320c55..9c5e600 100644 --- a/server/routes/dogs.js +++ b/server/routes/dogs.js @@ -41,10 +41,10 @@ const emptyToNull = (value) => { router.get('/', (req, res) => { try { const db = getDatabase(); - // Select only the fields we want, excluding sire/dam if they exist + // Select only fields that exist in the schema (no weight/height) const dogs = db.prepare(` - SELECT id, name, registration_number, microchip, sex, birth_date, breed, - color, weight, height, notes, litter_id, photo_urls, is_active, + SELECT id, name, registration_number, breed, sex, birth_date, + color, microchip, photo_urls, notes, is_active, created_at, updated_at FROM dogs WHERE is_active = 1 @@ -66,10 +66,10 @@ router.get('/', (req, res) => { router.get('/:id', (req, res) => { try { const db = getDatabase(); - // Select only the fields we want, excluding sire/dam if they exist + // Select only fields that exist in the schema (no weight/height) const dog = db.prepare(` - SELECT id, name, registration_number, microchip, sex, birth_date, breed, - color, weight, height, notes, litter_id, photo_urls, is_active, + SELECT id, name, registration_number, breed, sex, birth_date, + color, microchip, photo_urls, notes, is_active, created_at, updated_at FROM dogs WHERE id = ? @@ -116,22 +116,49 @@ router.post('/', (req, res) => { const db = getDatabase(); - // Convert empty strings to null for optional fields - const result = db.prepare(` - INSERT INTO dogs (name, registration_number, breed, sex, birth_date, color, microchip, notes, litter_id, photo_urls) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) - `).run( - name, - emptyToNull(registration_number), - breed, - sex, - emptyToNull(birth_date), - emptyToNull(color), - emptyToNull(microchip), - emptyToNull(notes), - emptyToNull(litter_id), - '[]' - ); + // Check if litter_id column exists + let hasLitterId = false; + try { + const columns = db.prepare("PRAGMA table_info(dogs)").all(); + hasLitterId = columns.some(col => col.name === 'litter_id'); + } catch (e) { + console.error('Error checking schema:', e); + } + + // Insert with or without litter_id depending on schema + let result; + if (hasLitterId) { + result = db.prepare(` + INSERT INTO dogs (name, registration_number, breed, sex, birth_date, color, microchip, notes, litter_id, photo_urls) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) + `).run( + name, + emptyToNull(registration_number), + breed, + sex, + emptyToNull(birth_date), + emptyToNull(color), + emptyToNull(microchip), + emptyToNull(notes), + emptyToNull(litter_id), + '[]' + ); + } else { + result = db.prepare(` + INSERT INTO dogs (name, registration_number, breed, sex, birth_date, color, microchip, notes, photo_urls) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) + `).run( + name, + emptyToNull(registration_number), + breed, + sex, + emptyToNull(birth_date), + emptyToNull(color), + emptyToNull(microchip), + emptyToNull(notes), + '[]' + ); + } const dogId = result.lastInsertRowid; @@ -144,8 +171,8 @@ router.post('/', (req, res) => { } const dog = db.prepare(` - SELECT id, name, registration_number, microchip, sex, birth_date, breed, - color, weight, height, notes, litter_id, photo_urls, is_active, + SELECT id, name, registration_number, breed, sex, birth_date, + color, microchip, photo_urls, notes, is_active, created_at, updated_at FROM dogs WHERE id = ? @@ -165,24 +192,52 @@ router.put('/:id', (req, res) => { const db = getDatabase(); - // Convert empty strings to null for optional fields - db.prepare(` - UPDATE dogs - SET name = ?, registration_number = ?, breed = ?, sex = ?, - birth_date = ?, color = ?, microchip = ?, notes = ?, litter_id = ? - WHERE id = ? - `).run( - name, - emptyToNull(registration_number), - breed, - sex, - emptyToNull(birth_date), - emptyToNull(color), - emptyToNull(microchip), - emptyToNull(notes), - emptyToNull(litter_id), - req.params.id - ); + // Check if litter_id column exists + let hasLitterId = false; + try { + const columns = db.prepare("PRAGMA table_info(dogs)").all(); + hasLitterId = columns.some(col => col.name === 'litter_id'); + } catch (e) { + console.error('Error checking schema:', e); + } + + // Update with or without litter_id + if (hasLitterId) { + db.prepare(` + UPDATE dogs + SET name = ?, registration_number = ?, breed = ?, sex = ?, + birth_date = ?, color = ?, microchip = ?, notes = ?, litter_id = ? + WHERE id = ? + `).run( + name, + emptyToNull(registration_number), + breed, + sex, + emptyToNull(birth_date), + emptyToNull(color), + emptyToNull(microchip), + emptyToNull(notes), + emptyToNull(litter_id), + req.params.id + ); + } else { + db.prepare(` + UPDATE dogs + SET name = ?, registration_number = ?, breed = ?, sex = ?, + birth_date = ?, color = ?, microchip = ?, notes = ? + WHERE id = ? + `).run( + name, + emptyToNull(registration_number), + breed, + sex, + emptyToNull(birth_date), + emptyToNull(color), + emptyToNull(microchip), + emptyToNull(notes), + req.params.id + ); + } // Update parent relationships db.prepare('DELETE FROM parents WHERE dog_id = ?').run(req.params.id); @@ -195,8 +250,8 @@ router.put('/:id', (req, res) => { } const dog = db.prepare(` - SELECT id, name, registration_number, microchip, sex, birth_date, breed, - color, weight, height, notes, litter_id, photo_urls, is_active, + SELECT id, name, registration_number, breed, sex, birth_date, + color, microchip, photo_urls, notes, is_active, created_at, updated_at FROM dogs WHERE id = ?