Files
breedr/server/routes/dogs.js

365 lines
14 KiB
JavaScript
Raw Normal View History

2026-03-08 22:44:23 -05:00
const express = require('express');
const router = express.Router();
2026-03-08 22:44:23 -05:00
const { getDatabase } = require('../db/init');
const multer = require('multer');
const path = require('path');
const fs = require('fs');
2026-03-08 22:44:23 -05:00
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 },
2026-03-08 22:44:23 -05:00
fileFilter: (req, file, cb) => {
const allowed = /jpeg|jpg|png|gif|webp/;
if (allowed.test(path.extname(file.originalname).toLowerCase()) && allowed.test(file.mimetype)) {
2026-03-08 22:44:23 -05:00
cb(null, true);
} else {
cb(new Error('Only image files are allowed'));
}
}
});
const emptyToNull = (v) => (v === '' || v === undefined) ? null : v;
// ── Shared SELECT columns ────────────────────────────────────────────────
const DOG_COLS = `
id, name, registration_number, breed, sex, birth_date,
color, microchip, photo_urls, notes, litter_id, is_active,
is_champion, is_external, created_at, updated_at
`;
// ── Helper: attach parents to a list of dogs ─────────────────────────────
function attachParents(db, dogs) {
const parentStmt = db.prepare(`
SELECT p.parent_type, d.id, d.name, d.is_champion, d.is_external
FROM parents p
JOIN dogs d ON p.parent_id = d.id
WHERE p.dog_id = ?
`);
dogs.forEach(dog => {
dog.photo_urls = dog.photo_urls ? JSON.parse(dog.photo_urls) : [];
const parents = parentStmt.all(dog.id);
dog.sire = parents.find(p => p.parent_type === 'sire') || null;
dog.dam = parents.find(p => p.parent_type === 'dam') || null;
});
return dogs;
}
// ── GET dogs (paginated)
// Default: kennel dogs only (is_external = 0)
// ?include_external=1 : all active dogs (kennel + external)
// ?external_only=1 : external dogs only
// ?page=1&limit=50 : pagination
// ?search=term : filter by name or registration_number
// ?sex=male|female : filter by sex
// Response: { data, total, page, limit, stats: { total, males, females } }
// ─────────────────────────────────────────────────────────────────────────
2026-03-08 22:44:23 -05:00
router.get('/', (req, res) => {
try {
const db = getDatabase();
const includeExternal = req.query.include_external === '1' || req.query.include_external === 'true';
const externalOnly = req.query.external_only === '1' || req.query.external_only === 'true';
const search = (req.query.search || '').trim();
const sex = req.query.sex === 'male' || req.query.sex === 'female' ? req.query.sex : '';
const page = Math.max(1, parseInt(req.query.page, 10) || 1);
const limit = Math.min(200, Math.max(1, parseInt(req.query.limit, 10) || 50));
const offset = (page - 1) * limit;
let baseWhere;
if (externalOnly) {
baseWhere = 'is_active = 1 AND is_external = 1';
} else if (includeExternal) {
baseWhere = 'is_active = 1';
} else {
baseWhere = 'is_active = 1 AND is_external = 0';
}
const filters = [];
const params = [];
if (search) {
filters.push('(name LIKE ? OR registration_number LIKE ?)');
params.push(`%${search}%`, `%${search}%`);
}
if (sex) {
filters.push('sex = ?');
params.push(sex);
}
const whereClause = 'WHERE ' + [baseWhere, ...filters].join(' AND ');
const total = db.prepare(`SELECT COUNT(*) as count FROM dogs ${whereClause}`).get(...params).count;
const statsWhere = externalOnly
? 'WHERE is_active = 1 AND is_external = 1'
: includeExternal
? 'WHERE is_active = 1'
: 'WHERE is_active = 1 AND is_external = 0';
const stats = db.prepare(`
SELECT
COUNT(*) as total,
SUM(CASE WHEN sex = 'male' THEN 1 ELSE 0 END) as males,
SUM(CASE WHEN sex = 'female' THEN 1 ELSE 0 END) as females
FROM dogs ${statsWhere}
`).get();
const dogs = db.prepare(`
SELECT ${DOG_COLS}
FROM dogs
${whereClause}
ORDER BY name
LIMIT ? OFFSET ?
`).all(...params, limit, offset);
res.json({ data: attachParents(db, dogs), total, page, limit, stats });
} catch (error) {
console.error('Error fetching dogs:', error);
res.status(500).json({ error: error.message });
}
});
// ── GET all dogs (kennel + external) for dropdowns/pairing/pedigree ──────────
// Kept for backwards-compat; equivalent to GET /?include_external=1
router.get('/all', (req, res) => {
try {
const db = getDatabase();
const dogs = db.prepare(`
SELECT ${DOG_COLS}
FROM dogs
WHERE is_active = 1
ORDER BY name
`).all();
res.json(attachParents(db, dogs));
} catch (error) {
console.error('Error fetching all dogs:', error);
res.status(500).json({ error: error.message });
}
});
// ── GET external dogs only (is_external = 1) ──────────────────────────────
// Kept for backwards-compat; equivalent to GET /?external_only=1
router.get('/external', (req, res) => {
try {
const db = getDatabase();
const dogs = db.prepare(`
SELECT ${DOG_COLS}
FROM dogs
WHERE is_active = 1 AND is_external = 1
ORDER BY name
`).all();
res.json(attachParents(db, dogs));
2026-03-08 22:44:23 -05:00
} catch (error) {
console.error('Error fetching external dogs:', error);
2026-03-08 22:44:23 -05:00
res.status(500).json({ error: error.message });
}
});
// ── GET single dog (with parents + offspring) ──────────────────────────
2026-03-08 22:44:23 -05:00
router.get('/:id', (req, res) => {
try {
const db = getDatabase();
const dog = db.prepare(`SELECT ${DOG_COLS} FROM dogs WHERE id = ?`).get(req.params.id);
if (!dog) return res.status(404).json({ error: 'Dog not found' });
2026-03-08 22:44:23 -05:00
dog.photo_urls = dog.photo_urls ? JSON.parse(dog.photo_urls) : [];
2026-03-08 22:44:23 -05:00
const parents = db.prepare(`
SELECT p.parent_type, d.id, d.name, d.is_champion, d.is_external
FROM parents p
JOIN dogs d ON p.parent_id = d.id
2026-03-08 22:44:23 -05:00
WHERE p.dog_id = ?
`).all(req.params.id);
2026-03-08 22:44:23 -05:00
dog.sire = parents.find(p => p.parent_type === 'sire') || null;
dog.dam = parents.find(p => p.parent_type === 'dam') || null;
2026-03-08 22:44:23 -05:00
dog.offspring = db.prepare(`
SELECT d.id, d.name, d.sex, d.is_champion, d.is_external
FROM dogs d
2026-03-08 22:44:23 -05:00
JOIN parents p ON d.id = p.dog_id
WHERE p.parent_id = ? AND d.is_active = 1
`).all(req.params.id);
2026-03-08 22:44:23 -05:00
res.json(dog);
} catch (error) {
console.error('Error fetching dog:', error);
2026-03-08 22:44:23 -05:00
res.status(500).json({ error: error.message });
}
});
// ── POST create dog ─────────────────────────────────────────────────────
2026-03-08 22:44:23 -05:00
router.post('/', (req, res) => {
try {
const { name, registration_number, breed, sex, birth_date, color,
microchip, notes, sire_id, dam_id, litter_id, is_champion, is_external } = req.body;
2026-03-08 22:44:23 -05:00
if (!name || !breed || !sex) {
return res.status(400).json({ error: 'Name, breed, and sex are required' });
}
2026-03-08 22:44:23 -05:00
const db = getDatabase();
const result = db.prepare(`
INSERT INTO dogs (name, registration_number, breed, sex, birth_date, color,
microchip, notes, litter_id, photo_urls, is_champion, is_external)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`).run(
name,
emptyToNull(registration_number),
breed, sex,
emptyToNull(birth_date),
emptyToNull(color),
emptyToNull(microchip),
emptyToNull(notes),
emptyToNull(litter_id),
'[]',
is_champion ? 1 : 0,
is_external ? 1 : 0
);
2026-03-08 22:44:23 -05:00
const dogId = result.lastInsertRowid;
if (sire_id && sire_id !== '' && sire_id !== null) {
db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, ?)').run(dogId, sire_id, 'sire');
2026-03-08 22:44:23 -05:00
}
if (dam_id && dam_id !== '' && dam_id !== null) {
db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, ?)').run(dogId, dam_id, 'dam');
2026-03-08 22:44:23 -05:00
}
const dog = db.prepare(`SELECT ${DOG_COLS} FROM dogs WHERE id = ?`).get(dogId);
2026-03-08 22:44:23 -05:00
dog.photo_urls = [];
console.log(`✔ Dog created: ${dog.name} (ID: ${dogId}, external: ${dog.is_external})`);
2026-03-08 22:44:23 -05:00
res.status(201).json(dog);
} catch (error) {
console.error('Error creating dog:', error);
2026-03-08 22:44:23 -05:00
res.status(500).json({ error: error.message });
}
});
// ── PUT update dog ───────────────────────────────────────────────────────
2026-03-08 22:44:23 -05:00
router.put('/:id', (req, res) => {
try {
const { name, registration_number, breed, sex, birth_date, color,
microchip, notes, sire_id, dam_id, litter_id, is_champion, is_external } = req.body;
const db = getDatabase();
db.prepare(`
UPDATE dogs
SET name = ?, registration_number = ?, breed = ?, sex = ?,
birth_date = ?, color = ?, microchip = ?, notes = ?,
litter_id = ?, is_champion = ?, is_external = ?, updated_at = datetime('now')
WHERE id = ?
`).run(
name,
emptyToNull(registration_number),
breed, sex,
emptyToNull(birth_date),
emptyToNull(color),
emptyToNull(microchip),
emptyToNull(notes),
emptyToNull(litter_id),
is_champion ? 1 : 0,
is_external ? 1 : 0,
req.params.id
);
2026-03-08 22:44:23 -05:00
db.prepare('DELETE FROM parents WHERE dog_id = ?').run(req.params.id);
if (sire_id && sire_id !== '' && sire_id !== null) {
db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, ?)').run(req.params.id, sire_id, 'sire');
2026-03-08 22:44:23 -05:00
}
if (dam_id && dam_id !== '' && dam_id !== null) {
db.prepare('INSERT INTO parents (dog_id, parent_id, parent_type) VALUES (?, ?, ?)').run(req.params.id, dam_id, 'dam');
2026-03-08 22:44:23 -05:00
}
const dog = db.prepare(`SELECT ${DOG_COLS} FROM dogs WHERE id = ?`).get(req.params.id);
2026-03-08 22:44:23 -05:00
dog.photo_urls = dog.photo_urls ? JSON.parse(dog.photo_urls) : [];
console.log(`✔ Dog updated: ${dog.name} (ID: ${req.params.id})`);
2026-03-08 22:44:23 -05:00
res.json(dog);
} catch (error) {
console.error('Error updating dog:', error);
2026-03-08 22:44:23 -05:00
res.status(500).json({ error: error.message });
}
});
// ── DELETE dog (hard delete with cascade) ───────────────────────────────
2026-03-08 22:44:23 -05:00
router.delete('/:id', (req, res) => {
try {
const db = getDatabase();
const existing = db.prepare('SELECT id, name FROM dogs WHERE id = ?').get(req.params.id);
if (!existing) return res.status(404).json({ error: 'Dog not found' });
const id = req.params.id;
db.prepare('DELETE FROM parents WHERE parent_id = ?').run(id);
db.prepare('DELETE FROM parents WHERE dog_id = ?').run(id);
db.prepare('DELETE FROM health_records WHERE dog_id = ?').run(id);
db.prepare('DELETE FROM heat_cycles WHERE dog_id = ?').run(id);
db.prepare('DELETE FROM dogs WHERE id = ?').run(id);
console.log(`✔ Dog #${id} (${existing.name}) permanently deleted`);
res.json({ success: true, message: `${existing.name} has been deleted` });
2026-03-08 22:44:23 -05:00
} catch (error) {
console.error('Error deleting dog:', error);
2026-03-08 22:44:23 -05:00
res.status(500).json({ error: error.message });
}
});
// ── POST upload photo ────────────────────────────────────────────────────
2026-03-08 22:44:23 -05:00
router.post('/:id/photos', upload.single('photo'), (req, res) => {
try {
if (!req.file) return res.status(400).json({ error: 'No file uploaded' });
2026-03-08 22:44:23 -05:00
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' });
2026-03-08 22:44:23 -05:00
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);
2026-03-08 22:44:23 -05:00
res.json({ url: `/uploads/${req.file.filename}`, photos: photoUrls });
} catch (error) {
console.error('Error uploading photo:', error);
2026-03-08 22:44:23 -05:00
res.status(500).json({ error: error.message });
}
});
// ── DELETE photo ──────────────────────────────────────────────────────
2026-03-08 22:44:23 -05:00
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) : [];
2026-03-08 22:44:23 -05:00
const photoIndex = parseInt(req.params.photoIndex);
2026-03-08 22:44:23 -05:00
if (photoIndex >= 0 && photoIndex < photoUrls.length) {
const photoPath = path.join(
process.env.UPLOAD_PATH || path.join(__dirname, '../../uploads'),
path.basename(photoUrls[photoIndex])
);
if (fs.existsSync(photoPath)) fs.unlinkSync(photoPath);
2026-03-08 22:44:23 -05:00
photoUrls.splice(photoIndex, 1);
db.prepare('UPDATE dogs SET photo_urls = ? WHERE id = ?').run(JSON.stringify(photoUrls), req.params.id);
}
2026-03-08 22:44:23 -05:00
res.json({ photos: photoUrls });
} catch (error) {
console.error('Error deleting photo:', error);
2026-03-08 22:44:23 -05:00
res.status(500).json({ error: error.message });
}
});
module.exports = router;