Clean: Proper sire/dam handling via parents table with logging
This commit is contained in:
@@ -41,10 +41,9 @@ const emptyToNull = (value) => {
|
|||||||
router.get('/', (req, res) => {
|
router.get('/', (req, res) => {
|
||||||
try {
|
try {
|
||||||
const db = getDatabase();
|
const db = getDatabase();
|
||||||
// Select only fields that exist in the schema (no weight/height)
|
|
||||||
const dogs = db.prepare(`
|
const dogs = db.prepare(`
|
||||||
SELECT id, name, registration_number, breed, sex, birth_date,
|
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
|
created_at, updated_at
|
||||||
FROM dogs
|
FROM dogs
|
||||||
WHERE is_active = 1
|
WHERE is_active = 1
|
||||||
@@ -58,18 +57,18 @@ router.get('/', (req, res) => {
|
|||||||
|
|
||||||
res.json(dogs);
|
res.json(dogs);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error('Error fetching dogs:', error);
|
||||||
res.status(500).json({ error: error.message });
|
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) => {
|
router.get('/:id', (req, res) => {
|
||||||
try {
|
try {
|
||||||
const db = getDatabase();
|
const db = getDatabase();
|
||||||
// Select only fields that exist in the schema (no weight/height)
|
|
||||||
const dog = db.prepare(`
|
const dog = db.prepare(`
|
||||||
SELECT id, name, registration_number, breed, sex, birth_date,
|
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
|
created_at, updated_at
|
||||||
FROM dogs
|
FROM dogs
|
||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
@@ -101,6 +100,7 @@ router.get('/:id', (req, res) => {
|
|||||||
|
|
||||||
res.json(dog);
|
res.json(dog);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error('Error fetching dog:', error);
|
||||||
res.status(500).json({ error: error.message });
|
res.status(500).json({ error: error.message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -110,77 +110,64 @@ router.post('/', (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const { name, registration_number, breed, sex, birth_date, color, microchip, notes, sire_id, dam_id, litter_id } = req.body;
|
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) {
|
if (!name || !breed || !sex) {
|
||||||
return res.status(400).json({ error: 'Name, breed, and sex are required' });
|
return res.status(400).json({ error: 'Name, breed, and sex are required' });
|
||||||
}
|
}
|
||||||
|
|
||||||
const db = getDatabase();
|
const db = getDatabase();
|
||||||
|
|
||||||
// Check if litter_id column exists
|
// Insert dog (dogs table has NO sire/dam columns)
|
||||||
let hasLitterId = false;
|
const result = db.prepare(`
|
||||||
try {
|
INSERT INTO dogs (name, registration_number, breed, sex, birth_date, color, microchip, notes, litter_id, photo_urls)
|
||||||
const columns = db.prepare("PRAGMA table_info(dogs)").all();
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
hasLitterId = columns.some(col => col.name === 'litter_id');
|
`).run(
|
||||||
} catch (e) {
|
name,
|
||||||
console.error('Error checking schema:', e);
|
emptyToNull(registration_number),
|
||||||
}
|
breed,
|
||||||
|
sex,
|
||||||
// Insert with or without litter_id depending on schema
|
emptyToNull(birth_date),
|
||||||
let result;
|
emptyToNull(color),
|
||||||
if (hasLitterId) {
|
emptyToNull(microchip),
|
||||||
result = db.prepare(`
|
emptyToNull(notes),
|
||||||
INSERT INTO dogs (name, registration_number, breed, sex, birth_date, color, microchip, notes, litter_id, photo_urls)
|
emptyToNull(litter_id),
|
||||||
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;
|
const dogId = result.lastInsertRowid;
|
||||||
|
console.log(`✓ Dog inserted with ID: ${dogId}`);
|
||||||
|
|
||||||
// Add parent relationships
|
// Add sire relationship if provided
|
||||||
if (sire_id) {
|
if (sire_id && sire_id !== '' && sire_id !== null) {
|
||||||
db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, "sire")').run(dogId, sire_id);
|
console.log(` Adding sire relationship: dog ${dogId} -> sire ${sire_id}`);
|
||||||
}
|
db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, ?)').
|
||||||
if (dam_id) {
|
run(dogId, sire_id, 'sire');
|
||||||
db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, "dam")').run(dogId, dam_id);
|
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(`
|
const dog = db.prepare(`
|
||||||
SELECT id, name, registration_number, breed, sex, birth_date,
|
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
|
created_at, updated_at
|
||||||
FROM dogs
|
FROM dogs
|
||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
`).get(dogId);
|
`).get(dogId);
|
||||||
dog.photo_urls = [];
|
dog.photo_urls = [];
|
||||||
|
|
||||||
|
console.log(`✓ Dog created successfully: ${dog.name} (ID: ${dogId})`);
|
||||||
res.status(201).json(dog);
|
res.status(201).json(dog);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error('Error creating dog:', error);
|
||||||
res.status(500).json({ error: error.message });
|
res.status(500).json({ error: error.message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -190,76 +177,64 @@ router.put('/:id', (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const { name, registration_number, breed, sex, birth_date, color, microchip, notes, sire_id, dam_id, litter_id } = req.body;
|
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();
|
const db = getDatabase();
|
||||||
|
|
||||||
// Check if litter_id column exists
|
// Update dog record (dogs table has NO sire/dam columns)
|
||||||
let hasLitterId = false;
|
db.prepare(`
|
||||||
try {
|
UPDATE dogs
|
||||||
const columns = db.prepare("PRAGMA table_info(dogs)").all();
|
SET name = ?, registration_number = ?, breed = ?, sex = ?,
|
||||||
hasLitterId = columns.some(col => col.name === 'litter_id');
|
birth_date = ?, color = ?, microchip = ?, notes = ?, litter_id = ?
|
||||||
} catch (e) {
|
WHERE id = ?
|
||||||
console.error('Error checking schema:', e);
|
`).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
|
// Remove existing parent relationships
|
||||||
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);
|
db.prepare('DELETE FROM parents WHERE dog_id = ?').run(req.params.id);
|
||||||
|
console.log(` ✓ Old parent relationships removed`);
|
||||||
|
|
||||||
if (sire_id) {
|
// Add new sire relationship if provided
|
||||||
db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, "sire")').run(req.params.id, sire_id);
|
if (sire_id && sire_id !== '' && sire_id !== null) {
|
||||||
}
|
console.log(` Adding sire relationship: dog ${req.params.id} -> sire ${sire_id}`);
|
||||||
if (dam_id) {
|
db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, ?)').
|
||||||
db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, "dam")').run(req.params.id, dam_id);
|
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(`
|
const dog = db.prepare(`
|
||||||
SELECT id, name, registration_number, breed, sex, birth_date,
|
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
|
created_at, updated_at
|
||||||
FROM dogs
|
FROM dogs
|
||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
`).get(req.params.id);
|
`).get(req.params.id);
|
||||||
dog.photo_urls = dog.photo_urls ? JSON.parse(dog.photo_urls) : [];
|
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);
|
res.json(dog);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error('Error updating dog:', error);
|
||||||
res.status(500).json({ error: error.message });
|
res.status(500).json({ error: error.message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -269,8 +244,10 @@ router.delete('/:id', (req, res) => {
|
|||||||
try {
|
try {
|
||||||
const db = getDatabase();
|
const db = getDatabase();
|
||||||
db.prepare('UPDATE dogs SET is_active = 0 WHERE id = ?').run(req.params.id);
|
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' });
|
res.json({ message: 'Dog deleted successfully' });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error('Error deleting dog:', error);
|
||||||
res.status(500).json({ error: error.message });
|
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 });
|
res.json({ url: `/uploads/${req.file.filename}`, photos: photoUrls });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error('Error uploading photo:', error);
|
||||||
res.status(500).json({ error: error.message });
|
res.status(500).json({ error: error.message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -327,6 +305,7 @@ router.delete('/:id/photos/:photoIndex', (req, res) => {
|
|||||||
|
|
||||||
res.json({ photos: photoUrls });
|
res.json({ photos: photoUrls });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.error('Error deleting photo:', error);
|
||||||
res.status(500).json({ error: error.message });
|
res.status(500).json({ error: error.message });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user