Enhanced litters API with puppy linking and litter_id support

This commit is contained in:
2026-03-09 00:07:26 -05:00
parent a246e5f84f
commit cc5af29c11

View File

@@ -16,18 +16,18 @@ router.get('/', (req, res) => {
ORDER BY l.breeding_date DESC
`).all();
// Get puppies for each litter
// Get puppies for each litter using litter_id
litters.forEach(litter => {
litter.puppies = db.prepare(`
SELECT d.* FROM dogs d
JOIN parents ps ON d.id = ps.dog_id
JOIN parents pd ON d.id = pd.dog_id
WHERE ps.parent_id = ? AND pd.parent_id = ?
`).all(litter.sire_id, litter.dam_id);
SELECT * FROM dogs WHERE litter_id = ? AND is_active = 1
`).all(litter.id);
litter.puppies.forEach(puppy => {
puppy.photo_urls = puppy.photo_urls ? JSON.parse(puppy.photo_urls) : [];
});
// Update puppy_count based on actual puppies
litter.actual_puppy_count = litter.puppies.length;
});
res.json(litters);
@@ -54,17 +54,17 @@ router.get('/:id', (req, res) => {
return res.status(404).json({ error: 'Litter not found' });
}
// Get puppies using litter_id
litter.puppies = db.prepare(`
SELECT d.* FROM dogs d
JOIN parents ps ON d.id = ps.dog_id
JOIN parents pd ON d.id = pd.dog_id
WHERE ps.parent_id = ? AND pd.parent_id = ?
`).all(litter.sire_id, litter.dam_id);
SELECT * FROM dogs WHERE litter_id = ? AND is_active = 1
`).all(litter.id);
litter.puppies.forEach(puppy => {
puppy.photo_urls = puppy.photo_urls ? JSON.parse(puppy.photo_urls) : [];
});
litter.actual_puppy_count = litter.puppies.length;
res.json(litter);
} catch (error) {
res.status(500).json({ error: error.message });
@@ -125,11 +125,70 @@ router.put('/:id', (req, res) => {
}
});
// POST link puppy to litter
router.post('/:id/puppies/:puppyId', (req, res) => {
try {
const { id: litterId, puppyId } = req.params;
const db = getDatabase();
// Verify litter exists
const litter = db.prepare('SELECT sire_id, dam_id FROM litters WHERE id = ?').get(litterId);
if (!litter) {
return res.status(404).json({ error: 'Litter not found' });
}
// Verify puppy exists
const puppy = db.prepare('SELECT id FROM dogs WHERE id = ?').get(puppyId);
if (!puppy) {
return res.status(404).json({ error: 'Puppy not found' });
}
// Link puppy to litter
db.prepare('UPDATE dogs SET litter_id = ? WHERE id = ?').run(litterId, puppyId);
// Also update parent relationships if not set
const existingParents = db.prepare('SELECT parent_type FROM parents WHERE dog_id = ?').all(puppyId);
const hasSire = existingParents.some(p => p.parent_type === 'sire');
const hasDam = existingParents.some(p => p.parent_type === 'dam');
if (!hasSire) {
db.prepare('INSERT OR IGNORE INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, \'sire\')').run(puppyId, litter.sire_id);
}
if (!hasDam) {
db.prepare('INSERT OR IGNORE INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, \'dam\')').run(puppyId, litter.dam_id);
}
res.json({ message: 'Puppy linked to litter successfully' });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// DELETE remove puppy from litter
router.delete('/:id/puppies/:puppyId', (req, res) => {
try {
const { puppyId } = req.params;
const db = getDatabase();
db.prepare('UPDATE dogs SET litter_id = NULL WHERE id = ?').run(puppyId);
res.json({ message: 'Puppy removed from litter' });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// DELETE litter
router.delete('/:id', (req, res) => {
try {
const db = getDatabase();
// Remove litter_id from associated puppies
db.prepare('UPDATE dogs SET litter_id = NULL WHERE litter_id = ?').run(req.params.id);
// Delete the litter
db.prepare('DELETE FROM litters WHERE id = ?').run(req.params.id);
res.json({ message: 'Litter deleted successfully' });
} catch (error) {
res.status(500).json({ error: error.message });