Files
breedr/server/routes/dogs.js

314 lines
10 KiB
JavaScript

const express = require('express');
const router = express.Router();
const { getDatabase } = require('../db/init');
const multer = require('multer');
const path = require('path');
const fs = require('fs');
// Configure multer for photo uploads
const storage = multer.diskStorage({
destination: (req, file, cb) => {
const uploadPath = process.env.UPLOAD_PATH || path.join(__dirname, '../../uploads');
cb(null, uploadPath);
},
filename: (req, file, cb) => {
const uniqueName = `${Date.now()}-${Math.random().toString(36).substring(7)}${path.extname(file.originalname)}`;
cb(null, uniqueName);
}
});
const upload = multer({
storage,
limits: { fileSize: 10 * 1024 * 1024 }, // 10MB limit
fileFilter: (req, file, cb) => {
const allowedTypes = /jpeg|jpg|png|gif|webp/;
const extname = allowedTypes.test(path.extname(file.originalname).toLowerCase());
const mimetype = allowedTypes.test(file.mimetype);
if (extname && mimetype) {
cb(null, true);
} else {
cb(new Error('Only image files are allowed'));
}
}
});
// Helper function to convert empty strings to null
const emptyToNull = (value) => {
return (value === '' || value === undefined) ? null : value;
};
// GET all dogs
router.get('/', (req, res) => {
try {
const db = getDatabase();
const dogs = db.prepare(`
SELECT id, name, registration_number, breed, sex, birth_date,
color, microchip, photo_urls, notes, litter_id, is_active,
created_at, updated_at
FROM dogs
WHERE is_active = 1
ORDER BY name
`).all();
// Parse photo_urls JSON
dogs.forEach(dog => {
dog.photo_urls = dog.photo_urls ? JSON.parse(dog.photo_urls) : [];
});
res.json(dogs);
} catch (error) {
console.error('Error fetching dogs:', error);
res.status(500).json({ error: error.message });
}
});
// GET single dog by ID with parents and offspring
router.get('/:id', (req, res) => {
try {
const db = getDatabase();
const dog = db.prepare(`
SELECT id, name, registration_number, breed, sex, birth_date,
color, microchip, photo_urls, notes, litter_id, 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' });
}
dog.photo_urls = dog.photo_urls ? JSON.parse(dog.photo_urls) : [];
// Get parents from parents table
const parents = db.prepare(`
SELECT p.parent_type, d.*
FROM parents p
JOIN dogs d ON p.parent_id = d.id
WHERE p.dog_id = ?
`).all(req.params.id);
dog.sire = parents.find(p => p.parent_type === 'sire') || null;
dog.dam = parents.find(p => p.parent_type === 'dam') || null;
// Get offspring
dog.offspring = db.prepare(`
SELECT d.* FROM dogs d
JOIN parents p ON d.id = p.dog_id
WHERE p.parent_id = ? AND d.is_active = 1
`).all(req.params.id);
res.json(dog);
} catch (error) {
console.error('Error fetching dog:', error);
res.status(500).json({ error: error.message });
}
});
// POST create new dog
router.post('/', (req, res) => {
try {
const { name, registration_number, breed, sex, birth_date, color, microchip, notes, sire_id, dam_id, litter_id } = req.body;
console.log('Creating dog with data:', { name, breed, sex, sire_id, dam_id, litter_id });
if (!name || !breed || !sex) {
return res.status(400).json({ error: 'Name, breed, and sex are required' });
}
const db = getDatabase();
// Insert dog (dogs table has NO sire/dam columns)
const result = db.prepare(`
INSERT INTO dogs (name, registration_number, breed, sex, birth_date, color, microchip, notes, litter_id, photo_urls)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`).run(
name,
emptyToNull(registration_number),
breed,
sex,
emptyToNull(birth_date),
emptyToNull(color),
emptyToNull(microchip),
emptyToNull(notes),
emptyToNull(litter_id),
'[]'
);
const dogId = result.lastInsertRowid;
console.log(`✓ Dog inserted with ID: ${dogId}`);
// Add sire relationship if provided
if (sire_id && sire_id !== '' && sire_id !== null) {
console.log(` Adding sire relationship: dog ${dogId} -> sire ${sire_id}`);
db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, ?)').
run(dogId, sire_id, 'sire');
console.log(` ✓ Sire relationship added`);
}
// Add dam relationship if provided
if (dam_id && dam_id !== '' && dam_id !== null) {
console.log(` Adding dam relationship: dog ${dogId} -> dam ${dam_id}`);
db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, ?)').
run(dogId, dam_id, 'dam');
console.log(` ✓ Dam relationship added`);
}
// Fetch the created dog
const dog = db.prepare(`
SELECT id, name, registration_number, breed, sex, birth_date,
color, microchip, photo_urls, notes, litter_id, is_active,
created_at, updated_at
FROM dogs
WHERE id = ?
`).get(dogId);
dog.photo_urls = [];
console.log(`✓ Dog created successfully: ${dog.name} (ID: ${dogId})`);
res.status(201).json(dog);
} catch (error) {
console.error('Error creating dog:', error);
res.status(500).json({ error: error.message });
}
});
// 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 } = req.body;
console.log(`Updating dog ${req.params.id} with data:`, { name, breed, sex, sire_id, dam_id, litter_id });
const db = getDatabase();
// Update dog record (dogs table has NO sire/dam columns)
db.prepare(`
UPDATE dogs
SET name = ?, registration_number = ?, breed = ?, sex = ?,
birth_date = ?, color = ?, microchip = ?, notes = ?, litter_id = ?
WHERE id = ?
`).run(
name,
emptyToNull(registration_number),
breed,
sex,
emptyToNull(birth_date),
emptyToNull(color),
emptyToNull(microchip),
emptyToNull(notes),
emptyToNull(litter_id),
req.params.id
);
console.log(` ✓ Dog record updated`);
// Remove existing parent relationships
db.prepare('DELETE FROM parents WHERE dog_id = ?').run(req.params.id);
console.log(` ✓ Old parent relationships removed`);
// Add new sire relationship if provided
if (sire_id && sire_id !== '' && sire_id !== null) {
console.log(` Adding sire relationship: dog ${req.params.id} -> sire ${sire_id}`);
db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, ?)').
run(req.params.id, sire_id, 'sire');
console.log(` ✓ Sire relationship added`);
}
// Add new dam relationship if provided
if (dam_id && dam_id !== '' && dam_id !== null) {
console.log(` Adding dam relationship: dog ${req.params.id} -> dam ${dam_id}`);
db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, ?)').
run(req.params.id, dam_id, 'dam');
console.log(` ✓ Dam relationship added`);
}
// Fetch updated dog
const dog = db.prepare(`
SELECT id, name, registration_number, breed, sex, birth_date,
color, microchip, photo_urls, notes, litter_id, 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) : [];
console.log(`✓ Dog updated successfully: ${dog.name} (ID: ${req.params.id})`);
res.json(dog);
} catch (error) {
console.error('Error updating dog:', error);
res.status(500).json({ error: error.message });
}
});
// DELETE dog (soft delete)
router.delete('/:id', (req, res) => {
try {
const db = getDatabase();
db.prepare('UPDATE dogs SET is_active = 0 WHERE id = ?').run(req.params.id);
console.log(`✓ Dog soft-deleted: ID ${req.params.id}`);
res.json({ message: 'Dog deleted successfully' });
} catch (error) {
console.error('Error deleting dog:', error);
res.status(500).json({ error: error.message });
}
});
// POST upload photo for dog
router.post('/:id/photos', upload.single('photo'), (req, res) => {
try {
if (!req.file) {
return res.status(400).json({ error: 'No file uploaded' });
}
const db = getDatabase();
const dog = db.prepare('SELECT photo_urls FROM dogs WHERE id = ?').get(req.params.id);
if (!dog) {
return res.status(404).json({ error: 'Dog not found' });
}
const photoUrls = dog.photo_urls ? JSON.parse(dog.photo_urls) : [];
photoUrls.push(`/uploads/${req.file.filename}`);
db.prepare('UPDATE dogs SET photo_urls = ? WHERE id = ?').run(JSON.stringify(photoUrls), req.params.id);
res.json({ url: `/uploads/${req.file.filename}`, photos: photoUrls });
} catch (error) {
console.error('Error uploading photo:', error);
res.status(500).json({ error: error.message });
}
});
// DELETE photo from dog
router.delete('/:id/photos/:photoIndex', (req, res) => {
try {
const db = getDatabase();
const dog = db.prepare('SELECT photo_urls FROM dogs WHERE id = ?').get(req.params.id);
if (!dog) {
return res.status(404).json({ error: 'Dog not found' });
}
const photoUrls = dog.photo_urls ? JSON.parse(dog.photo_urls) : [];
const photoIndex = parseInt(req.params.photoIndex);
if (photoIndex >= 0 && photoIndex < photoUrls.length) {
const photoPath = path.join(process.env.UPLOAD_PATH || path.join(__dirname, '../../uploads'), path.basename(photoUrls[photoIndex]));
// Delete file from disk
if (fs.existsSync(photoPath)) {
fs.unlinkSync(photoPath);
}
photoUrls.splice(photoIndex, 1);
db.prepare('UPDATE dogs SET photo_urls = ? WHERE id = ?').run(JSON.stringify(photoUrls), req.params.id);
}
res.json({ photos: photoUrls });
} catch (error) {
console.error('Error deleting photo:', error);
res.status(500).json({ error: error.message });
}
});
module.exports = router;