feat(api): add is_external support — GET /api/dogs filters kennel dogs; GET /api/dogs/external returns external roster

This commit is contained in:
2026-03-10 15:24:50 -05:00
parent 0c84b83e75
commit 9738b24db6

View File

@@ -31,15 +31,49 @@ const upload = multer({
const emptyToNull = (v) => (v === '' || v === undefined) ? null : v;
// ── Shared SELECT columns ────────────────────────────────────────────
// ── Shared SELECT columns ────────────────────────────────────────────────
const DOG_COLS = `
id, name, registration_number, breed, sex, birth_date,
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) => {
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 {
const db = getDatabase();
const dogs = db.prepare(`
@@ -48,29 +82,31 @@ router.get('/', (req, res) => {
WHERE is_active = 1
ORDER BY name
`).all();
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);
res.json(attachParents(db, dogs));
} catch (error) {
console.error('Error fetching dogs:', error);
console.error('Error fetching all dogs:', error);
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) => {
try {
const db = getDatabase();
@@ -81,7 +117,7 @@ router.get('/:id', (req, res) => {
dog.photo_urls = dog.photo_urls ? JSON.parse(dog.photo_urls) : [];
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
JOIN dogs d ON p.parent_id = d.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.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
JOIN parents p ON d.id = p.dog_id
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) => {
try {
const { name, registration_number, breed, sex, birth_date, color,
microchip, notes, sire_id, dam_id, litter_id, is_champion } = req.body;
console.log('Creating dog:', { name, breed, sex, sire_id, dam_id, litter_id, is_champion });
microchip, notes, sire_id, dam_id, litter_id, is_champion, is_external } = req.body;
if (!name || !breed || !sex) {
return res.status(400).json({ error: 'Name, breed, and sex are required' });
@@ -119,8 +153,8 @@ router.post('/', (req, res) => {
const db = getDatabase();
const result = db.prepare(`
INSERT INTO dogs (name, registration_number, breed, sex, birth_date, color,
microchip, notes, litter_id, photo_urls, is_champion)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
microchip, notes, litter_id, photo_urls, is_champion, is_external)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`).run(
name,
emptyToNull(registration_number),
@@ -131,11 +165,11 @@ router.post('/', (req, res) => {
emptyToNull(notes),
emptyToNull(litter_id),
'[]',
is_champion ? 1 : 0
is_champion ? 1 : 0,
is_external ? 1 : 0
);
const dogId = result.lastInsertRowid;
console.log(`✔ Dog inserted with ID: ${dogId}`);
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');
@@ -147,7 +181,7 @@ router.post('/', (req, res) => {
const dog = db.prepare(`SELECT ${DOG_COLS} FROM dogs WHERE id = ?`).get(dogId);
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);
} catch (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) => {
try {
const { name, registration_number, breed, sex, birth_date, color,
microchip, notes, sire_id, dam_id, litter_id, is_champion } = req.body;
console.log(`Updating dog ${req.params.id}:`, { name, breed, sex, sire_id, dam_id, is_champion });
microchip, notes, sire_id, dam_id, litter_id, is_champion, is_external } = req.body;
const db = getDatabase();
db.prepare(`
UPDATE dogs
SET name = ?, registration_number = ?, breed = ?, sex = ?,
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 = ?
`).run(
name,
@@ -180,6 +212,7 @@ router.put('/:id', (req, res) => {
emptyToNull(notes),
emptyToNull(litter_id),
is_champion ? 1 : 0,
is_external ? 1 : 0,
req.params.id
);
@@ -202,10 +235,7 @@ router.put('/:id', (req, res) => {
}
});
// ── 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.
// ── DELETE dog (hard delete with cascade) ───────────────────────────────
router.delete('/:id', (req, res) => {
try {
const db = getDatabase();
@@ -213,10 +243,8 @@ router.delete('/:id', (req, res) => {
if (!existing) return res.status(404).json({ error: 'Dog not found' });
const id = req.params.id;
// Cascade cleanup
db.prepare('DELETE FROM parents WHERE parent_id = ?').run(id); // remove as parent
db.prepare('DELETE FROM parents WHERE dog_id = ?').run(id); // remove own parents
db.prepare('DELETE FROM parents WHERE parent_id = ?').run(id);
db.prepare('DELETE FROM parents WHERE dog_id = ?').run(id);
db.prepare('DELETE FROM health_records WHERE dog_id = ?').run(id);
db.prepare('DELETE FROM heat_cycles WHERE dog_id = ?').run(id);
db.prepare('DELETE FROM dogs WHERE id = ?').run(id);
@@ -229,7 +257,7 @@ router.delete('/:id', (req, res) => {
}
});
// ── POST upload photo ────────────────────────────────────────────────
// ── POST upload photo ────────────────────────────────────────────────────
router.post('/:id/photos', upload.single('photo'), (req, res) => {
try {
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) => {
try {
const db = getDatabase();