diff --git a/server/routes/dogs.js b/server/routes/dogs.js index 9c5e600..3fdf934 100644 --- a/server/routes/dogs.js +++ b/server/routes/dogs.js @@ -41,10 +41,9 @@ const emptyToNull = (value) => { router.get('/', (req, res) => { try { const db = getDatabase(); - // Select only fields that exist in the schema (no weight/height) const dogs = db.prepare(` SELECT id, name, registration_number, breed, sex, birth_date, - color, microchip, photo_urls, notes, is_active, + color, microchip, photo_urls, notes, litter_id, is_active, created_at, updated_at FROM dogs WHERE is_active = 1 @@ -58,18 +57,18 @@ router.get('/', (req, res) => { res.json(dogs); } catch (error) { + console.error('Error fetching dogs:', error); res.status(500).json({ error: error.message }); } }); -// GET single dog by ID +// GET single dog by ID with parents and offspring router.get('/:id', (req, res) => { try { const db = getDatabase(); - // Select only fields that exist in the schema (no weight/height) const dog = db.prepare(` SELECT id, name, registration_number, breed, sex, birth_date, - color, microchip, photo_urls, notes, is_active, + color, microchip, photo_urls, notes, litter_id, is_active, created_at, updated_at FROM dogs WHERE id = ? @@ -101,6 +100,7 @@ router.get('/:id', (req, res) => { res.json(dog); } catch (error) { + console.error('Error fetching dog:', error); res.status(500).json({ error: error.message }); } }); @@ -110,77 +110,64 @@ router.post('/', (req, res) => { try { const { name, registration_number, breed, sex, birth_date, color, microchip, notes, sire_id, dam_id, litter_id } = req.body; + console.log('Creating dog with data:', { name, breed, sex, sire_id, dam_id, litter_id }); + if (!name || !breed || !sex) { return res.status(400).json({ error: 'Name, breed, and sex are required' }); } const db = getDatabase(); - // 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), - '[]' - ); - } + // Insert dog (dogs table has NO sire/dam columns) + 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), + '[]' + ); const dogId = result.lastInsertRowid; + console.log(`✓ Dog inserted with ID: ${dogId}`); - // Add parent relationships - if (sire_id) { - db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, "sire")').run(dogId, sire_id); - } - if (dam_id) { - db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, "dam")').run(dogId, dam_id); + // Add sire relationship if provided + if (sire_id && sire_id !== '' && sire_id !== null) { + console.log(` Adding sire relationship: dog ${dogId} -> sire ${sire_id}`); + db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, ?)'). + run(dogId, sire_id, 'sire'); + console.log(` ✓ Sire relationship added`); } + // Add dam relationship if provided + if (dam_id && dam_id !== '' && dam_id !== null) { + console.log(` Adding dam relationship: dog ${dogId} -> dam ${dam_id}`); + db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, ?)'). + run(dogId, dam_id, 'dam'); + console.log(` ✓ Dam relationship added`); + } + + // Fetch the created dog const dog = db.prepare(` SELECT id, name, registration_number, breed, sex, birth_date, - color, microchip, photo_urls, notes, is_active, + color, microchip, photo_urls, notes, litter_id, is_active, created_at, updated_at FROM dogs WHERE id = ? `).get(dogId); dog.photo_urls = []; + console.log(`✓ Dog created successfully: ${dog.name} (ID: ${dogId})`); res.status(201).json(dog); } catch (error) { + console.error('Error creating dog:', error); res.status(500).json({ error: error.message }); } }); @@ -190,76 +177,64 @@ router.put('/:id', (req, res) => { try { const { name, registration_number, breed, sex, birth_date, color, microchip, notes, sire_id, dam_id, litter_id } = req.body; + console.log(`Updating dog ${req.params.id} with data:`, { name, breed, sex, sire_id, dam_id, litter_id }); + const db = getDatabase(); - // 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 dog record (dogs table has NO sire/dam columns) + 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 + ); + console.log(` ✓ Dog record updated`); - // 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 + // Remove existing parent relationships db.prepare('DELETE FROM parents WHERE dog_id = ?').run(req.params.id); + console.log(` ✓ Old parent relationships removed`); - if (sire_id) { - db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, "sire")').run(req.params.id, sire_id); - } - if (dam_id) { - db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, "dam")').run(req.params.id, dam_id); + // Add new sire relationship if provided + if (sire_id && sire_id !== '' && sire_id !== null) { + console.log(` Adding sire relationship: dog ${req.params.id} -> sire ${sire_id}`); + db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, ?)'). + run(req.params.id, sire_id, 'sire'); + console.log(` ✓ Sire relationship added`); } + // Add new dam relationship if provided + if (dam_id && dam_id !== '' && dam_id !== null) { + console.log(` Adding dam relationship: dog ${req.params.id} -> dam ${dam_id}`); + db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, ?)'). + run(req.params.id, dam_id, 'dam'); + console.log(` ✓ Dam relationship added`); + } + + // Fetch updated dog const dog = db.prepare(` SELECT id, name, registration_number, breed, sex, birth_date, - color, microchip, photo_urls, notes, is_active, + color, microchip, photo_urls, notes, litter_id, is_active, created_at, updated_at FROM dogs WHERE id = ? `).get(req.params.id); dog.photo_urls = dog.photo_urls ? JSON.parse(dog.photo_urls) : []; + console.log(`✓ Dog updated successfully: ${dog.name} (ID: ${req.params.id})`); res.json(dog); } catch (error) { + console.error('Error updating dog:', error); res.status(500).json({ error: error.message }); } }); @@ -269,8 +244,10 @@ router.delete('/:id', (req, res) => { try { const db = getDatabase(); db.prepare('UPDATE dogs SET is_active = 0 WHERE id = ?').run(req.params.id); + console.log(`✓ Dog soft-deleted: ID ${req.params.id}`); res.json({ message: 'Dog deleted successfully' }); } catch (error) { + console.error('Error deleting dog:', error); res.status(500).json({ error: error.message }); } }); @@ -296,6 +273,7 @@ router.post('/:id/photos', upload.single('photo'), (req, res) => { res.json({ url: `/uploads/${req.file.filename}`, photos: photoUrls }); } catch (error) { + console.error('Error uploading photo:', error); res.status(500).json({ error: error.message }); } }); @@ -327,6 +305,7 @@ router.delete('/:id/photos/:photoIndex', (req, res) => { res.json({ photos: photoUrls }); } catch (error) { + console.error('Error deleting photo:', error); res.status(500).json({ error: error.message }); } });