diff --git a/server/routes/dogs.js b/server/routes/dogs.js index 9fc25ad..b320c55 100644 --- a/server/routes/dogs.js +++ b/server/routes/dogs.js @@ -41,7 +41,15 @@ const emptyToNull = (value) => { router.get('/', (req, res) => { try { const db = getDatabase(); - const dogs = db.prepare('SELECT * FROM dogs WHERE is_active = 1 ORDER BY name').all(); + // Select only the fields we want, excluding sire/dam if they exist + const dogs = db.prepare(` + SELECT id, name, registration_number, microchip, sex, birth_date, breed, + color, weight, height, notes, litter_id, photo_urls, is_active, + created_at, updated_at + FROM dogs + WHERE is_active = 1 + ORDER BY name + `).all(); // Parse photo_urls JSON dogs.forEach(dog => { @@ -58,7 +66,14 @@ router.get('/', (req, res) => { router.get('/:id', (req, res) => { try { const db = getDatabase(); - const dog = db.prepare('SELECT * FROM dogs WHERE id = ?').get(req.params.id); + // Select only the fields we want, excluding sire/dam if they exist + const dog = db.prepare(` + SELECT id, name, registration_number, microchip, sex, birth_date, breed, + color, weight, height, notes, litter_id, photo_urls, is_active, + created_at, updated_at + FROM dogs + WHERE id = ? + `).get(req.params.id); if (!dog) { return res.status(404).json({ error: 'Dog not found' }); @@ -66,7 +81,7 @@ router.get('/:id', (req, res) => { dog.photo_urls = dog.photo_urls ? JSON.parse(dog.photo_urls) : []; - // Get parents + // Get parents from parents table const parents = db.prepare(` SELECT p.parent_type, d.* FROM parents p @@ -93,7 +108,7 @@ router.get('/:id', (req, res) => { // POST create new dog router.post('/', (req, res) => { try { - const { name, registration_number, breed, sex, birth_date, color, microchip, notes, sire_id, dam_id } = req.body; + const { name, registration_number, breed, sex, birth_date, color, microchip, notes, sire_id, dam_id, litter_id } = req.body; if (!name || !breed || !sex) { return res.status(400).json({ error: 'Name, breed, and sex are required' }); @@ -103,8 +118,8 @@ router.post('/', (req, res) => { // Convert empty strings to null for optional fields const result = db.prepare(` - INSERT INTO dogs (name, registration_number, breed, sex, birth_date, color, microchip, notes, photo_urls) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) + INSERT INTO dogs (name, registration_number, breed, sex, birth_date, color, microchip, notes, litter_id, photo_urls) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) `).run( name, emptyToNull(registration_number), @@ -112,8 +127,9 @@ router.post('/', (req, res) => { sex, emptyToNull(birth_date), emptyToNull(color), - emptyToNull(microchip), // Convert empty string to NULL - emptyToNull(notes), + emptyToNull(microchip), + emptyToNull(notes), + emptyToNull(litter_id), '[]' ); @@ -127,7 +143,13 @@ router.post('/', (req, res) => { db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, "dam")').run(dogId, dam_id); } - const dog = db.prepare('SELECT * FROM dogs WHERE id = ?').get(dogId); + const dog = db.prepare(` + SELECT id, name, registration_number, microchip, sex, birth_date, breed, + color, weight, height, notes, litter_id, photo_urls, is_active, + created_at, updated_at + FROM dogs + WHERE id = ? + `).get(dogId); dog.photo_urls = []; res.status(201).json(dog); @@ -139,7 +161,7 @@ router.post('/', (req, res) => { // PUT update dog router.put('/:id', (req, res) => { try { - const { name, registration_number, breed, sex, birth_date, color, microchip, notes, sire_id, dam_id } = req.body; + const { name, registration_number, breed, sex, birth_date, color, microchip, notes, sire_id, dam_id, litter_id } = req.body; const db = getDatabase(); @@ -147,7 +169,7 @@ router.put('/:id', (req, res) => { db.prepare(` UPDATE dogs SET name = ?, registration_number = ?, breed = ?, sex = ?, - birth_date = ?, color = ?, microchip = ?, notes = ? + birth_date = ?, color = ?, microchip = ?, notes = ?, litter_id = ? WHERE id = ? `).run( name, @@ -156,8 +178,9 @@ router.put('/:id', (req, res) => { sex, emptyToNull(birth_date), emptyToNull(color), - emptyToNull(microchip), // Convert empty string to NULL - emptyToNull(notes), + emptyToNull(microchip), + emptyToNull(notes), + emptyToNull(litter_id), req.params.id ); @@ -171,7 +194,13 @@ router.put('/:id', (req, res) => { db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, "dam")').run(req.params.id, dam_id); } - const dog = db.prepare('SELECT * FROM dogs WHERE id = ?').get(req.params.id); + const dog = db.prepare(` + SELECT id, name, registration_number, microchip, sex, birth_date, breed, + color, weight, height, notes, litter_id, photo_urls, is_active, + created_at, updated_at + FROM dogs + WHERE id = ? + `).get(req.params.id); dog.photo_urls = dog.photo_urls ? JSON.parse(dog.photo_urls) : []; res.json(dog); @@ -247,4 +276,4 @@ router.delete('/:id/photos/:photoIndex', (req, res) => { } }); -module.exports = router; \ No newline at end of file +module.exports = router;