feature/external-dogs #50

Merged
jason merged 9 commits from feature/external-dogs into master 2026-03-11 01:01:49 -05:00
Showing only changes of commit 9738b24db6 - Show all commits

View File

@@ -31,15 +31,49 @@ const upload = multer({
const emptyToNull = (v) => (v === '' || v === undefined) ? null : v; const emptyToNull = (v) => (v === '' || v === undefined) ? null : v;
// ── Shared SELECT columns ──────────────────────────────────────────── // ── Shared SELECT columns ────────────────────────────────────────────────
const DOG_COLS = ` const DOG_COLS = `
id, name, registration_number, breed, sex, birth_date, id, name, registration_number, breed, sex, birth_date,
color, microchip, photo_urls, notes, litter_id, is_active, color, microchip, photo_urls, notes, litter_id, is_active,
is_champion, created_at, updated_at is_champion, is_external, created_at, updated_at
`; `;
// ── GET all dogs ───────────────────────────────────────────────────── // ── Helper: attach parents to a list of dogs ─────────────────────────────
function attachParents(db, dogs) {
const parentStmt = db.prepare(`
SELECT p.parent_type, d.id, d.name, d.is_champion, d.is_external
FROM parents p
JOIN dogs d ON p.parent_id = d.id
WHERE p.dog_id = ?
`);
dogs.forEach(dog => {
dog.photo_urls = dog.photo_urls ? JSON.parse(dog.photo_urls) : [];
const parents = parentStmt.all(dog.id);
dog.sire = parents.find(p => p.parent_type === 'sire') || null;
dog.dam = parents.find(p => p.parent_type === 'dam') || null;
});
return dogs;
}
// ── GET all kennel dogs (is_external = 0) ───────────────────────────────────
router.get('/', (req, res) => { router.get('/', (req, res) => {
try {
const db = getDatabase();
const dogs = db.prepare(`
SELECT ${DOG_COLS}
FROM dogs
WHERE is_active = 1 AND is_external = 0
ORDER BY name
`).all();
res.json(attachParents(db, dogs));
} catch (error) {
console.error('Error fetching dogs:', error);
res.status(500).json({ error: error.message });
}
});
// ── GET all dogs (kennel + external) for dropdowns/pairing/pedigree ──────────
router.get('/all', (req, res) => {
try { try {
const db = getDatabase(); const db = getDatabase();
const dogs = db.prepare(` const dogs = db.prepare(`
@@ -48,29 +82,31 @@ router.get('/', (req, res) => {
WHERE is_active = 1 WHERE is_active = 1
ORDER BY name ORDER BY name
`).all(); `).all();
res.json(attachParents(db, dogs));
const parentStmt = db.prepare(`
SELECT p.parent_type, d.id, d.name, d.is_champion
FROM parents p
JOIN dogs d ON p.parent_id = d.id
WHERE p.dog_id = ?
`);
dogs.forEach(dog => {
dog.photo_urls = dog.photo_urls ? JSON.parse(dog.photo_urls) : [];
const parents = parentStmt.all(dog.id);
dog.sire = parents.find(p => p.parent_type === 'sire') || null;
dog.dam = parents.find(p => p.parent_type === 'dam') || null;
});
res.json(dogs);
} catch (error) { } catch (error) {
console.error('Error fetching dogs:', error); console.error('Error fetching all dogs:', error);
res.status(500).json({ error: error.message }); res.status(500).json({ error: error.message });
} }
}); });
// ── GET single dog (with parents + offspring) ──────────────────────── // ── GET external dogs only (is_external = 1) ──────────────────────────────
router.get('/external', (req, res) => {
try {
const db = getDatabase();
const dogs = db.prepare(`
SELECT ${DOG_COLS}
FROM dogs
WHERE is_active = 1 AND is_external = 1
ORDER BY name
`).all();
res.json(attachParents(db, dogs));
} catch (error) {
console.error('Error fetching external dogs:', error);
res.status(500).json({ error: error.message });
}
});
// ── GET single dog (with parents + offspring) ──────────────────────────
router.get('/:id', (req, res) => { router.get('/:id', (req, res) => {
try { try {
const db = getDatabase(); const db = getDatabase();
@@ -81,7 +117,7 @@ router.get('/:id', (req, res) => {
dog.photo_urls = dog.photo_urls ? JSON.parse(dog.photo_urls) : []; dog.photo_urls = dog.photo_urls ? JSON.parse(dog.photo_urls) : [];
const parents = db.prepare(` const parents = db.prepare(`
SELECT p.parent_type, d.id, d.name, d.is_champion SELECT p.parent_type, d.id, d.name, d.is_champion, d.is_external
FROM parents p FROM parents p
JOIN dogs d ON p.parent_id = d.id JOIN dogs d ON p.parent_id = d.id
WHERE p.dog_id = ? WHERE p.dog_id = ?
@@ -91,7 +127,7 @@ router.get('/:id', (req, res) => {
dog.dam = parents.find(p => p.parent_type === 'dam') || null; dog.dam = parents.find(p => p.parent_type === 'dam') || null;
dog.offspring = db.prepare(` dog.offspring = db.prepare(`
SELECT d.id, d.name, d.sex, d.is_champion SELECT d.id, d.name, d.sex, d.is_champion, d.is_external
FROM dogs d FROM dogs d
JOIN parents p ON d.id = p.dog_id JOIN parents p ON d.id = p.dog_id
WHERE p.parent_id = ? AND d.is_active = 1 WHERE p.parent_id = ? AND d.is_active = 1
@@ -104,13 +140,11 @@ router.get('/:id', (req, res) => {
} }
}); });
// ── POST create dog ────────────────────────────────────────────────── // ── POST create dog ─────────────────────────────────────────────────────
router.post('/', (req, res) => { router.post('/', (req, res) => {
try { try {
const { name, registration_number, breed, sex, birth_date, color, const { name, registration_number, breed, sex, birth_date, color,
microchip, notes, sire_id, dam_id, litter_id, is_champion } = req.body; microchip, notes, sire_id, dam_id, litter_id, is_champion, is_external } = req.body;
console.log('Creating dog:', { name, breed, sex, sire_id, dam_id, litter_id, is_champion });
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' });
@@ -119,8 +153,8 @@ router.post('/', (req, res) => {
const db = getDatabase(); const db = getDatabase();
const result = db.prepare(` const result = db.prepare(`
INSERT INTO dogs (name, registration_number, breed, sex, birth_date, color, INSERT INTO dogs (name, registration_number, breed, sex, birth_date, color,
microchip, notes, litter_id, photo_urls, is_champion) microchip, notes, litter_id, photo_urls, is_champion, is_external)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`).run( `).run(
name, name,
emptyToNull(registration_number), emptyToNull(registration_number),
@@ -131,11 +165,11 @@ router.post('/', (req, res) => {
emptyToNull(notes), emptyToNull(notes),
emptyToNull(litter_id), emptyToNull(litter_id),
'[]', '[]',
is_champion ? 1 : 0 is_champion ? 1 : 0,
is_external ? 1 : 0
); );
const dogId = result.lastInsertRowid; const dogId = result.lastInsertRowid;
console.log(`✔ Dog inserted with ID: ${dogId}`);
if (sire_id && sire_id !== '' && sire_id !== null) { if (sire_id && sire_id !== '' && sire_id !== null) {
db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, ?)').run(dogId, sire_id, 'sire'); db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, ?)').run(dogId, sire_id, 'sire');
@@ -147,7 +181,7 @@ router.post('/', (req, res) => {
const dog = db.prepare(`SELECT ${DOG_COLS} FROM dogs WHERE id = ?`).get(dogId); const dog = db.prepare(`SELECT ${DOG_COLS} FROM dogs WHERE id = ?`).get(dogId);
dog.photo_urls = []; dog.photo_urls = [];
console.log(`✔ Dog created: ${dog.name} (ID: ${dogId})`); console.log(`✔ Dog created: ${dog.name} (ID: ${dogId}, external: ${dog.is_external})`);
res.status(201).json(dog); res.status(201).json(dog);
} catch (error) { } catch (error) {
console.error('Error creating dog:', error); console.error('Error creating dog:', error);
@@ -155,20 +189,18 @@ router.post('/', (req, res) => {
} }
}); });
// ── PUT update dog ─────────────────────────────────────────────────── // ── PUT update dog ───────────────────────────────────────────────────────
router.put('/:id', (req, res) => { router.put('/:id', (req, res) => {
try { try {
const { name, registration_number, breed, sex, birth_date, color, const { name, registration_number, breed, sex, birth_date, color,
microchip, notes, sire_id, dam_id, litter_id, is_champion } = req.body; microchip, notes, sire_id, dam_id, litter_id, is_champion, is_external } = req.body;
console.log(`Updating dog ${req.params.id}:`, { name, breed, sex, sire_id, dam_id, is_champion });
const db = getDatabase(); const db = getDatabase();
db.prepare(` db.prepare(`
UPDATE dogs UPDATE dogs
SET name = ?, registration_number = ?, breed = ?, sex = ?, SET name = ?, registration_number = ?, breed = ?, sex = ?,
birth_date = ?, color = ?, microchip = ?, notes = ?, birth_date = ?, color = ?, microchip = ?, notes = ?,
litter_id = ?, is_champion = ?, updated_at = datetime('now') litter_id = ?, is_champion = ?, is_external = ?, updated_at = datetime('now')
WHERE id = ? WHERE id = ?
`).run( `).run(
name, name,
@@ -180,6 +212,7 @@ router.put('/:id', (req, res) => {
emptyToNull(notes), emptyToNull(notes),
emptyToNull(litter_id), emptyToNull(litter_id),
is_champion ? 1 : 0, is_champion ? 1 : 0,
is_external ? 1 : 0,
req.params.id req.params.id
); );
@@ -202,10 +235,7 @@ router.put('/:id', (req, res) => {
} }
}); });
// ── DELETE dog (hard delete with cascade) ──────────────────────────── // ── DELETE dog (hard delete with cascade) ───────────────────────────────
// Removes: parent relationships (both directions), health records,
// heat cycles, and the dog record itself.
// Photo files on disk are NOT removed here — run a gc job if needed.
router.delete('/:id', (req, res) => { router.delete('/:id', (req, res) => {
try { try {
const db = getDatabase(); const db = getDatabase();
@@ -213,13 +243,11 @@ router.delete('/:id', (req, res) => {
if (!existing) return res.status(404).json({ error: 'Dog not found' }); if (!existing) return res.status(404).json({ error: 'Dog not found' });
const id = req.params.id; const id = req.params.id;
db.prepare('DELETE FROM parents WHERE parent_id = ?').run(id);
// Cascade cleanup db.prepare('DELETE FROM parents WHERE dog_id = ?').run(id);
db.prepare('DELETE FROM parents WHERE parent_id = ?').run(id); // remove as parent db.prepare('DELETE FROM health_records WHERE dog_id = ?').run(id);
db.prepare('DELETE FROM parents WHERE dog_id = ?').run(id); // remove own parents db.prepare('DELETE FROM heat_cycles WHERE dog_id = ?').run(id);
db.prepare('DELETE FROM health_records WHERE dog_id = ?').run(id); db.prepare('DELETE FROM dogs WHERE id = ?').run(id);
db.prepare('DELETE FROM heat_cycles WHERE dog_id = ?').run(id);
db.prepare('DELETE FROM dogs WHERE id = ?').run(id);
console.log(`✔ Dog #${id} (${existing.name}) permanently deleted`); console.log(`✔ Dog #${id} (${existing.name}) permanently deleted`);
res.json({ success: true, message: `${existing.name} has been deleted` }); res.json({ success: true, message: `${existing.name} has been deleted` });
@@ -229,7 +257,7 @@ router.delete('/:id', (req, res) => {
} }
}); });
// ── POST upload photo ──────────────────────────────────────────────── // ── POST upload photo ────────────────────────────────────────────────────
router.post('/:id/photos', upload.single('photo'), (req, res) => { router.post('/:id/photos', upload.single('photo'), (req, res) => {
try { try {
if (!req.file) return res.status(400).json({ error: 'No file uploaded' }); if (!req.file) return res.status(400).json({ error: 'No file uploaded' });
@@ -249,7 +277,7 @@ router.post('/:id/photos', upload.single('photo'), (req, res) => {
} }
}); });
// ── DELETE photo ───────────────────────────────────────────────────── // ── DELETE photo ─────────────────────────────────────────────────────
router.delete('/:id/photos/:photoIndex', (req, res) => { router.delete('/:id/photos/:photoIndex', (req, res) => {
try { try {
const db = getDatabase(); const db = getDatabase();