Fix: Don't return sire/dam columns from dogs table, select explicit fields #13

Merged
jason merged 1 commits from feature/enhanced-litters-and-pedigree into master 2026-03-09 01:31:00 -05:00

View File

@@ -41,7 +41,15 @@ const emptyToNull = (value) => {
router.get('/', (req, res) => { router.get('/', (req, res) => {
try { try {
const db = getDatabase(); 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 // Parse photo_urls JSON
dogs.forEach(dog => { dogs.forEach(dog => {
@@ -58,7 +66,14 @@ router.get('/', (req, res) => {
router.get('/:id', (req, res) => { router.get('/:id', (req, res) => {
try { try {
const db = getDatabase(); 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) { if (!dog) {
return res.status(404).json({ error: 'Dog not found' }); 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) : []; dog.photo_urls = dog.photo_urls ? JSON.parse(dog.photo_urls) : [];
// Get parents // Get parents from parents table
const parents = db.prepare(` const parents = db.prepare(`
SELECT p.parent_type, d.* SELECT p.parent_type, d.*
FROM parents p FROM parents p
@@ -93,7 +108,7 @@ router.get('/:id', (req, res) => {
// POST create new dog // POST create new dog
router.post('/', (req, res) => { router.post('/', (req, res) => {
try { 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) { 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' });
@@ -103,8 +118,8 @@ router.post('/', (req, res) => {
// Convert empty strings to null for optional fields // Convert empty strings to null for optional fields
const result = db.prepare(` const result = db.prepare(`
INSERT INTO dogs (name, registration_number, breed, sex, birth_date, color, microchip, notes, photo_urls) INSERT INTO dogs (name, registration_number, breed, sex, birth_date, color, microchip, notes, litter_id, photo_urls)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`).run( `).run(
name, name,
emptyToNull(registration_number), emptyToNull(registration_number),
@@ -112,8 +127,9 @@ router.post('/', (req, res) => {
sex, sex,
emptyToNull(birth_date), emptyToNull(birth_date),
emptyToNull(color), emptyToNull(color),
emptyToNull(microchip), // Convert empty string to NULL emptyToNull(microchip),
emptyToNull(notes), 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); 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 = []; dog.photo_urls = [];
res.status(201).json(dog); res.status(201).json(dog);
@@ -139,7 +161,7 @@ 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, 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(); const db = getDatabase();
@@ -147,7 +169,7 @@ router.put('/:id', (req, res) => {
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 = ?
WHERE id = ? WHERE id = ?
`).run( `).run(
name, name,
@@ -156,8 +178,9 @@ router.put('/:id', (req, res) => {
sex, sex,
emptyToNull(birth_date), emptyToNull(birth_date),
emptyToNull(color), emptyToNull(color),
emptyToNull(microchip), // Convert empty string to NULL emptyToNull(microchip),
emptyToNull(notes), emptyToNull(notes),
emptyToNull(litter_id),
req.params.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); 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) : []; dog.photo_urls = dog.photo_urls ? JSON.parse(dog.photo_urls) : [];
res.json(dog); res.json(dog);
@@ -247,4 +276,4 @@ router.delete('/:id/photos/:photoIndex', (req, res) => {
} }
}); });
module.exports = router; module.exports = router;