diff --git a/server/routes/litters.js b/server/routes/litters.js index e8cf492..c400be3 100644 --- a/server/routes/litters.js +++ b/server/routes/litters.js @@ -16,17 +16,13 @@ router.get('/', (req, res) => { ORDER BY l.breeding_date DESC `).all(); - // Get puppies for each litter using litter_id litters.forEach(litter => { litter.puppies = db.prepare(` 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; }); @@ -36,14 +32,14 @@ router.get('/', (req, res) => { } }); -// GET single litter +// GET single litter with puppies router.get('/:id', (req, res) => { try { const db = getDatabase(); const litter = db.prepare(` - SELECT l.*, - s.*, s.name as sire_name, - d.*, d.name as dam_name + SELECT l.*, + s.name as sire_name, s.registration_number as sire_reg, s.breed as sire_breed, + d.name as dam_name, d.registration_number as dam_reg, d.breed as dam_breed FROM litters l JOIN dogs s ON l.sire_id = s.id JOIN dogs d ON l.dam_id = d.id @@ -54,7 +50,6 @@ router.get('/:id', (req, res) => { return res.status(404).json({ error: 'Litter not found' }); } - // Get puppies using litter_id litter.puppies = db.prepare(` SELECT * FROM dogs WHERE litter_id = ? AND is_active = 1 `).all(litter.id); @@ -74,7 +69,7 @@ router.get('/:id', (req, res) => { // POST create new litter router.post('/', (req, res) => { try { - const { sire_id, dam_id, breeding_date, whelping_date, notes } = req.body; + const { sire_id, dam_id, breeding_date, whelping_date, puppy_count, notes } = req.body; if (!sire_id || !dam_id || !breeding_date) { return res.status(400).json({ error: 'Sire, dam, and breeding date are required' }); @@ -82,7 +77,6 @@ router.post('/', (req, res) => { const db = getDatabase(); - // Verify sire is male and dam is female const sire = db.prepare('SELECT sex FROM dogs WHERE id = ?').get(sire_id); const dam = db.prepare('SELECT sex FROM dogs WHERE id = ?').get(dam_id); @@ -94,12 +88,11 @@ router.post('/', (req, res) => { } const result = db.prepare(` - INSERT INTO litters (sire_id, dam_id, breeding_date, whelping_date, notes) - VALUES (?, ?, ?, ?, ?) - `).run(sire_id, dam_id, breeding_date, whelping_date, notes); + INSERT INTO litters (sire_id, dam_id, breeding_date, whelping_date, puppy_count, notes) + VALUES (?, ?, ?, ?, ?, ?) + `).run(sire_id, dam_id, breeding_date, whelping_date || null, puppy_count || 0, notes || null); const litter = db.prepare('SELECT * FROM litters WHERE id = ?').get(result.lastInsertRowid); - res.status(201).json(litter); } catch (error) { res.status(500).json({ error: error.message }); @@ -110,13 +103,12 @@ router.post('/', (req, res) => { router.put('/:id', (req, res) => { try { const { breeding_date, whelping_date, puppy_count, notes } = req.body; - const db = getDatabase(); db.prepare(` UPDATE litters SET breeding_date = ?, whelping_date = ?, puppy_count = ?, notes = ? WHERE id = ? - `).run(breeding_date, whelping_date, puppy_count, notes, req.params.id); + `).run(breeding_date, whelping_date || null, puppy_count || 0, notes || null, req.params.id); const litter = db.prepare('SELECT * FROM litters WHERE id = ?').get(req.params.id); res.json(litter); @@ -131,22 +123,14 @@ router.post('/:id/puppies/:puppyId', (req, res) => { 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' }); - } + 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' }); - } + 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'); @@ -169,26 +153,77 @@ 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 }); } }); +// ─── Puppy Weight / Health Log ─────────────────────────────────────────────── + +// GET weight/health logs for a puppy +router.get('/:litterId/puppies/:puppyId/logs', (req, res) => { + try { + const db = getDatabase(); + // Use health_records table with note field to store weight logs + const logs = db.prepare(` + SELECT * FROM health_records + WHERE dog_id = ? AND record_type = 'weight_log' + ORDER BY record_date ASC + `).all(req.params.puppyId); + res.json(logs); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}); + +// POST add weight/health log entry for a puppy +router.post('/:litterId/puppies/:puppyId/logs', (req, res) => { + try { + const { puppyId } = req.params; + const { record_date, weight_oz, weight_lbs, notes, record_type } = req.body; + + if (!record_date) return res.status(400).json({ error: 'record_date is required' }); + + const db = getDatabase(); + + // Store weight as notes JSON in health_records + const description = JSON.stringify({ + weight_oz: weight_oz || null, + weight_lbs: weight_lbs || null, + notes: notes || '' + }); + + const result = db.prepare(` + INSERT INTO health_records (dog_id, record_type, record_date, description, vet_name) + VALUES (?, ?, ?, ?, ?) + `).run(puppyId, record_type || 'weight_log', record_date, description, null); + + const log = db.prepare('SELECT * FROM health_records WHERE id = ?').get(result.lastInsertRowid); + res.status(201).json(log); + } catch (error) { + res.status(500).json({ error: error.message }); + } +}); + +// DELETE weight/health log entry +router.delete('/:litterId/puppies/:puppyId/logs/:logId', (req, res) => { + try { + const db = getDatabase(); + db.prepare('DELETE FROM health_records WHERE id = ? AND dog_id = ?').run(req.params.logId, req.params.puppyId); + res.json({ message: 'Log entry deleted' }); + } 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 });